Page 1 of 1

[SOLVED]Populating a dynamic list box

Posted: Mon Mar 06, 2006 1:34 pm
by rubberjohn
Hi,

Can anyone help me with this?

I am trying to populate a list box with values from a database but the only output that ends up in the list box is the last element of the array.

I can see what is happening - the assignment of $tag_list just gets overwritten with each iteration of the loop instead of being appended to the end of it.

Is there a better way of doing this (I'm sure there is)?

Code: Select all

$tags = array($fetch_tag['tag']);
		
$tag_list = '<select name="tag_list" size="10">';
	
        for($x=0; $x<=$tag_count; $x++){
	
	           $tag_list .= ' <option value="">' . $tags[$x] . '</option>';
	
	}
	
$tag_list .= '</select>';
Thanks in advance.

rj

Posted: Mon Mar 06, 2006 2:05 pm
by neophyte
Where is $tag_count;

This is working for me:

Code: Select all

$tags = array('one', 'two', 'thre', 'four');
$tag_count = count($tags);
$tag_list = '<select name="tag_list" size="10">';

        for($x=0; $x<=$tag_count; $x++){

              $tag_list .= ' <option value="">' . $tags[$x] . '</option>';

   }

$tag_list .= '</select>';
echo $tag_list;

Posted: Mon Mar 06, 2006 2:12 pm
by rubberjohn
i am using nested while sql and while statements.

The count is declared in the first while statement but called in the second, is this the problem?

Here is some more of the code:

Code: Select all

$sql_check_tag = mysql_query("SELECT * FROM f_tag_f_user WHERE user_id='$_SESSION[user_id]'");
while ($check_tag = mysql_fetch_array($sql_check_tag, MYSQL_ASSOC)){

$tag_count = mysql_num_rows($sql_check_tag);


	$sql_fetch_tag = mysql_query("SELECT * FROM f_tags WHERE tag_id= '$check_tag[tag_id]'");
	while($fetch_tag = mysql_fetch_array($sql_fetch_tag, MYSQL_ASSOC)){
	
	$tags = array($fetch_tag['tag']);
	
	
	$tag_list = '<select name="tag_list" size="10">';
	   
             for($x=0; $x<=$tag_count; $x++){
	
	             $tag_list .= ' <option value="">' . $tags[$x] . '</option>';
	
	     }
	
	$tag_list .= '</select>';
	
		}	

}

Posted: Mon Mar 06, 2006 4:04 pm
by rubberjohn
thanks for your time neophyte but i've managed to sort it out

rj