Jump to content

[SOLVED] Help with SELECT statement


thomashw

Recommended Posts

I'm having trouble getting something to be in alphaebetical order.

 

Here's my code:

 

$query = mysql_query( "SELECT * FROM feature_group, feature_item WHERE feature_group.feature_product_id= {$id} AND feature_item.feature_group_id = feature_group.feature_group_id ORDER BY feature_group.feature_group_id" ) or die(mysql_error());
$curr = '';
$number = 0;

echo '<table class="purchasetable">';
echo '<form method="post" action="cart.php">';

if( mysql_num_rows( $query ) > 0 )
{
while($row = mysql_fetch_assoc($query))
{
	if($curr != $row['feature_group_name'])
	{
		if($curr != '')
		{
			echo "</select>\n</td></tr>\n<br />\n";
			$number++;
		}
		echo "<tr>\n<td>\n<label><span>";
		echo $row['feature_group_name'];
		echo '&#58;';
		echo "</span></label>\n</td>";
		echo "\n<td class=\"productdrop\">\n<select name=\"option" . $number . "\">\n";
		$curr = $row['feature_group_name'];//update the current group
	}
	echo '<option value="' . htmlspecialchars( $row['feature_group_name'] ) . ': ' . htmlspecialchars( $row['feature_item_name'] ) . '|' . htmlspecialchars( $row['feature_item_price'] ) . '">'.$row['feature_item_name'] .'</option>';
}
echo "</select>\n</td>\n</tr>\n<br />\n";
}

 

What this code does is populate select boxes with options. The options that go into the select boxes are contained in the feature_item table. The feature_group table just contains the names of the select boxes. Each option in the feature_item table has a feature_group id associated with it.

 

The feature_item's that are placed into the options aren't in alphaebetical order. I have to order the first query by feature_group_id or else sometimes a select box isn't fully populated before it goes onto the next one.

 

I'm thinking I should make my first query into two query's. One that selects the name of the select boxes, and then inside the while-loop have another query select all the options for the current select box using the feature_group id. I'm just not sure how to code it so it knows which group_id it's on.

 

Anyone know what I mean?

Link to comment
https://forums.phpfreaks.com/topic/106682-solved-help-with-select-statement/
Share on other sites

If I heard you right (and I'm tired and may not have) -- you should look into JOIN commands.

 

SELECT * FROM feature_group g JOIN feature_item i ON g.feature_group_id=i.feature_group_id ORDER BY g.feature_group_id ASC

 

Google "mysql join" to see what it does.  Adding 'ASC' or 'DESC' after ORDER BY just sets which order -- and if I remember right (again, I'm tired) ASC will go from A-Z.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.