[SOLVED]Populating a dynamic list box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

[SOLVED]Populating a dynamic list box

Post 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
Last edited by rubberjohn on Mon Mar 06, 2006 4:03 pm, edited 1 time in total.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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;
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Post 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>';
	
		}	

}
rubberjohn
Forum Contributor
Posts: 193
Joined: Fri Feb 25, 2005 4:03 am

Post by rubberjohn »

thanks for your time neophyte but i've managed to sort it out

rj
Post Reply