0

I want to use my db values of table (Category) into a product/add form, but not able to get the values.

Model :
 function getAllCategories()
    {
        $query = $this->db->get('category');
        return $query->result_array();
    }

 Controller:

function getAllCategories()
   {
      $data['groups'] = $this->productmodel->getAllCategories();

       $this->load->view('admin/product/add',$data);
   }   

  View:

<div class="form-group">
                  <label for="exampleInputEmail1">Brand </label>
                  <select class="form-control">
                <?php 
                $i = 0;
                while($i < count($groups)){
                  $val= $groups[$i]['value'];
                  $des = $groups[$i]['category_name'];
                  echo "<option value='$i'>$des</option>";
                }?>
               </select>
 </div>

Help me please it is urgent.I took reference from stack overflow only, but was not able to do it.

3 Answers 3

1

Use foreach loop for array data type

<select class="form-control">
<?php 
foreach($groups as $group)
{
   $val= $group['value']; 
   $des = $group['category_name'];
   echo "<option value='$val'>$des</option>";
} ?>
</select>
Sign up to request clarification or add additional context in comments.

2 Comments

The <option> value is coming blank. The values from db are not getting print.
Print the $groups variable using print_r($groups); , you will get clear data.
0

Himanshu, Simply replace your code with this one in your view. The thing is that you are not incrementing the value of $i

<div class="form-group">
    <label for="exampleInputEmail1">Brand </label>
    <select class="form-control">
        <?php
        $i = 0;
        while ($i < count($groups)) {
            $val = $groups[$i]['value'];
            $des = $groups[$i]['category_name'];
            echo "<option value='".$i."'>$des</option>";
            $i++;
        } ?>
    </select>
</div>

Comments

0

No panic it's should be easy. Do you get some data from model in controller ?
First, var_dump this line $this->productmodel->getAllCategories();
Second, I think you should do $i++ in your while cycle

Like this

<?php 
$i = 0;  
while($i < count($groups)){ 
  $val= $groups[$i]['value']; 
  $des = $groups[$i]['category_name']; 
  echo "<option value='$i'>$des</option>"; 
  $i++; 
}?> 

1 Comment

Okay will check it sir.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.