Welcome Guest, Not a member yet? Register   Sign In
Codeigniter getting row details through link (foreach)
#1

[eluser]Unknown[/eluser]
Hi everyone, I've been doing this for almost a month but I cannot figure it out on how to get the "details" of every row in my table. My table in my view is foreach. And post it in another page. By clicking the link named "Details..." on first page.

for example: ive searched in first page and this is the result:


----------
column1 column1 column3 column4 column5 column6

name desc age num add Details...

name2 desc2 age2 num2 add Details...


When i click the first details... it would go to second page and post name, desc, age, num, add into textbox. same as the second details...
Any help would be greatly appreciated. Thanks.

here are my codes:

my model model.php - this is my code for search data in my database table

Code:
public function search_equip() {
  
  if ($this->input->post('category') == "All")
  {
  $match = $this->input->post('search');
  $array = array('column1' => $match);
  $this->db->like($array);
   $query = $this->db->get('equip');
   return $query->result();
  }
  elseif ($this->input->post('category') == "Appliance")
  {
  $match = $this->input->post('search');
  $array = array('column1' => $match, 'category' => "Appliance");
  $this->db->like($array);
   $query = $this->db->get('equip');
   return $query->result();
  }
  elseif ($this->input->post('category') == "Furniture")
  {
  $match = $this->input->post('search');
  $array = array('column1' => $match, 'category' => "Furniture");
  $this->db->like($array);
   $query = $this->db->get('equip');
   return $query->result();
  }
  elseif ($this->input->post('category') == "Equipment")
  {
  $match = $this->input->post('search');
  $array = array('column1' => $match, 'category' => "Equipment");
  $this->db->like($array);
   $query = $this->db->get('equip');
   return $query->result();
  }
}


----------


my controller search.php - my code with set rules

Code:
function search_equipment()
{

  //If searchbox is empty    
  $this->load->helper(array('form', 'url'));

  $this->load->library('form_validation');

  $this->form_validation->set_rules('search', 'Search', 'required|xss_clean');
  
  if ($this->form_validation->run() == FALSE)
   {
   $this->load->view('auth/index', 'refresh');
   }
  else
  {
    $data['query'] = $this->model->search_equip();
  $this->load->view('auth/search_view_equipment', $data);
  }
}

----------

my view search_view_equipment.php - this is where i search data and show results with the link every row

Code:
<?php

echo "<table border=1 cellpadding=5>";
echo "<tr>";
echo "<td><b>Column1</b></td>";
echo "<td><b>Column2</b></td>";
echo "<td><b>Column3</b></td>";
echo "<td><b>Column4</b></td>";
echo "<td><b>Column5</b></td>";
echo "<td><b>Column6</b></td>";
echo "</tr>";

foreach($query as $item){

echo "<tr>";
echo "<td>".&lt;?= $item->Column1 ?&gt;."</td>";
echo "<td>".&lt;?= $item->Column2 ?&gt;;."</td>";
echo "<td>".&lt;?= $item->Column3 ?&gt;."</td>";
echo "<td>".&lt;?= $item->Column4 ?&gt;;."</td>";
echo "<td>".&lt;?= $item->Column5 ?&gt;;."</td>";
echo "<td>".anchor("auth/view_equipment_details/".$item->Column6, 'Details...', array('class' => 'detail'))."</td>";
echo "</tr>";
}
echo "</table>";
?&gt;



----------

my second view view_equipment.php - this is where to post the data coming from first page (specific row)
Note: This td's are input, i dunno how to make a code here using input Smile
Code:
&lt;?php

foreach($query as $item){

echo "<tr>";
echo "<td>".&lt;?= $item->Column1 ?&gt;."</td>";
echo "<td>".&lt;?= $item->Column2 ?&gt;;."</td>";
echo "<td>".&lt;?= $item->Column3 ?&gt;."</td>";
echo "<td>".&lt;?= $item->Column4 ?&gt;;."</td>";
echo "<td>".&lt;?= $item->Column5 ?&gt;;."</td>";
echo "<td>".anchor("auth/view_equipment_details/".$item->Column6, 'Details...', array('class' => 'detail'))."</td>";
echo "</tr>";
}
echo "</table>";
?&gt;


----------
Is it possible to get that whole data of that row and show it in another page? Specifically, I'm confused on what am I going to do, because the table is in foreach. I cannot get the value of the specific link in first page, here's my code on getting the value of links in first page but im not succesful:

controller: auth.php


Code:
function view_equipment_details()
    {
   $data['equipid'] = $this->model->get_equipment_id();
  $this->load->view('auth/view_equipment', $data);
}


model: addition in model.php

Code:
public function get_equipment_id() {
  
  $match = $this->input->post(); //This line is where i want to put the specific links ( any links that i click in first page) VALUE or NAME or whatsoever that is EXACTLY THE SAME AS THE ID OF THAT ROW I WILL CLICK to search using (next line): all i need is the value equal to the id of a row in table in database.
  $array = array('id' => $match);
  $this->db->where($array);
   $equipid = $this->db->get('equip');
   return $equipid->result();
    
}



Thank you in advance for any help. Hoping for a feed back. Smile
#2

[eluser]jvicab[/eluser]
I think that what you need is to pass the id of the row (equipment_id) to the called function. Your controller view_equipment_details will expect one parameter: equipment_id and its header would look like:
function view_equipment_details($equipment_id)
That value will be passed to model function as well:
public function get_equipment_id($equipment_id)
and will be used to build the where clause: $array = array('id' => $equipment_id);
For this to work, you should add this value to the href attribute for the detail link in your view so the controller function will receive it:
echo "<td>".anchor("auth/view_equipment_details/".$item->id, 'Details...', array('class' => 'detail'))."</td>";


#3

[eluser]Unknown[/eluser]
@jvicab thank you for reading my question and my post even though it's very long. Thumbs up for your answer, it works!! Thank you very much! One more thing, is it ok that my view_equipment.php would be &lt;input&gt;? Not in <td>? And is it ok that is it in foreach? Or is there a better way to show that detail?




Theme © iAndrew 2016 - Forum software by © MyBB