Jump to content

array multiple results help


mrooks1984

Recommended Posts

hello, i am hoping someone can help me, i am trying to build a array all the array displays correctly using the print_r so i know that parts working.

the issue is i have 2 arrays one with the drop down menu name and other information and the other one with the values for each dropdown menu.

i am currently trying to figure out and need help on how to get it to display multiple menus if they exist in the database, my current code only displays the last result of the array.

 

heres the code

function product() {
	//Import product varible
	global $product;
	$product = mysql_real_escape_string($product);
	//convert product value into a title
	$value = str_replace("-"," ",$product);

	//Connect to database and get the required product.
	$result = mysql_query("SELECT * FROM store_products WHERE name = '$value' ORDER BY id");

	//If the product is not found in the database.
	if (mysql_num_rows($result) == 0) {

		//Display a message
		echo '<h1>' . "This product doesent exist" . '</h1>'  . "\n";

		//If products are found in database.
	} else {
		while ($row = mysql_fetch_array($result)) {

			$store_products[] = array("name" => $row['name'], "image" => $row['image'], "category" => $row['category'], "description"=> $row['description'], "price" => $row['price']);

		}

		foreach ($store_products as $store_product)

		//Get variations Names
		$sql = "SELECT * FROM store_variations WHERE product = '$product'";
		$res = mysql_query($sql) or die(mysql_error());
		//If the options are not named
		if (0 === mysql_num_rows($res)) {

		} else {

			while ($row = mysql_fetch_assoc($res)) {

				$store_variations[] = array("title" => $row['title'], "variation" => $row['variation'], "price" => $row['price'], "product"=> $row['product']);

			}
			foreach ($store_variations as $store_variation)
			print_r($store_variations);
		}

		//Get variations values
		$sql = "SELECT * FROM store_variations_values WHERE product = '$product'";
		$res = mysql_query($sql) or die(mysql_error());
		//If the options are not named
		if (0 === mysql_num_rows($res)) {

		} else {

			while ($row = mysql_fetch_assoc($res)) {

				$store_variations_values[] = array("title" => $row['title'], "variation" => $row['variation'], "price" => $row['price'], "product"=> $row['product']);

			}
			foreach ($store_variations_values as $store_variation_values)
			print_r($store_variations_values);
		}

		//Show Product form.

		echo '<div id ="StoreCol1">' . "\n";
		echo '<form method="post" action="">' . "\n";
		echo '<h1>' . $store_product['name'] . '</h1>' . "\n";
		echo '<input type="hidden" name="name" value="' . $store_product['name'] . '">' . "\n";
		echo '<a href="public/images/' . $store_product['image'] . '" target="_blank"><img src="public/images/' . $store_product['image'] . '" height="200" width="300" style="border:none;" /></a>' . "\n";
		echo '<p>' . $store_product['description'] . '</p>' . "\n";
		echo '<a href="store.php?category=' . $store_product['category'] . '">' . "Go Back" . '</a>' . "\n";
		echo '</div>' . "\n";
		echo '<div id="StoreCol2">' . "\n";
		echo '<p>' . "Any extra charges will be displayed and added in your cart." . '</p>' . "\n";
		echo '<p>' . '<b>' . "Price: £" . $store_product['price'] . '</b>' . "</p>" . "\n";
		echo '<p>' . "Excludes postage and packaging." . '</p>' . "\n";
		//Add options here
		if ($store_variation_values['variation'] == $store_variation['variation']) {
			echo '<p>' . $store_variation['title'] . ": ";
			echo '<select name = "option'.$store_variation['variation'].'" >' . "\n";

			echo '<option value= "'.$store_variation_values['title'].'">'.$store_variation_values['title'].'</option>' . "\n";				
			echo '</select></p>';
			echo '</form></div>';
			echo $store_variation_values['title'];

		}
	}
}

i think this is the area that needs fixing, maybe a loop or something, just not sure how to.

echo '<p>' . $store_variation['title'] . ": ";
echo '<select name = "option'.$store_variation['variation'].'" >' . "\n";

