0

I recovered some information from my database and then I turn into the array using:

<?php $clients = explode(",",$data['comptes']) ; ?>

<?php $clients = explode(",",$data['comptes']) ;
    for ( $i = 0; $i < count( $clients ); $i++ ) { 
 echo '<option value="'.trim($clients[$i]).',">'.trim($clients[$i]).'</option>'; 
} ?>

I would then display all in a selection list.

Before the whole table is not empty, it displays a print_r():

Array (
    [0] => 1566 
    [1] => 1599 
    [2] =>
)

Then for the selection list so I'm doing following code:

but yet it does not return me the last selection list is empty, I do not see where I made ​​my mistake because I have no error messages.

5
  • 2
    Your question seems to be a bit all over the place. Have you remembered a starting <select> and finishing </select>. And did you say you'd checked that clients had things in> Commented Aug 23, 2012 at 15:14
  • Why do you call $clients = explode(",",$data['comptes']) ; twice? Commented Aug 23, 2012 at 15:16
  • 1
    You shouldn't use count($clients) in your for loop definition. You're essentially calling that function each time you iterate. Instead, set its value to a variable, and use the variable. Commented Aug 23, 2012 at 15:17
  • It's very likely @PippaRoseSmith is right. Most browsers don't render orphan option elements Commented Aug 23, 2012 at 15:18
  • I did not called twice someone of staff edi my post. first I show how I get datas. Commented Aug 23, 2012 at 15:26

2 Answers 2

1
<?php 
    $clients = explode(",",$data['comptes']) ;

    echo '<select id="selectboxname" name="selectboxname">';

    for ( $i = 0; $i < count( $clients ) - 1; $i++ ) 
    { 
        echo '<option value="'.trim($clients[$i]).',">'.trim($clients[$i]).'</option>'; 
    } 

    echo '</select>';
?>

Make sure you have

<select> </select> 

round your options.

Sign up to request clarification or add additional context in comments.

12 Comments

this code display to me that <select><option value=","></option></select>
yes I have select /select in fact the entirely code I use is the following: <select name="selection[]" size="15" id="selection" multiple class="multiple" OnDblClick="javascript:selection_champs(this.form.selection,this.form.liste_champs);listbox_selectall('selection', true);"><?php $clients = explode(",", trim($data['comptes'], ',')); for ( $i = 0; $i < count( $clients ); $i++ ) { echo '<option value="'.trim($clients[$i]).',">'.trim($clients[$i]).'</option>'; } ?> </select>
Array position 2 doesn't seem to have a value in it. Should it have? And can you post what $data['comptes'] is? If you have put a , at the end of the string, it might be creating a final array position
it is because of the extra coma, I've change this now there is no more second position in aray for rhis code
Excellent, glad you sorted it :)
|
1

I am really guessing, but you probably have an extra comma in $data['comptes']. Try

$clients = explode(",", trim($data['comptes'], ','));

1 Comment

I've changed the code for this it still does not work I have no error messages

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.