[56k Warn] Putting SELECTED into option tags

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
trent2800
Forum Commoner
Posts: 48
Joined: Mon Oct 02, 2006 7:02 am

[56k Warn] Putting SELECTED into option tags

Post by trent2800 »

I've posted on similar stuff before, but I can't seem to make something work for this. This time however, I made a visio diagram to assist anyone who would help.

Image

What I have done so far is when you edit an incense, it queries the database for the current types of sizes and puts it into a multiple select box:

Image

What I need to do is figure out a way to make some of the option tags have SELECTED in them if there is a corrosponding ProductID and CategoryID in the INCENSE SIZE database.

This seems like it should be simple, and I may well figure it out here soon... but I thought that the talented people here at PHPDN might think of it faster than I.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You echo the selected attribute into the options which should be selected.
trent2800
Forum Commoner
Posts: 48
Joined: Mon Oct 02, 2006 7:02 am

Post by trent2800 »

I mean how would the code to determine that go? Im trying to figure out how the database queries would work and how to code the whole thing. Displaying the results from the SIZE table is easy, but determining which ones are in the INCENSE_SIZE table and then highliting the proper entries in the select box is not as easy.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Typically that will be an INNER JOIN type query.
trent2800
Forum Commoner
Posts: 48
Joined: Mon Oct 02, 2006 7:02 am

Post by trent2800 »

Code: Select all

$query2 = "SELECT * FROM INCENSE_SIZE WHERE ProductID = '" . $ProductID . "' AND SizeID = '".$SizeID."'";

$result2 = mysql_query($query2) or die('Query failed: ' . mysql_error());

$row2 = mysql_fetch_assoc($result2);

$query3 = "SELECT * FROM CATEGORY WHERE Parent = '8' ORDER BY CategoryID ASC";

$result3 = mysql_query($query)3 or die('Query failed: ' . mysql_error());

echo "<tr align=left><td>Category: </td><td><select name=\"CategoryID\">\n";

while ($row3 = mysql_fetch_assoc($result3)) {

	$message = "<option value=\"" . $row3['CategoryID'] ."\"";
	if ($row3['SizeID'] == $row3['SizeID']) { $message = $message . " SELECTED >"; }else{$message = $message . ">";}
	$message = $message . $row3['Name'] . "</option>\n";
	
	echo $message;
}
echo "</select><br /></td>\n";
This is close I think. The only thing is that $row will return multiple rows. I need to figure out how to cycle through each of these rows to determine if the current SizeID matches any of the ones in INCENSE_SIZE.
trent2800
Forum Commoner
Posts: 48
Joined: Mon Oct 02, 2006 7:02 am

Post by trent2800 »

Code: Select all

$query2 = "SELECT * FROM SIZE";

$result2 = mysql_query($query2) or die('Query failed: ' . mysql_error());

echo "<tr align=left><td>Sizes:</td><td><em>Hold CTRL to Select Multiple</em><br><select MULTIPLE size=10 name=\"Pics[]\">\n";

while ($row2 = mysql_fetch_assoc($result2)) {
	
	$query4 = "SELECT * FROM `INCENSE_SIZE` WHERE `ProductID` = '".$ProductID."' AND `SizeID` = '".$row2['SizeID']."'";

	$result4 = mysql_query($query4) or die('Query failed: ' . mysql_error());
	
	//echo mysql_num_rows($result4);
	
	$message = "<option value=\"" . $row2['Name'] ."\"";
	
	if(!mysql_num_rows($result4)){$message = $message . ">";}
	
	while ($row4 = mysql_fetch_assoc($result4)) {
		if ($row4['SizeID'] == $row2['SizeID']) { $message = $message . " SELECTED >";}
	}
	
	$message = $message . $row2['Name'] . "</option>\n";
	
	echo $message;
}
Wow, is it just me or is figureing out these sort of problems better than sex? Wooooooo....

More to the point, is this elegant or can I do this better?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Storing up the results from the first query into a basic array will allow you to use in_array(), potentially.
trent2800
Forum Commoner
Posts: 48
Joined: Mon Oct 02, 2006 7:02 am

Post by trent2800 »

I'm thinking that after this is submitted I should first drop all of the entries in INCENSE SIZE where ProductID = the product id for that item then insert the ones that were submitted. Does that sound reasonable? Or is there a better way?

ED The problem I see is that every time you edit it, you're going to lose all the QOH and price data.... how would I find out which ones need to be droped and which ones need to be added? ... this is getting tough!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You'll have to compare the selections they made versus those in the database and decide from there.
Post Reply