Page 1 of 1

dropdown menu using database table?

Posted: Tue Sep 11, 2007 9:15 pm
by snappydesigns
Hi again,

I'm trying to figure out how to make a dropdown menu using values from a table (actually probably from 2 columns. Ok, here's a rundown:

I've got a table called prices; it has 2 columns: size, price. Sizes are 5x7, 8x10, 11x14, etc. The corresponding prices are 20.00, 25.00, 45.00, etc. So I want the dropdown menu to pull all the values from the corresponding columns. For example, 5x7 for $20.00, 8x10 for $25.00, etc. I am then working on taking the print size and corresponding price chosen and making a simple shopping cart to submit orders of several prints ordered.

This is what I have now which works fine if I don't have to use the information later that the customer chose:

Code: Select all

echo '<tr>'; 
echo '<td><font size="1" face="Arial, Helvetica, sans-serif">';
echo '<select name="quantity" class="menubox" id="quantity" style="width:140;">';
echo '<option selected>Select Print Size</option>';
echo '<option>8 wallets: $' . $row['8wallets'] . '</option>';
echo '<option>5x7: $' . $row['5x7'] . '</option>';
echo '<option>8x10: $' . $row['8x10'] . '</option>';
echo '<option>11x14: $' . $row['11x14'] . '</option>';
echo '<option>16x20: $' . $row['16x20'] . '</option>';
echo '<option>20x24: $' . $row['20x24'] . '</option>';
echo '<option>20x30: $' . $row['20x30'] . '</option>';
echo '</select>';
echo '</font></td>';
echo'</tr>';
I have found some examples but they don't incorportate into html tables like I have (I know, a pain). One such example that seems promising is this:

Code: Select all

echo "<select name='state'>"; 
while($row = mysql_fetch_assoc($result)) {  
echo "<option value='$row[state_name]'";  
if (previously selected option == $row['state_name']) {  
echo " selected='selected'";  
}  
echo ">$row[state_name]"; 
} 
echo "</select>";
I have no idea what else to do. This is a whole new territory for me, and my MySql/PHP skills are quite limited. Using a great book to walk me through this, but it doesn't do advanced stuff like this. Any help would be greatly appreciated :D Thanks so much!

Posted: Wed Sep 12, 2007 7:23 pm
by califdon
Do I understand that your dropdown box is working correctly? (It looks like it should.) Then what is it that you want to do next? That wasn't clear. Do remember that nothing that you do in PHP can be dependent on what happens after the page is sent to the browser, so if you want to use the value that the user selects to do something else, you have to either use Javascript or send the results back to another PHP script on the server.

Posted: Thu Sep 13, 2007 3:32 pm
by snappydesigns
well hello,

Thanks, califdon, for the response. I actually got it working after doing extensive research and just trying anything I came across. I'm now working on a different problem, extending what I was working on before. I've got a book that I'm trying to follow pretty closely to set up an e-commerce portion of my site for people to order prints from photo shoots--no money exchange on the site, just submitting a detailed order form.

I'm now working on the "add to cart" portion where the customer clicks "Add to Cart" and then it takes them to a new page that will say something like, "The print has been added to your order." It's not my first choice for set up, but beggers can't be choosers in my position of having little knowledge in this area.

The addtocart.php code is this:

Code: Select all

<?php # Script 14.9 - add_cart.php
// This page adds prints to the shopping cart.

session_start();
// Set the page title and include the HTML header.
include_once 'headerorder.htm';

$page_title = 'Add to Cart';

if (isset ($_GET['pid'])) { // Check for a print ID.

	$pid = $_GET['pid'];

	// Check if the cart already contains one of these prints, increment the quantity.
	if (isset($_SESSION['cart'][$pid])) {
	
		$_SESSION['cart'][$pid]['quantity']++; // Add another.

		// Display a message.
		echo '<p>Another copy of the print has been added to your shopping cart.</p>';
		
	} else { // New product to the cart, get the price information.

		include_once 'connect_orders.php'; // Connect to the database.
		
		$query = "SELECT * FROM john1_proofs, prices1 WHERE john1_proofs.prints = prices1.prints AND john1_proofs.proof_id = '$pid' ";
		$result = mysql_query ($query);
		
		if (mysql_num_rows($result) == 1) { // Valid print ID.
		
			// Fetch the information.
			list($price) = mysql_fetch_array ($result, MYSQL_NUM);
			
			// Add to the cart.
			$_SESSION['cart'][$pid] = array ('quantity' => 1, 'price' => $price);

			// Display a message.
			echo '<p>The print has been added to your shopping cart.</p>';

		} else { // Not a valid print ID.
			echo '<div align="center">This page has been accessed in error!</div>';		
		}
		
		

	} // End of isset($_SESSION['cart'][$pid] conditional.

} else { // No print ID.
	echo '<div align="center">This page has been accessed in error!</div>';
}

include_once 'footerorder.htm'; 
?>
Here's the link to the order page: http://www.snappychicdesigns.com/viewpr ... .php?pid=3 . When I click "Add to Cart", I just get the message, "This page has been accessed in error!" that's at the end of the code. This must mean that nothing is happening. Does anyone have idea what I can do to make work. I really haven't changed the code from the book other than adding my own variables and table info.

Thank in advance to anyone who can help me!
Jen