...

View Full Version : using functions to populate listboxes, need some advice please



LJackson
08-17-2012, 01:53 AM
Hi All

I have the following function which populates list boxes with a list of names

function playerSelect()
{
$sql = "SELECT staffName, staffID, staffType FROM tbl_staff WHERE staffType = 'player' ORDER BY staffName ASC";
$rs = mysql_query($sql);

for($i = 0; $i < mysql_num_rows( $rs ); $i++)
{
$tmp = mysql_fetch_row( $rs );
print("<OPTION value=\"$tmp[0]:$tmp[1]\">$tmp[0]</OPTION>\n");
}
}

a couple of questions if i may.

1/ how do i have the default value set to "Please Select" instead of the first name in the db?

2/ what does $tmp[0] and $tmp[1] do in the following code? <OPTION value=\"$tmp[0]:$tmp[1]\">

3/ is it possible to remove selections from the list once they have been selected so that once the function is run again the name that has already been selected in no longer available to select again?

many thanks
Luke

DrDOS
08-17-2012, 04:13 AM
I'll answer you first question, just make a single line of code that prints that option before the for loop runs.

LJackson
08-17-2012, 02:59 PM
cool thank you!!!!!

im guessing with the code below if i were to submit the form the values returned from the list boxes will be something like players ID and players Name which is what $tmp[0] and $tmp[1] are? instead of the actual value selected? have i got that right?


print("<OPTION value=\"$tmp[0]:$tmp[1]\">$tmp[0]</OPTION>\n");

DrDOS
08-17-2012, 04:49 PM
Just look at the source code for the generated page to see what they are. I don't see any quotes with dots around the variables in question, so I don't know how they will be interpreted by the parser.


print("<OPTION value=\"".$tmp[0].":".$tmp[1]."\">".$tmp[0]."</OPTION>\n");

That may be what you need instead.

LJackson
08-17-2012, 05:33 PM
ah thats a good idea never thought of that lol :D cheers mate

Len Whistler
08-17-2012, 05:36 PM
3/ is it possible to remove selections from the list once they have been selected so that once the function is run again the name that has already been selected in no longer available to select again?


You would add an if statement and compare the selection with a stored value. You could use cookies, sessions or a DB to hold the stored value.


This is a quick example, there probably is syntax errors. But it should give you an idea of what to do.

for($i = 0; $i < mysql_num_rows( $rs ); $i++) {
$tmp = mysql_fetch_row( $rs );
if ($stored_value != $tmp[0]:$tmp[1]) {
print("<OPTION value=\"$tmp[0]:$tmp[1]\">$tmp[0]</OPTION>\n");
}
}



----

DrDOS
08-17-2012, 06:16 PM
In addition to what Len Whistler has said you can use javascript to remove an option from a drop down, but you have to pass the code to it, and remenber it the next time the page loads.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum