Link to home
Start Free TrialLog in
Avatar of ironwill96
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


<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>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of cr4ck3rj4ck
cr4ck3rj4ck
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ironwill96
ironwill96

ASKER

Hmm, that stops them from all being selected but it selects the item AFTER the one it should and then the loop stops working (it doesnt finish filling the select box).  Any ideas?

Thanks,
Nathan
Whoops, looks like it should also be $sql_resultCat

Trying that change now..
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 the help, I noticed the actual issue after going in to make your suggested change!
Avatar of NerdsOfTech
Change Line 15
                if ($catExists = mysql_fetch_array($sql_result))
To
                if ($catExists = mysql_fetch_array($sql_resultCat))
Try the above example FIRST

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>

Open in new window