0

I have created following table in mysql

enter image description here

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.

Pradeep
  • 9,667
  • 13
  • 27
  • 34
Kumar Ajeet
  • 72
  • 12

1 Answers1

0

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);
}?> 
Pradeep
  • 9,667
  • 13
  • 27
  • 34
  • A PHP Error was encountered Severity: Notice Message: Trying to get property 'id' of non-object – Kumar Ajeet Jun 27 '18 at 05:49
  • Error Number: 1066 Not unique table/alias: 'abcde' SELECT * FROM `abcde`, `abcde` WHERE `id` IS NULL Filename: helpers/custom_helper.php Line Number: 6 – Kumar Ajeet Jun 27 '18 at 05:50
  • show your controller , how you r retrieving all data for the view pls show . the error is just because may be the id column is incorrect show me the correct field name for id – Pradeep Jun 27 '18 at 05:54
  • 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); } – Kumar Ajeet Jun 27 '18 at 05:55
  • show me model method `fetch_data` from this `$this->Sample_model->fetch_data();` – Pradeep Jun 27 '18 at 06:00
  • function fetch_data() { $this->db->select("*"); $this->db->from("prlaps"); $query = $this->db->query("SELECT * FROM prlaps ORDER BY DocumentID ASC"); return $query; } – Kumar Ajeet Jun 27 '18 at 06:02
  • prlaps is table name----but in question i changed it as abcde, id as DocumentID – Kumar Ajeet Jun 27 '18 at 06:03
  • is your DocumentID is the correct id if not pls replace it with your correct id – Pradeep Jun 27 '18 at 06:05
  • i have updated my answer for controller and model pls check model and controller – Pradeep Jun 27 '18 at 06:06
  • it is giving all the abstract of the table.How to display only one in view by changing id in view – Kumar Ajeet Jun 27 '18 at 06:14
  • In view -id=3);?> is giving correct abstract but 7 times i.e. number of rows in the table.How to stop this loop so that i can get only one time – Kumar Ajeet Jun 27 '18 at 06:21
  • ok just add limit to 1 to your model `fetch_data` like this `$this->db->limit(1);` see my updated answer, or better return `row()` instead or `result()` in view – Pradeep Jun 27 '18 at 06:30
  • it is given you 7 times just because you have total number of records in `prlaps` table is 7 – Pradeep Jun 27 '18 at 06:33
  • Your first answer has solved my problem. But row() option is again giving error A PHP Error was encountered Severity: Notice Message: Trying to get property 'DocumentID' of non-object – Kumar Ajeet Jun 27 '18 at 06:39
  • this is because if you use `row()` no need of `foreach` loop there – Pradeep Jun 27 '18 at 06:40
  • can you write the exact code using row() also.This will be useful – Kumar Ajeet Jun 27 '18 at 06:42
  • see my updated answer in the last, by the way if my answer helps you pls do upvote and accept my answer – Pradeep Jun 27 '18 at 06:53