Page 1 of 1

Sorting an array by price, using a form

Posted: Mon Aug 08, 2005 5:58 am
by keveh
Hi there, I have a web site which has an sql database full of wines.

When somebody visits this page, the wines are soted by year, but i want the user to have an option to sort the wines by price.

The way in which I have done this myself, is have a form with a select drop down menu where the user can sort by higher or lower prices. The form is submitted to the same page, and the forms are generated again, depending on which option was selected.

Here is my code:

Code: Select all

if (isset($order) && $order == "$lowest"){ ?>
		
				<form name="order" method="post" action="redwine.php">
					<select name="order">
						<option value="lowest" selected>Lowest Price</option>
						<option value="highest">Highest Price</option>
					</select>
					<input type="submit" value="Sort" class="addwine">
				</form>					
				<?php
					$result = mysql_query( "SELECT * FROM sullyvin WHERE wine='red single' ORDER BY price ASC", $link );
				
				
				} elseif (isset($order) && $order == "$highest"){ ?>
				<form name="order" method="post" action="redwine.php">
					<select name="order">
						<option value="lowest">Lowest Price</option>
						<option value="highest" selected>Highest Price</option>
					</select>
					<input type="submit" value="Sort" class="addwine">
				</form>	
				<?php
					$result = mysql_query( "SELECT * FROM sullyvin WHERE wine='red single' ORDER BY price DESC", $link );
				
				
				}else{ ?>
					<form name="order" method="post" action="redwine.php">
						<select name="order">
							<option value="lowest">Lowest Price</option>
							<option value="highest">Highest Price</option>
						</select>
						<input type="submit" value="Sort" class="addwine">
					</form>
				<?php
					$result = mysql_query( "SELECT * FROM sullyvin WHERE wine='red single' ORDER BY year ASC", $link );
				}
Now, this works when the wines are ordered by the lowest price, but for some reason it wont work when the user selects, highest price.

The page is located at:

http://www.incitodesign.com/sullyvin/redwine.php

if you want to take a look at the page.

can anybody see the reason why this isn't working?

Posted: Mon Aug 08, 2005 6:35 am
by feyd
doesn't appear to work, period.

How are the prices stored?

Posted: Mon Aug 08, 2005 6:43 am
by Sander

Code: Select all

if (isset($order) && $order == "$lowest"){ // $lowest??>

Code: Select all

} elseif (isset($order) && $order == "$highest"){ // $highest? ?>
I think you should get rid of the '$' in '$lowest' and '$highest'.

Posted: Mon Aug 08, 2005 6:54 am
by keveh
Sander wrote:

Code: Select all

if (isset($order) && $order == "$lowest"){ // $lowest??>

Code: Select all

} elseif (isset($order) && $order == "$highest"){ // $highest? ?>
I think you should get rid of the '$' in '$lowest' and '$highest'.
thanks!

that worked perfectly.

why would it work that way?

Posted: Mon Aug 08, 2005 6:55 am
by feyd
you don't have $lowest and $highest variables, likely...

Posted: Mon Aug 08, 2005 10:42 am
by Ambush Commander
Double quotes (") interpolate variables inside them. For instance:

Code: Select all

$lowest = 'selected';
var_dump("$lowest"); //outputs 'selected'
var_dump($lowest); //outputs 'selected'
var_dump("\$lowest"); //outputs '$lowest'
The last instance, I escaped the dollar sign ($) with a backslash (\) to tell PHP not to interpolate the variables.

See more here: http://us2.php.net/manual/en/language.types.string.php