Welcome Guest, Not a member yet? Register   Sign In
drop down menus
#1

[eluser]babu[/eluser]
hello experts,
i need help regarding drop down menus.In my dropdownmenu,options values should get from the database.

example for static dropdownmenus.

<tr>
<TD align="right" width="120" class="forrowcolr">
&lt;?php echo form_label('Category : ');?&gt;
</TD>
<TD align="left" class="forrowcolr">&lt;?php $options = array('Books'=>'Books','Calligraphy'=>'Calligraphy','Miniatures'=>'Miniatures','Prints'=>'Prints','Watercolours'=>'Watercolours','Photographs'=>'Photographs');?&gt;
&lt;?php echo form_dropdown('category', $options);?&gt;</TD>

</tr>

here the values are static.what i need is,options values should get from the database.if i change anything from the database.options values should get updated.very urgent pls..thanks in advance
#2

[eluser]mdowns[/eluser]
You can prepare the options using a model (calling to the database). Then pass them into your view.

In the controller:
Code:
$options = $this->mymodel->getMyOptions();
$data['options'] = $options;
$this->load->view('myview',$data);
#3

[eluser]babu[/eluser]
hello sir,
first of all, thanks for ur reply.As per ur suggestions.i implement the things in controller and models.but i dont know how to implement in views.

In controller,

$options = $this->inventory_model->getMyOptions();
$data['options'] = $options;
$this->load->view('inventory_view',$data);

In models,

function getMyOptions()
{
$this->db->select('sizedefinitions');
$query=$this->db->get('definitions');
return $query->results();
}
In views,

<td align="right" width="120" class="forrowcolr">
&lt;?php echo form_label('Size : ');?&gt;
</td>
<td align="left" class="forrowcolr">
&lt;?php $i=0;?&gt;
&lt;?php foreach ($results as $item):?&gt;
&lt;?php $i++;?&gt;
&lt;?php $options = array($item->sizedefinitions[$i]=>$item->sizedefinitions[$i]);?&gt;
&lt;?php echo form_dropdown('category', $options);?&gt;
&lt;?php endforeach; ?&gt;
</td>



I am getting error.Is this code is correct or i am doing anything wrong here.could u please tell me the view part.give one example for it
#4

[eluser]sl3dg3hamm3r[/eluser]
When you pass $data to your view, you defined 'options'. Therefore, replace $results with $options in your view. And rename the already existing variable $options (which you fill with data for dropdown) to something else, in order to avoid name-conflict.
#5

[eluser]babu[/eluser]
hello sir,
i made changes to views part.but i am getting error like this

A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/inventory_view.php

Line Number: 129

my code

in controller

$options = $this->inventory_model->getMyOptions();
$data[‘options’] = $options;
$this->load->view(‘inventory_view’,$data);

in models

function getMyOptions()
{
$this->db->select('sizedefinitions');
$query=$this->db->get('definitions');
return $query->result();
}

in views

<td align="right" width="120" class="forrowcolr">
&lt;?php echo form_label('Size : ');?&gt;</td>
<td align="left" class="forrowcolr">
&lt;?php $i=0;?&gt;
&lt;?php foreach ($options as $item):?&gt;
&lt;?php $i++;?&gt;
&lt;?php $sel = array($item->sizedefinitions[$i]=>$item->sizedefinitions[$i]);?&gt;
&lt;?php echo form_dropdown('size', $sel);?&gt;
&lt;?php endforeach; ?&gt;
</td>

now this code is correct or i am doing anything wrong over here.thanks in advance.
#6

[eluser]sl3dg3hamm3r[/eluser]
what line would that be?

the $i - counter is not needed, it should be like that:

Code:
$sel = array($item->sizedefinitions => $item->sizedefinitions);

furthermore, there is absolutely no need to use the php open/close-tag on each new line.
#7

[eluser]mdowns[/eluser]
You can also prepare the options array in the model itself.

Code:
function getMyOptions()
{
  $this->db->select('sizedefinitions');
  $query=$this->db->get('definitions');
  $result = $query->result();
  $options = array();
  foreach($result as $item){
    $options[$result->sizedefinitions] = $result->sizedefinitions;
  }
  return $options;
}

Then when you pass it into the view all you have to do is:

Code:
<td align="right" width="120" class="forrowcolr">
    &lt;?php echo form_label('Size : ');?&gt;
  </td>
  <td align="left" class="forrowcolr">
    &lt;?php echo form_dropdown('size', $options);?&gt;
  </td>

This will make your views cleaner.

P.S. When posting code or markup, please use the code tag [ code ] (without spaces)




Theme © iAndrew 2016 - Forum software by © MyBB