ironwill96
asked on
Using PHP with MySQL to fill a Select Box and Pre-Select Items
Hey all, thanks in advance for any help you can provide.
I know i'm overlooking something silly here but I can't get my little piece of code to work for pre-selecting several items in a select box (listbox). I've attached the code in question. What I am trying to doo is fill the select box with category names (this part works), but I also want to pre-select some items based on whether a particular user has those categories assigned to them. The user category assignment table just consists of CATID paired with USERID so i'm trying to do a lookup on both and then insert the "selected" attribute when they are found.
What happens with this code is that it selects everything instead of just the ones that match. How can I check to see if a result is returned from the MySQL query without needing to pull the row etc?
Thanks,
Nathan
I know i'm overlooking something silly here but I can't get my little piece of code to work for pre-selecting several items in a select box (listbox). I've attached the code in question. What I am trying to doo is fill the select box with category names (this part works), but I also want to pre-select some items based on whether a particular user has those categories assigned to them. The user category assignment table just consists of CATID paired with USERID so i'm trying to do a lookup on both and then insert the "selected" attribute when they are found.
What happens with this code is that it selects everything instead of just the ones that match. How can I check to see if a result is returned from the MySQL query without needing to pull the row etc?
Thanks,
Nathan
<select name="lstCategories" size="20" multiple="multiple" id="lstCategories">
<?php
$sql = "SELECT * FROM $tableCategories ORDER BY CATNAME ASC";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if ($myrow = mysql_fetch_array($sql_result)) {
do
{
$sqlCat = "SELECT * FROM $tableReviewerInfo WHERE USERID = $userID AND CATID = '".$myrow['CATID']."'";
$sql_resultCat = mysql_query ($sqlCat, $connection) or die ('request "Could not execute SQL query" '.$sql);
printf("<option value={$myrow['CATNAME']}");
if ($catExists = mysql_fetch_array($sql_result))
{
printf(" selected");
}
printf(">{$myrow['CATNAME']}</option>");
} while ($myrow = mysql_fetch_array($sql_result));
}
?>
</select>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Whoops, looks like it should also be $sql_resultCat
Trying that change now..
Trying that change now..
ASKER
Yeah it was because I didn't have $sql_resultCat in my original check (it should remain = though, not ==).
Thanks for pointing me in the right direction!
Nathan
Thanks for pointing me in the right direction!
Nathan
ASKER
Thanks for the help, I noticed the actual issue after going in to make your suggested change!
Change Line 15
if ($catExists = mysql_fetch_array($sql_res ult))
To
if ($catExists = mysql_fetch_array($sql_res ultCat))
if ($catExists = mysql_fetch_array($sql_res
To
if ($catExists = mysql_fetch_array($sql_res
Try the above example FIRST
I also rewrote the code (not tested) below also...
I also rewrote the code (not tested) below also...
<select name="lstCategories" size="20" multiple="multiple" id="lstCategories">
<?php
$sql = "SELECT * FROM $tableCategories ORDER BY CATNAME ASC";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($myrow = mysql_fetch_array($result)){
$sqlCat = "SELECT * FROM $tableReviewerInfo WHERE USERID = $userID AND CATID = '".$myrow['CATID']."'";
$sql_resultCat = mysql_query ($sqlCat, $connection) or die ('request "Could not execute SQL query" '.$sql);
printf("<option value={$myrow['CATNAME']}");
if ($catExists = mysql_fetch_array($sql_resultCat))
{
printf(" selected");
}
printf(">{$myrow['CATNAME']}</option>");
}
}
?>
</select>
ASKER
Thanks,
Nathan