Welcome Guest, Not a member yet? Register   Sign In
[SOLVED]Select box doubt
#1

[eluser]oldrock[/eluser]
Hi..

i have fetched value from th database and populated it in the select box

with the following code

<?php
echo form_dropdown(‘team’,$teams);

?>

<select name=“team”>
<optgroup label=“0”>
<option value=“team” selected=“selected”>Australia</option>
</optgroup>
<optgroup label=“1”>
<option value=“team” selected=“selected”>Bangladesh</option>
</select>

the thing i want to set the value of option field as below dynamically

<option value=“Bangladesh”>Bangladesh</option>

help me to do that with the form_dropdown. it would be grate full to me

thanks in advance
#2

[eluser]SPeed_FANat1c[/eluser]
you can make a function in your controller which generates the select menu. Something like that:

Code:
function generate_options()
{
  echo options_menu
}

then in your view file you can use jquery and on some button click event write dinamicaly the options meniu code. Here is an example how I generated dropdown menu when #button_add is clicked.

Code:
$(function(){
            var base_url = "&lt;?php echo base_url();?&gt;";
            $('#button_add').live('click',function(){

                
                $('#button_add').hide("fast");
                $('.form').empty();                
                    
                $.post(base_url+"admin_/cities/generate_form_for_add/", function(form){
                    $('.form').append(form);
            
                    $('.error_name').html('<span class="errorsSmall">Laukas "Pavadinimas" turi būti užpildytas</span>');
                    $('.form').show();
                });    
                
                    
                return false;
            })
        });

Here are additional things like error messages, if you don't need them you can erase those lines.
#3

[eluser]oldrock[/eluser]
Hi Thanks for your interest..,

Is it possible without using jquery

thanks in advance
#4

[eluser]oldrock[/eluser]
Hi..,

I m hardly in need of solution for this can anybody help me as soon as possible

thanks in advance
#5

[eluser]cahva[/eluser]
Why are all the values "team" in the first place? Seems to me that the initial array is wrong and should be something like this:
Code:
$teams = array(
    'Australia' => 'Australia',
    'Bangladesh' => 'Bangladesh'
);

Show some code how you now define your $teams array and maybe we can offer some advice.
#6

[eluser]oldrock[/eluser]
controller code:

$data['teams']=$this->addplayermodel->select_teams();
$this->load->view('admin-tool/add_player',$data);

model code:

function select_teams()
{
$query = $this->db->query("SELECT team FROM team");

if($query)
{
while($team=$query->result_array())
{
return $team;
}
}
else
{
return mysql_error();
}
}

view code:

Team
&lt;?php
echo form_dropdown('team',$teams);
?&gt;

Thanks in advance
#7

[eluser]cahva[/eluser]
Thats your problem there. You will have to feed the dropdown an array I showed you in my last post. Instead this is the array you are now sending to dropdown:
Code:
$teams = array(
    'Australia',
    'Bangladesh'
);
It needs key and value. So change your model to this:
Code:
function select_teams()
{
    $tmp = $this->db->get('teams')->result_array();
    
    $teams = array();
    
    if ($tmp)
    {
        foreach ($tmp as $team)
        {
            $teams[$team] = $team;
        }
    }
    
    return $teams;
}
#8

[eluser]oldrock[/eluser]
Hi thanks for your help.., the above code gives the following error

A PHP Error was encountered

Severity: Warning

Message: Illegal offset type

Filename: admin_tool/addplayermodel.php

Line Number: 15

that is in the line $teams[$team] = $team;

thanks in advance
#9

[eluser]cahva[/eluser]
ah.. sorry:
Code:
$teams[$team['team']] = $team['team'];
#10

[eluser]jrtashjian[/eluser]
If you're using PHP 4, modify this single line:
Code:
$tmp = $this->db->get('teams')->result_array();

Into two different lines:
Code:
$tmp = $this->db->get('teams');
$tmp = $tmp->result_array();

Method chaining is only available in PHP5.




Theme © iAndrew 2016 - Forum software by © MyBB