Jump to content

[SOLVED] Need a bit of help


thomashw

Recommended Posts

In this following code, you'll notice there are two $form['feature_group_name'] and two $form['feature_item_name'].

 

<?
$query = mysql_query("
SELECT feature_product.feature_product_id, feature_product_name, feature_product_price, feature_group.feature_group_id, feature_item.feature_group_id, feature_group_name, feature_item_name, feature_item_price 
FROM feature_product, feature_group, feature_item 
WHERE feature_product.feature_product_id={$id} 
AND feature_group.feature_product_id=feature_product.feature_product_id 
AND feature_item.feature_group_id=feature_group.feature_group_id");
echo "<tr>\n<td>\n<label><span>";
$count==0;
while($form = mysql_fetch_array($query)) {
if($count==0) {
echo $form['feature_group_name'] . '&#58;';
echo "</span>\n</label>\n</td\n<td align=\"left\" width=\"57%\" class=\"productdrop\">\n<select name=\"cat1\">";
echo '<option value="' . $form['feature_group_name'] . '&#58;' . $form['feature_item_name'] . '">' . $form['feature_item_name'] . '</option>';
++$count;
}}
echo "</select>\n</td>\n</tr>\n<br />";
?>

 

The first $form['feature_group_name'] is displayed as field_name in the following image (it's just some text.) The $form['feature_item_name'] are the actual options in the dropdown boxes.

 

dropdown.gif

 

In my database, there are multiple options per "field_name" for the dropdown boxes, as well as multiple "field_names" (as shown in the image.) I need the <option></option> field for the first dropdown box to loop until all $form['feature_item_name'] fields are used up for that specific $form['feature_group_name'] (field_name in the image.) Then I need the whole thing to move on (loop) to the next dropdown box (and field_name.)

 

Basically, I need the part in the <option></option> area to loop until all the $form['feature_item_name'] are used up, and then I need the whole thing to echo to create the second drop down box along with its "field_name."

 

Does this make sense, or did I overcomplicate my explanation?

 

Anyways, the way I've done it it isn't going to loop properly. I was wondering if someone could help. :)

Link to comment
https://forums.phpfreaks.com/topic/85145-solved-need-a-bit-of-help/
Share on other sites

Well, firstly, you need to be ordering by the groups. You can then cycle through the results, outputing a new select box if the group name of the current row is different to the last row's group name. Try:

 

<?php
$query = mysql_query("
SELECT feature_product.feature_product_id, feature_product_name, feature_product_price, feature_group.feature_group_id, feature_item.feature_group_id, feature_group_name, feature_item_name, feature_item_price 
FROM feature_product, feature_group, feature_item 
WHERE feature_product.feature_product_id={$id} 
AND feature_group.feature_product_id=feature_product.feature_product_id 
AND feature_item.feature_group_id=feature_group.feature_group_id ORDER BY feature_group_name") or die(mysql_error());
$curr = '';
while($row = mysql_fetch_assoc($query)){
if($curr != $row['feature_group_name']){//last group isn't the same as the current one
	if($curr != ''){//we're not on the first iteration of the loop - end the last select box
		echo "</select> <br />\n";
	}
	//output the new select box
	echo '<select="'.$row['feature_group_name'].'">';//not sure what name you wanted the select boxes to have?
	$curr = $row['feature_group_name'];//update the current group
}
//output the option for this row
echo '<option value="'.$row['feature_group_name'].':'.$row['feature_item_name'] . '">'.$row['feature_item_name'] ."</option>\n";
}
echo "</select><br />\n";
?>

 

You'll need to format the layout a bit, and you might need to change the names of the select boxes/options - i wasn't entirely sure what you wanted.

 

Hope that helps.

Ahh, okay. I use div's as much as I can, but usually for something like this it's much easier to get everything to line up with a table.

 

It's not "bad" to use tables for content, is it?

 

def not "bad" - whatever works, works! forums like this still use tables because it's the best approach that works well.

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.