• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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.

PHP Code:
    public function set_category_list()
$query $this->db->get('cat');
         // return $query->result();

         if ($query->num_rows() > 0) {
           foreach ($query->result() as $row) {
                 $data[] = $row;
           return $data;
       return false;



PHP Code:
    public function get_category_list()

$data['result'] = $this->Samakalin_Model->set_category_list();

sizeof($result) > 0):



Quote:<?php foreach($result as $results): ?>
    <option value="<?php echo $results['cat_id']; ?>">
        <?php echo $results['cat_name']; ?></option>

<?php endforeach; ?>

You are returning an object not an array.

PHP Code:
 * -----------------------------------------------------------------------
 * Return Object's:
 * result() = $row->title;
 * row()    = $row->title;
 * Return Array's:
 * result_array() = $row['title'];
 * row_array()    = $row['title'];
 * -----------------------------------------------------------------------
<?php foreach($result as $results): ?>
    <option value="<?php echo $results->cat_id?>">
        <?php echo $results->cat_name?></option>

<?php endforeach; ?>
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()
$categories $this->db->get('cat')->result();
$data = [];  //insure $data is set so it can be returned

foreach($categories as $category)
$data[$category->cat_id] = $category->cat_name;


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): 
or even more concise
PHP Code:

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()
$data['results'] = $this->Samakalin_Model->set_category_list();

$data['results'] = [=> "No Categories Available"];

Now the view always has something to use in the select element
PHP Code:
<?php foreach($results as $value => $label): ?>
 <option value="<?= $value?>"><?= $label?></option>
<?php endforeach; ?>

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
A PHP Error was encountered

Severity: Notice
Message:  Undefined variable: results
Filename: views/insert.php
Line Number: 15


File: /var/www/html/sahitya/application/views/insert.php
Line: 15<br />
Function: _error_handler

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.