CodeIgniter Forums
advanced database query :p - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: advanced database query :p (/showthread.php?tid=28981)



advanced database query :p - El Forum - 03-26-2010

[eluser]johansonevR[/eluser]
hello,
i am new to this forum as i am new to CI. However i am looking at the blog tutorial.
well the problem is here i don't know how to explain it exactly but i will give it a try.
i have a function:
user clicks on a link and gets the data from view().
Code:
function view(){
        $data['contents']=$this->db->get('adv');
        $this->load->view("adv_view_view",$data);
    }
the problem is that this function does not know which table row to select, it selects them all. How to select the id of the table i want to use dynamically, i want my db query to become
Code:
$this->db->query("select * from adv where id='$id';");
i want to get the the variable $id from the previous page and use it in the query.


advanced database query :p - El Forum - 03-26-2010

[eluser]theprodigy[/eluser]
Quote:$this->db->query("select * from adv where id='$id';");
What you are looking for is:
Code:
$this->db->where('id',$id);
$this->db->get('adv');
You can read all about CodeIgniter's Active Record implementation here


advanced database query :p - El Forum - 03-26-2010

[eluser]johansonevR[/eluser]
tnx for the reply , however i still don't understand how to get the id from this :
Code:
anchor("adv/view/".$row->id, "View");



advanced database query :p - El Forum - 03-26-2010

[eluser]theprodigy[/eluser]
Quote:function view(){
$data['contents']=$this->db->get('adv');
$this->load->view("adv_view_view",$data);
}
Quote:anchor("adv/view/".$row->id, "View");

I'm going to assume that this function is in a Controller called adv.

What you need to do is give your view method a parameter, and use that parameter.
Code:
function view($id){
    $this->db->where('id',$id);
    $data['contents']=$this->db->get('adv');
    $this->load->view("adv_view_view",$data);
}



advanced database query :p - El Forum - 03-27-2010

[eluser]johansonevR[/eluser]
tnx