how to view individual field of the table in codeigniter

喜夏-厌秋 提交于 2019-12-25 02:19:08

问题


I have created following table in mysql

I want to retrieve one of the field i.e abstract or author or Title by using id dynamically in view field. These are my model, controller and view code. Model:This is my model

 $this->db->select("*"); 
               $this->db->limit(10);  
               $this->db->from("abcde");
     $query = $this->db->query("SELECT * FROM abcde ORDER BY DocumentID ASC"); 

Controller: this is my controller here

 $this->load->model("Sample_model");  
              $data["fetch_data"] = $this->Sample_model->fetch_data();  
              $data['view']='services_view';
              $this->load->view('load_view', $data);
    return $query; 

View:This is my view page

 <?php 

           if($fetch_data->num_rows() > 0)  
           {  
                foreach($fetch_data->result() as $row)  
                {  
           ?>  
                <tr>  

                     <td><?php echo $row->Abstract

                </tr>  
           <?php       
                }  
           }  

           ?>  
            </table> 

The above view is displaying all the record of abstract.I want to use ID in view so that i can get specific abstract in one line.

for example display abstract where id=1 or display author where id 3.Important point is here that i want to use id in view of the codeigniter. I will be grateful to you for your help.


回答1:


Hope this will help you :

You can use custom helper to get the desired result

Add a file in your helpers folder name custom_helper.php and autoload with autoload.php like this :

$autoload['helper'] = array('custom');

In your custom_helper.php add a function like this :

function get_abstract($id)
{
   $ci = & get_instance();
   $query = $ci->db->get_where('abcde',['id' => $id]); 
   return $query->row()->abstract;   
}

in your view call this get_abstract function like this:

   <?php 

    if($fetch_data->num_rows() > 0)  
    {  
        foreach($fetch_data->result() as $row)  
        {  
    ?>  
        <tr>  
            <!-- get abstract by id like this -->
            <td><?php echo get_abstract($row->id);?></td>
        </tr>  
  <?php }  
}?>  

Update : your controller should be like this :

public function services() 
{ 
    $this->load->model("Sample_model"); 
    $data["fetch_data"] = $this->Sample_model->fetch_data(); 
    $data['view']='services_view'; 
    $this->load->view('load_view', $data); 
}

Your model fetch_data should be like this :

function fetch_data() 
{ 
    $this->db->order_by("DocumentID" ,"ASC");
    $this->db->limit(1);  
    $query = $this->db->get('prlaps');
    return $query; 
} 

Update for View with using row() instead of result():

<?php 
if($fetch_data->num_rows() > 0)  
{  
    $row = $fetch_data->row();    
    echo get_abstract($row->id);
}?> 


来源:https://stackoverflow.com/questions/51054631/how-to-view-individual-field-of-the-table-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!