Page 1 of 1
[56k Warn] Putting SELECTED into option tags
Posted: Thu Nov 23, 2006 7:28 pm
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.
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:
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.
Posted: Thu Nov 23, 2006 7:49 pm
by feyd
You echo the selected attribute into the options which should be selected.
Posted: Thu Nov 23, 2006 7:53 pm
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.
Posted: Thu Nov 23, 2006 8:00 pm
by feyd
Typically that will be an INNER JOIN type query.
Posted: Thu Nov 23, 2006 8:08 pm
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.
Posted: Thu Nov 23, 2006 8:56 pm
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?
Posted: Thu Nov 23, 2006 9:00 pm
by feyd
Storing up the results from the first query into a basic array will allow you to use
in_array(), potentially.
Posted: Thu Nov 23, 2006 9:18 pm
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!
Posted: Thu Nov 23, 2006 10:09 pm
by feyd
You'll have to compare the selections they made versus those in the database and decide from there.