Welcome Guest, Not a member yet? Register   Sign In
New to php Array question, probably VERY simple...
#1

[eluser]PoetaWD[/eluser]
Sorry for this very infant question.. I am just learning PHP.

I am using the form helper to create a <select> input...

The data is passed to the view file as a object...

But i am having trouble building the array to generate the <select> input !

Here is what I got:

Code:
foreach ($clientes as $cliente)
                                {
                                    $opcoesC[] = array($cliente->id => $cliente->id.' - '.$cliente->stNome);
                                }
echo form_dropdown('cliente', $opcoesC);

But that will generate a array like this:

$opcoesC[0] = 'aaaaaaaaa' => 'aaaaaaaaaaa'
$opcoesC[1] = 'bbbbbbbbb' => 'bbbbbbbbbbb'
...

or like this:

Array ( [0] => Array ( [7] => 7 - jkkk ) [1] => Array ( [8] => 8 - jk ) [2] => Array ( [9] => 9 - kkh ) [3] => Array ( [10] => 10 - klçklçklç ) )

I needed to be generated like this:

$opcoesC = array( 'aaaaaaa' => 'aaaaaaa','bbbbbbb' => 'bbbbbbbb','cccccccc'=>'ccccccc' ....);

I have also tryed this:

Code:
$opcoes = array(
                        '' => 'Selecione o funcionário',
               foreach ($funcionarios as $funcionario)
                                {
           $funcionario->id => $funcionario->id.' - '.$funcionario->stNome,
                     }
                            );

Can anyone give me a hint ?

Thanks in advance...
#2

[eluser]kgill[/eluser]
The reason you're getting:
Code:
$opcoesC[0] = ‘aaaaaaaaa’ => ‘aaaaaaaaaaa’
is this part here:
Code:
$opcoesC[] = array($cli...
when you use the empty brackets like that it automatically increments an integer based index. If you want to use an associative array then you have to specify the index like so:
Code:
$opcoesC[$cliente->id] = $cliente->id.' - '.$cliente->stNome;
#3

[eluser]Phil Sturgeon[/eluser]
Here's what kgill said implemented to your example.

Code:
foreach ($clientes as $cliente)
                                {
                                    $opcoesC[$cliente->id] = $cliente->id.' - '.$cliente->stNome;
                                }
echo form_dropdown('cliente', $opcoesC);
#4

[eluser]PoetaWD[/eluser]
[quote author="Phil Sturgeon" date="1245961469"]Here's what kgill said implemented to your example.

Code:
foreach ($clientes as $cliente)
                                {
                                    $opcoesC[$cliente->id] = $cliente->id.' - '.$cliente->stNome;
                                }
echo form_dropdown('cliente', $opcoesC);
[/quot

Got that !

Thank you very much for the quick help ! It is working now !

Big Grin
#5

[eluser]tashigiri[/eluser]
[quote author="Phil Sturgeon" date="1245961469"]Here's what kgill said implemented to your example.

Code:
foreach ($clientes as $cliente)
                                {
                                    $opcoesC[$cliente->id] = $cliente->id.' - '.$cliente->stNome;
                                }
echo form_dropdown('cliente', $opcoesC);
[/quote]
Thanks Phil and kgill..
my problem solved..^^




Theme © iAndrew 2016 - Forum software by © MyBB