Dropdown list in HTML form |
Hello All
Please could go though the my work and let me know where did I make mistake while preparing drop down list for html form. Model PHP Code: public function set_category_list() Controller PHP Code: public function get_category_list() Quote:<?php foreach($result as $results): ?>
You are returning an object not an array.
PHP Code: /** What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
I see several issues the first of which is that the model could create a much more useful structure.
It appears that the 'cat' table returns two columns - "cat_id" and "cat_name" which represent the "value" and "label" of the <option>s. This method returns either an array with the structure array('cat_id value' =>'cat_name value' , ...) or, if the there are no 'result()'s an empty array is returned. PHP Code: public function set_category_list() Note the use of plural and singular nouns, i.e. $categories and $category. I find this convention helps me understand what I'm working with at any step. In the controller you have a problem with PHP Code: if(sizeof($result) > 0): because $result is not set. What you really mean is PHP Code: if(sizeof($data['result']) > 0): Since your version of the model returns FALSE if no data is found you could simply use PHP Code: if($data['result'] !== FALSE): PHP Code: if($data['result']): You don't show what your controller does if that condition fails. Maybe you left that out on purpose, maybe not. But something useful should happen. But because my model always returns and array there's no need to check the result unless there is an alternate action that should be taken. My "alternate action" is to create an option that says there are no options. That could have been done in the model instead but I'll do it in the controller... just because. Here's the resulting controller method. PHP Code: public function get_category_list() Now the view always has something to use in the select element PHP Code: <?php foreach($results as $value => $label): ?> If you're not familiar with the syntax "<?=" it is the same as writing "<?php echo " only with less typing.
Thank you for the suggestion but still does not work and shows the error message given
Code: A PHP Error was encountered |
Welcome Guest, Not a member yet? Register Sign In |