Sorting an array by price, using a form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
keveh
Forum Commoner
Posts: 27
Joined: Mon Aug 08, 2005 5:50 am

Sorting an array by price, using a form

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

doesn't appear to work, period.

How are the prices stored?
Sander
Forum Commoner
Posts: 38
Joined: Sat Aug 06, 2005 12:43 pm

Post 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'.
keveh
Forum Commoner
Posts: 27
Joined: Mon Aug 08, 2005 5:50 am

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you don't have $lowest and $highest variables, likely...
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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
Post Reply