2

What will be the process to show to data in codeignator using Model View and Controller?

Controller

public function SecondarySchoolLevel() {
    $this->load->model('Schoolprogram_model');
    $data['progcategoryid'] = $this->Schoolprogram_model->getschoolprog(4);

    $this->load->view('SchoolPrograms', $data);
}

View

$query = $this->db->query("SELECT * from programs where progcategoryid = $progcategoryid");

foreach ($query->result_array() as $row){
?>
<tr id="r<?php echo $row["progid"]; ?>">
<td><?php echo $row["progcode"]; ?></td>
<td><?php echo $row["activities"]; ?></td>
<td><?php echo $row["description"]; ?></td>
<td><?php echo $row["session"]; ?></td>
<td><?php echo $row["hours"]; ?></td>
<td><?php echo $row["cost"]; ?></td>
<td><?php echo $row["progcategoryid"]; ?></td>
<td><?php echo $row["agefrom"]; ?></td>
<td><?php echo $row["ageto"]; ?></td>

Model

function getschoolprog($progcategoryid) {
    $this->db->where('progcategoryid', $progcategoryid);
    $query = $this->db->get('programs');
    return $query->result();
}

This is my code.

4
  • you want to show your query result in your codeignter views Commented May 19, 2015 at 10:45
  • Yes....if u have any idea. please let me know. Commented May 19, 2015 at 10:46
  • You are executing query in view. This is not correct as per the MVC. The query should be executed in model. Commented May 19, 2015 at 10:48
  • First have a look at mvc Commented May 19, 2015 at 10:57

2 Answers 2

3

Controller

public function SecondarySchoolLevel() {
    $this->load->model('Schoolprogram_model');
    $data['progcategoryid'] = $this->Schoolprogram_model->getschoolprog(4);//result of your query is stored in this ($data['progcategoryid']) variable
    $this->load->view('SchoolPrograms', $data);
}

Model

function getschoolprog($progcategoryid) {
    $this->db->select('*');
    $this->db->where('progcategoryid', $progcategoryid);
    $query = $this->db->get('programs');
    return $query->result_array();//<---- returns you an array
}

View

foreach ($progcategoryid as $key => $row) { ?>
    <tr id='r<?php echo $row["progid"]; ?>'>
        <td><?php echo $row["progcode"]; ?></td>
        <td><?php echo $row["activities"]; ?></td>
        <td><?php echo $row["description"]; ?></td>
        <td><?php echo $row["session"]; ?></td>
        <td><?php echo $row["hours"]; ?></td>
        <td><?php echo $row["cost"]; ?></td>
        <td><?php echo $row["progcategoryid"]; ?></td>
        <td><?php echo $row["agefrom"]; ?></td>
        <td><?php echo $row["ageto"]; ?></td>
    </tr>
    <?php } ?>
Sign up to request clarification or add additional context in comments.

Comments

1

Controller

 $data['progcategoryid'] = $this->Schoolprogram_model->getschoolprog(4);
 $this->load->view('SchoolPrograms', $data);

View

<?php foreach($progcategoryid as $row)
{
?>
 <td><?php echo $row->progcode; ?></td>// you can render your data like this

<?php }?>

No need to write query in view because you've already written it within your model.

You just need to send your data to views

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.