Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is a good approach in dealing with a set of select statements but different conditions?
    primarykey
    data
    text
    <p>One problem that I am facing is having many queries with similar select statements but different join/where statements. </p> <p>Below is an example of a code that I am working on via CodeIgniter. What I normally do is make one function, get(), that accepts an array of random keys/values. Depending on what keys/values are passed, it will generate and run the appropriate query. Now, I am wondering is this a good way of doing things? Because as you can see, this function becomes more and more complex. Initially, I had a bunch of functions such as get_all(), get_only_lessons(), etc but it becomes kinda annoying having to repeat the same set of code with one or two lines that are different.</p> <p>My question is what is the best way with dealing with this problem.</p> <pre><code>function get($param = NULL) { /* SELECT m.id AS id, CAST(m.order_number AS SIGNED) AS order_number, m.name AS name, m.permalink as permalink, m.suplesson_id as suplesson_id, CAST(sm.order_number AS SIGNED) AS suplesson_order_number FROM lessons m JOIN courses c ON m.course_id = c.id LEFT JOIN lessons sm ON m.suplesson_id = sm.id WHERE [various] */ $select = 'm.id AS id, CAST(m.order_number AS SIGNED) AS order_number, m.name AS name, m.permalink as permalink, '; $select .= ' m.suplesson_id as suplesson_id'; if (isset($param['id']) || isset($param['suplesson_order_number']) || isset($param['permalink'])) $select .= ', CAST(sm.order_number AS SIGNED) AS suplesson_order_number '; $this-&gt;db-&gt;select($select); $this-&gt;db-&gt;from($this-&gt;table_name.' m'); $this-&gt;db-&gt;join($this-&gt;courses_table_name.' c', 'm.course_id = c.id'); if (isset($param['id']) || isset($param['suplesson_order_number']) || isset($param['permalink'])) $this-&gt;db-&gt;join($this-&gt;table_name.' sm', 'm.suplesson_id = sm.id', 'left'); // where clauses if (isset($param['course_id'])) $this-&gt;db-&gt;where(array('c.id' =&gt; $param['course_id'])); if (isset($param['id'])) $this-&gt;db-&gt;where(array('m.id' =&gt; $param['id'])); if (isset($param['order_number'])) $this-&gt;db-&gt;where(array('m.order_number' =&gt; $param['order_number'])); if (isset($param['permalink'])) $this-&gt;db-&gt;like('m.permalink', $param['permalink'], 'none'); if (isset($param['suplesson_id'])) $this-&gt;db-&gt;where(array('m.suplesson_id' =&gt; $param['suplesson_id'])); if (isset($param['suplesson_order_number'])) $this-&gt;db-&gt;where(array('sm.order_number' =&gt; $param['suplesson_order_number'])); if (isset($param['NULL'])) $this-&gt;db-&gt;where('m.'.$param['NULL'].' IS NULL'); if (isset($param['NOT NULL'])) $this-&gt;db-&gt;where('m.'.$param['NOT NULL'].' IS NOT NULL'); $this-&gt;db-&gt;order_by('order_number'); // filter based on num_rows/offset if (isset($param['id']) || isset($param['permalink'])) $this-&gt;db-&gt;limit(1); if (isset($param['num_rows']) &amp;&amp; isset($param['offset'])) $this-&gt;db-&gt;limit($param['num_rows'], $param['offset']); $query = $this-&gt;db-&gt;get(); // return row if expecting 1 result if (isset($param['id']) || isset($param['suplesson_order_number']) || isset($param['permalink'])) return ($query-&gt;num_rows() == 1) ? $query-&gt;row_array() : NULL; return ($query-&gt;num_rows() &gt; 0) ? $query-&gt;result_array() : NULL; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload