CodeIgniter Forums
query for getting row from table - 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: query for getting row from table (/showthread.php?tid=40351)



query for getting row from table - El Forum - 04-06-2011

[eluser]talwinder[/eluser]
Hi, Currently i am working on a site. I have a table named product in the database. In table there is about 20 rows means 20 products with id's 1-20. From that table i just want to select only 3 rows. E.g I want to select row which have id of 1, 6 and 15. I am juggling with it and unable to get right query. PLEASE HELP!!!


query for getting row from table - El Forum - 04-06-2011

[eluser]toopay[/eluser]
if you are try to get non-grouped result, you can write some function in your model...
Code:
...
     function get_list($table,$field,$args=array())
     {
        $i = 0;
        while ($i<count($args))
        {
            if($i = 0)
            {
                $this->db->where($field, $args[$i]);
            }
            else
            {
                $this->db->or_where($field, $args[$i]);
            }
        }
        $query = $this->db->get($table);
        return $query->result_array();
     }
...

and to use above function, in your case now you can put something like this in your controller ...
Code:
$data['my_list'] = $this->model_name->get_list('table_name','id',array(1,6,15));



query for getting row from table - El Forum - 04-06-2011

[eluser]toopay[/eluser]
forgot 1 line in while statement, here you go...
Code:
...
     function get_list($table,$field,$args=array())
     {
        $i = 0;
        while ($i<count($args))
        {
            if($i = 0)
            {
                $this->db->where($field, $args[$i]);
            }
            else
            {
                $this->db->or_where($field, $args[$i]);
            }
            $i++;
        }
        $query = $this->db->get($table);
        return $query->result_array();
     }
...



query for getting row from table - El Forum - 04-06-2011

[eluser]talwinder[/eluser]
thanks for help. I am quite new to codeigniter. So not much familiar with it. Js wanna know is it gud practive to use model directly into the veiw ????

This the following html for which I want to pull data dynamicly

<div class="abc" id="cde">

<h2></h2>

<h3></h3>
<p></p>
</div>

I have about 20 <div> in a page with unique id but same structure. I want to get <h2>, <h3> and <p> from database. and all 20 products shout display in the page. and how can i echo particular field data in particular div say if i want to echo field which have id 5 and i want to echo that row in <div> 10


query for getting row from table - El Forum - 04-06-2011

[eluser]toopay[/eluser]
[quote author="talwinder" date="1302146159"]thanks for help. I am quite new to codeigniter. So not much familiar with it. [/quote]

Then, you need to read about Controller, Model and View Section in User Guide ...