echo '<option value= "'.$store_variation_values['title'].'">'.$store_variation_values['title'].'</option>' . "\n";				
echo '</select></p>';
echo '</form></div>';
echo $store_variation_values['title'];

if anyone could help me i would be really greatful, thanks all

Link to comment
Share on other sites

by the way this is a continueation of this: http://forums.phpfreaks.com/index.php?topic=363743.15 but no longer fitted under the title and i cant edit my posts so moved to this one.

 

is there away to e.g. if i have 3 results for $store_variation_values['title']; is there a way to call out say result 1 with my current code. if this is possible how, as i could make it work with a run loop.

Link to comment
Share on other sites

Looks like you are holding same fields in store_variations and store_variations_values.  In any case, I made a variation of your code making two queries and building one array.  I normally don't like nested queries but building that sub variations array was giving me trouble without it.  I wouldn't expect this to work (out of the box) as I'm not querying the store_variations_values table but maybe it will get you moving forward.

<?php
function product() {
//Import product varible
global $product;
$product = mysql_real_escape_string($product);
//convert product value into a title
$value = str_replace("-"," ",$product);

//Connect to database and get the required product.
$sql  = "SELECT  id, category, name, image, description, price ";
$sql .= "FROM store_products ";
$sql .= "WHERE name = '$value' ORDER BY id ASC";
$result = mysql_query($sql) or die(mysql_error());
//If the product is not found in the database.
if (mysql_num_rows($result) == 0) {
	//Display a message
	echo '<h1>' . "This product doesent exist" . '</h1>'  . "\n";
//If products are found in database.
} else {
	while ($row = mysql_fetch_array($result)) {
	$store_products[$row['id']] = array("name" => $row['name'], "image" => $row['image'], "category" => $row['category'], "description"=> $row['description'], "price" => $row['price'],);
		//Get variations for product and add to array
		$sql2  = "SELECT title, variation, price ";
		$sql2 .= "FROM store_variations ";
		$sql2 .= "WHERE product = '{$row['name']}'";
		$result2 = mysql_query($sql2) or die(mysql_error());
		if (mysql_num_rows($result2) != 0){
			while ($row2 = mysql_fetch_array($result2)) {
				$store_products[$row['id']]['variation'][] = array("title" =>$row2['title'],"variation" =>$row2['variation'],"price" =>$row2['price']);
			}
		}
	}
}


	//Show Product form.
	foreach ($store_products as $store_product){
		echo '<form method="post" action="">' . "\n";
			echo '<div id ="StoreCol1">' . "\n";
				echo '<h1>' . $store_product['name'] . '</h1>' . "\n";
				echo '<input type="hidden" name="name" value="' . $store_product['name'] . '">' . "\n";
				echo '<a href="public/images/' . $store_product['image'] . '" target="_blank"><img src="public/images/' . $store_product['image'] . '" height="200" width="300" style="border:none;" /></a>' . "\n";
				echo '<p>' . $store_product['description'] . '</p>' . "\n";
				echo '<a href="store.php?category=' . $store_product['category'] . '">' . "Go Back" . '</a>' . "\n";
			echo '</div>' . "\n";
			echo '<div id="StoreCol2">' . "\n";
				echo '<p>' . "Any extra charges will be displayed and added in your cart." . '</p>' . "\n";
				echo '<p>' . '<b>' . "Price: £" . $store_product['price'] . '</b>' . "</p>" . "\n";
				echo '<p>' . "Excludes postage and packaging." . '</p>' . "\n";

				//Add options here
				echo '<p>' . $store_product['variation'][0]['title'] . ": ";
				echo '<select name = "variation_option" >' . "\n";	 	
				foreach($store_product['variation'] as $variation) {
					echo '<option value= "'.$variation['variation'].'">'.$variation['variation'].'</option>' . "\n";
				}
				echo '</select></p>';
			echo '</div>' . "\n";		
		echo '</form>';	
	}
//Uncomment to see array
/*
echo "<pre>";
print_r($store_products);
echo "</pre>";
*/
}
?>

 

