CodeIgniter Forums
linking to pages using Id's....New to CodeIgniter. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: linking to pages using Id's....New to CodeIgniter. (/showthread.php?tid=52052)



linking to pages using Id's....New to CodeIgniter. - El Forum - 05-28-2012

[eluser]reghan[/eluser]
Hello,

I am new to code igniter. I am trying to create links for a bunch of different departments. When the user clicks on a link I would like that departments info to be produced in a view. So far I am just getting errors.

here is my code. Any advice would be greatly appreciated!

Controller:
Code:
<?php

class Bia extends CI_Controller

{
function Bia()
{
  parent::__construct();
  $this->load->model('department_model');

  $this->load->helper('form');
  $this->load->helper('url');
  $this->load->helper('form_helper');
  $this->load->library('form_validation');
  $this->load->library('table');
}



function index()
{



  $this->db->order_by('DepartmentName', 'asc');
  $query['depart_list'] = $this->db->get('department');





  $this->load->view('bia_home', $query);
}

function get_depart_info() //gets the department info based on the Id number.
{
  $id = $this->uri->segment(3);
  $this->db->where('DepartmentId', $id);
  $query['dept_info'];

  $this->load->view('dept_info', $query);

}

}


?>


view # 1:

Code:
<table border="1">

   <tr>

     <th>Department</th>
<th>Responder</th>
     <th>Last Updated</th>


  </tr>



&lt;?php foreach($depart_list->result() as $row): ?&gt;


   &lt;?php echo "<tr>"; ?&gt;

    &lt;?php echo "<td>". anchor('bia/get_dept_info/'.$row->DepartmentId, $row->DepartmentName, 'id="'.$row-&gt;DepartmentId.'"') ."</td>";  ?&gt;

     &lt;?php echo "<td>". $row->Responder ."</td>";  ?&gt;

     &lt;?php echo "<td>". $row->DateCompleted ."</td>";  ?&gt;



  &lt;?php echo "</tr>";    ?&gt;



  &lt;?php endforeach; ?&gt;

</table>

view #2

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
<h3>Business Impact Analysis(BIA) Questionnaire</h3>

  <p>The Business Impact Analysis (BIA) is a process
  to determine the mission critical business functions and
  associated critical resources. This will be accomplished for
  Manitoba Lotteries by completing the following six components:
  </p>

  &lt;?php if($depart_list->num_rows() == 0): ?&gt;

  <p>There are no Departments to List</p>

  &lt;?php else: ?&gt;




<table border="1">

   <tr>

     <th>Department</th>
<th>Responder</th>
     <th>Last Updated</th>


  </tr>



&lt;?php foreach($depart_info->result() as $row): ?&gt;


   &lt;?php echo "<tr>"; ?&gt;



     &lt;?php echo "<td>". $row->Responder ."</td>";  ?&gt;

     &lt;?php echo "<td>". $row->DateCompleted ."</td>";  ?&gt;



  &lt;?php echo "</tr>";    ?&gt;



  &lt;?php endforeach; ?&gt;

</table>







  &lt;?php endif; ?&gt;






&lt;/body&gt;
&lt;/html&gt;

So basically view #1 lists all the departments and some info. when the link is clicked I would like to store the department Id and produce View #2 which only shows department info based on the Id of the link clicked. Again I am super new to codeignitor and I know this should be easy, but I just dont understand!

Thanks for the help!


linking to pages using Id's....New to CodeIgniter. - El Forum - 05-28-2012

[eluser]InsiteFX[/eluser]
Create a new function in your controller called show_dept_info and pass the dept_id into that.

Code:
function show_depart_info() //gets the department info based on the Id number.
{
  $id = $this->uri->segment(3);
  $this->db->where('DepartmentId', $id);
  $query['dept_info'];

  $this->load->view('dept_info', $query);

}

}

I would start right now but moving all your database queries to your database model, you should not
be calling the database in your controller.

Controller calls database model methods, controller then sends the database results to the view for processing.



linking to pages using Id's....New to CodeIgniter. - El Forum - 05-28-2012

[eluser]reghan[/eluser]
I did do that and it gives me a 404 page not found...So it is not locating the page. I am not sure what else to try.


linking to pages using Id's....New to CodeIgniter. - El Forum - 05-28-2012

[eluser]InsiteFX[/eluser]
Give this a try:
Code:
&lt;?php echo "<td>" . anchor(site_url('bia/get_dept_info/').$row->DepartmentId, $row->DepartmentName, array('id=" =&gt; $row-&gt;DepartmentId) . "</td>"; ?&gt;

You can also try base_url