简体   繁体   中英

How to extract field values from query result

I have problem in extracting data from query result. I have the following method in my model that returns a result of many rows

 function method(){
    $this->db->select('*');
    $this->db->from('table_name');

    $query = $this->db->get();

    return $query->result();
 }

from my controller I call the above function like this

     $data['returned_records']= $this->model_name->method(); 

and now I want to display returned_records in table format in a view. How do I go about that?

<table>
    <tr>
        <th>Column Name</th>
    </tr>
    <?php foreach($returned_records as $row){ ?>
    <tr>
        <td><?php $row->column_name; ?></td>
    </tr>
    <?php } ?>
</table>

To display the output in view in tablur form The code would be something like this:

<table border="1">  
          <tbody>  
             <tr>  
                <td>Heading 1</td>  
                <td>Heading 2</td>  
              // add more according to requirement
             </tr>  
             <?php  
             foreach ($returned_records->result() as $row)  
             {  
                ?><tr>  
                <td><?php echo $row->Column_name;?></td>  
                <td><?php echo $row->Column_name;?></td>
               // add more according to requirement  
                </tr>  
             <?php }  
             ?>  
          </tbody>  
       </table>  
<table border="1">  
          <tbody>  
             <tr>  
                <th>Heading 1</th>  
                <th>Heading 2</th>  
             </tr>  
             <?php  
             foreach ($returned_records as $row)  
             {  
                ?><tr>  
                <td><?php echo $row->Column_name1;?></td>  
                <td><?php echo $row->Column_name2;?></td>  
                </tr>  
             <?php }  
             ?>  
          </tbody>  
       </table> 

as you have already returned as "query->result()" no need write $returned_records->result() again..!!

You have to print data in table format in view file.

<table>
   <th>Title</th>
    <?php foreach($returned_records as $result){
    <tr>
       <td><?php echo $result->title;?></td>
    </tr>
   <?php 
    }
  ?>
</table> 

In your controller function pass $data like the below code:

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

In your view:

<table>
    <tr>
        <th>Column Name</th>
    </tr>
    <?php foreach($returned_records as $row){ ?>
    <tr>
        <td><?= $row->column_name ?></td>
    </tr>
    <?php } ?>
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM