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

LVL 2
ironwill96Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cr4ck3rj4ckCommented:
if ($catExists = mysql_fetch_array($sql_result))

should be

if ($catExists == mysql_fetch_array($sql_result))

Try that,
CJ
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ironwill96Author Commented:
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
0
ironwill96Author Commented:
Whoops, looks like it should also be $sql_resultCat

Trying that change now..
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

ironwill96Author Commented:
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
0
ironwill96Author Commented:
Thanks for the help, I noticed the actual issue after going in to make your suggested change!
0
NerdsOfTechTechnology ScientistCommented:
Change Line 15
                if ($catExists = mysql_fetch_array($sql_result))
To
                if ($catExists = mysql_fetch_array($sql_resultCat))
0
NerdsOfTechTechnology ScientistCommented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.