Note, I have not looked at your other post.

Link to comment
Share on other sites

Looking at your other post I see you are wishing to loop through variations so you have a select box for each type.  I think you could probably store things a little better but won't get into that.  Here's a version, doing three queries and building one array.  Should be close to what you need.

<?php
function product() {
//Import product varible
global $product;
$product = mysql_real_escape_string($product);
//convert product value into a title
$value = str_replace("-"," ",$product);

//Connect to database and get the required product.
$sql  = "SELECT  id, category, name, image, description, price ";
$sql .= "FROM store_products ";
$sql .= "WHERE name = '$value' ORDER BY id ASC";
$result = mysql_query($sql) or die(mysql_error());
//If the product is not found in the database.
if (mysql_num_rows($result) == 0) {
	//Display a message
	echo '<h1>' . "This product doesent exist" . '</h1>'  . "\n";
//If products are found in database.
} else {
	while ($row = mysql_fetch_array($result)) {
	$store_products[$row['id']] = array("name" => $row['name'], "image" => $row['image'], "category" => $row['category'], "description"=> $row['description'], "price" => $row['price'],);
		//Get variations for product and add to array
		$sql2  = "SELECT title, variation, price ";
		$sql2 .= "FROM store_variations ";
		$sql2 .= "WHERE product = '{$row['name']}'";
		$result2 = mysql_query($sql2) or die(mysql_error());
		if (mysql_num_rows($result2) != 0){
			while ($row2 = mysql_fetch_array($result2)) {
				$store_products[$row['id']]['variation'][] = array("title" =>$row2['title'],"variation" =>$row2['variation'],"price" =>$row2['price']);
			}
		}
		//Get variation values for product and add to array
		$sql3  = "SELECT title, variation, price ";
		$sql3 .= "FROM store_variations_values ";
		$sql3 .= "WHERE product = '{$row['name']}'";
		$result3 = mysql_query($sql3) or die(mysql_error());
		if (mysql_num_rows($result3) != 0){
			while ($row3 = mysql_fetch_array($result3)) {
				$store_products[$row['id']]['variation_values'][] = array("title" =>$row3['title'],"variation" =>$row3['variation'],"price" =>$row3['price']);
			}
		}
	}
}


	//Show Product form.
	foreach ($store_products as $store_product){
		echo '<form method="post" action="">' . "\n";
			echo '<div id ="StoreCol1">' . "\n";
				echo '<h1>' . $store_product['name'] . '</h1>' . "\n";
				echo '<input type="hidden" name="name" value="' . $store_product['name'] . '">' . "\n";
				echo '<a href="public/images/' . $store_product['image'] . '" target="_blank"><img src="public/images/' . $store_product['image'] . '" height="200" width="300" style="border:none;" /></a>' . "\n";
				echo '<p>' . $store_product['description'] . '</p>' . "\n";
				echo '<a href="store.php?category=' . $store_product['category'] . '">' . "Go Back" . '</a>' . "\n";
			echo '</div>' . "\n";
			echo '<div id="StoreCol2">' . "\n";
				echo '<p>' . "Any extra charges will be displayed and added in your cart." . '</p>' . "\n";
				echo '<p>' . '<b>' . "Price: £" . $store_product['price'] . '</b>' . "</p>" . "\n";
				echo '<p>' . "Excludes postage and packaging." . '</p>' . "\n";

				//Add options here

				foreach($store_product['variation'] as $variation) {
					echo '<p>' . $variation['title'] . ": ";
					echo '<select name = "variation_option" >' . "\n";	 	
					foreach($store_product['variation_values'] as $variation_values) {
						if ($variation['title']==$variation_values['title']){
							echo '<option value= "'.$variation_values['variation'].'">'.$variation_values['variation'].'</option>' . "\n";
						}
					}
					echo '</select></p>';
				}
			echo '</div>' . "\n";		
		echo '</form>';	
	}
//Uncomment to see array
/*
echo "<pre>";
print_r($store_products);
echo "</pre>";
*/
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.