• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 775
  • Last Modified:

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

0
ironwill96
Asked:
ironwill96
  • 4
  • 2
1 Solution
 
cr4ck3rj4ckCommented:
if ($catExists = mysql_fetch_array($sql_result))

should be

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

Try that,
CJ
0
 
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now