Welcome Guest, Not a member yet? Register   Sign In
Dropdown list in HTML form
#1

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()
    {
    
        
$query $this->db->get('cat');
 
         // return $query->result();

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

    } 

Controller

PHP Code:
    public function get_category_list()
    {

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

        if(
sizeof($result) > 0):

            
$this->load->view('insert',$data);

        endif;

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

<?php endforeach; ?>
Reply
#2

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 )
Reply
#3

(This post was last modified: 01-20-2018, 08:35 AM by dave friend.)

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;
    }

    return 
$data;


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

    if(empty(
$data['results']))
    {
        
$data['results'] = [=> "No Categories Available"];
    }
    
$this->load->view('insert'$data);


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.
Reply
#4

Thank you for the suggestion but still does not work and shows the error message given
Code:
A PHP Error was encountered

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


Backtrace:

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




Theme © iAndrew 2016 - Forum software by © MyBB