Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is how I've done it, (very easy to do)</p> <ol> <li>download jQuery, + <a href="http://www.appelsiini.net/projects/chained" rel="nofollow">jquery.chained.js</a></li> <li>insert jQuery + jquery.chained.js to the CI view(s)</li> <li>create function for generating selects from database</li> <li>calling the function</li> </ol> <p>as far my structure of tables is as following (I know it can be easily done in one table)</p> <p><strong>category</strong></p> <pre><code>CREATE TABLE IF NOT EXISTS `category` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(64) NOT NULL, `active` tinyint(1) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ; INSERT INTO `category` (`id`, `name`, `active`) VALUES (3, 'Science', 1), (4, 'History', 1); </code></pre> <p><strong>subcategory</strong></p> <pre><code>CREATE TABLE IF NOT EXISTS `subcategory` ( `id` int(11) NOT NULL AUTO_INCREMENT, `category_id` int(11) NOT NULL, `name` varchar(64) NOT NULL, `active` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `category_id` (`category_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ; INSERT INTO `subcategory` (`id`, `category_id`, `name`, `active`) VALUES (2, 3, 'Mathematics', 1), (3, 3, 'Physics', 1), (4, 3, 'Medicine', 1), (5, 4, '21st Century', 1), (6, 4, '18-20th Century', 1), (7, 4, '15-17th Century', 1), (8, 4, 'Before 15th Century', 1); ALTER TABLE `subcategory` ADD CONSTRAINT `subcategory_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`); </code></pre> <p>Now I just load all the <em>active</em> categories/subcategories and create <code>&lt;select&gt;</code>s for both of them. </p> <p>Note that I am using twitter-bootstrap 2 so there is some extra <code>HTML</code>.</p> <p>Note that I am extending <strong>CI_Controller</strong> with my own <strong>MY_Controller</strong> file therefore I can set "global" <code>$data[]</code> (that is passed to <strong>view</strong>) by doing <code>$this-&gt;data['key']</code> to exted CI_Controller follow this <a href="http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY" rel="nofollow">tutorial</a>.</p> <p>Note that it has built in "repopulate feature" so whenever you pass (valid or invalid) <code>category_id &amp;&amp; || $subcategory_id</code> it looks in the DB if parameters are correct and cateogry/subcategory exists if so it repopulates itself.</p> <pre><code>public function category_chain($category_id = FALSE, $subcategory_id = FALSE) { $this-&gt;load-&gt;model('general_model'); $repopulate['category'] = ''; $repopulate['subcategory'] = ''; if (($category_id !== FALSE &amp;&amp; $subcategory_id !== FALSE) || ($category_id !== "FALSE" &amp;&amp; $subcategory_id !== "FALSE")) { if ($this-&gt;general_model-&gt;_isInDBWhere('subcategory', array('id' =&gt; $subcategory_id, 'category_id' =&gt; $category_id))) { $repopulate['category'] = $category_id; $repopulate['subcategory'] = $subcategory_id; } } if (($category_id !== FALSE &amp;&amp; $subcategory_id === FALSE) || ($category_id !== "FALSE" &amp;&amp; $subcategory_id === "FALSE")) { if ($this-&gt;general_model-&gt;_isInDB('category', 'id', $category_id)) { $repopulate['category'] = $category_id; $repopulate['subcategory'] = ''; } } $categories = $this-&gt;general_model-&gt;_getAllWhere('category', 'active', '1'); $subcategories = $this-&gt;general_model-&gt;_getAllWhere('subcategory', 'active', '1'); $return = "&lt;div class=\"control-group\"&gt; &lt;label class=\"control-label\"&gt;.category &lt;/label&gt; &lt;div class=\"controls\"&gt; &lt;div class=\"input-prepend\"&gt; &lt;span class=\"add-on\"&gt;&lt;i class=\"icon-th-large\"&gt;&lt;/i&gt;&lt;/span&gt;"; $return .= "&lt;select name=\"category_id\" id=\"category\"&gt;"; $return .= "&lt;option value=\"\"&gt;--&lt;/option&gt;"; foreach ($categories as $category) { $return .= "&lt;option value=\"".$category-&gt;id."\"".(($repopulate['category'] == $category-&gt;id) ? " selected": "")."&gt;".$category-&gt;name."&lt;/option&gt;"; } $return .= "&lt;/select&gt;"; $return .= "&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;"; $return .= "&lt;div class=\"control-group\"&gt; &lt;label class=\"control-label\"&gt;.subcategory &lt;/label&gt; &lt;div class=\"controls\"&gt; &lt;div class=\"input-prepend\"&gt; &lt;span class=\"add-on\"&gt;&lt;i class=\"icon-th\"&gt;&lt;/i&gt;&lt;/span&gt;"; $return .= "&lt;select name=\"subcategory_id\" id=\"subcategory\"&gt;"; $return .= "&lt;option value=\"\"&gt;--&lt;/option&gt;"; foreach ($subcategories as $subcategory) { $return .= "&lt;option value=\"".$subcategory-&gt;id."\" class=\"".$subcategory-&gt;category_id."\"".(($repopulate['subcategory'] == $subcategory-&gt;id) ? " selected": "")."&gt;".$subcategory-&gt;name."&lt;/option&gt;"; } $return .= "&lt;/select&gt;"; $return .= "&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;"; $this-&gt;data['category_chain'] = $return; } </code></pre> <p>lastly create <code>application/models/general_model.php</code> and create this function</p> <pre><code>function _getAllWhere($table, $where, $value) { $q = $this-&gt;db-&gt;get_where($table, array($where =&gt; $value)); return ($q-&gt;num_rows() &gt; 0) ? $q-&gt;result() : FALSE; } </code></pre> <p>Lasts step is to call the function in a way that it is called just when we need it. In desired controller just call <code>$this-&gt;category_chain()</code> and in view there will be a variable avalible <code>$category_chain</code> (just echo it out like <code>&lt;?=$category_chain?&gt;</code>)</p> <p>Thats it ;)</p>
    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.
    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