Forum Moderators: coopster

Message Too Old, No Replies

Statistics page with "SORT BY" from form input?

         

PartisanEntity

9:44 pm on May 8, 2009 (gmt 0)

10+ Year Member



I would like to create a statistics page, that pulls all data from a table called orders and sorts it according to the value from a drop down menu. I can't get it to work and would really appreciate any help.

Here is my php code:

<?php
session_start();

//include our db data to make the connection
include ('db.php');

$selector = $_GET['selector'];
$sorter = $_GET['sorter'];

//pull all the data from users table
$content = mysql_query("SELECT order_id, user_id, ingredients, price, quantity, Day, Date, Month, Year, Time FROM orders ORDER BY '$selector' '$sorter'");

while($row = mysql_fetch_array($content))
{
echo
$row['order_id'].' '.
$row['user_id'].' '.
$row['ingredients'].' '.
$row['price'].' '.
$row['quantity'].' '.
$row['Day'].' '.
$row['Date'].' '.
$row['Month'].' '.
$row['Year'].' '.
$row['Time'].'<br />';
}
?>

and here is the form:

<p>Sort according to:</p>
<form action="stats.php" method="GET">
<select name="selector">
<option>Selector</option>
<option value="Month">Month</option>
<option value="Year">Year</option>
<option value="nick">Nick</option>
<option value="order_id">Order ID</option>
</select>
<input type="submit" value="OK" /><br />
<select name="sorter">
<option>Sorter</option>
<option value="DESC">DESC</option>
<option value="ASC">ASC</option>
</select>
</form>

LifeinAsia

9:58 pm on May 8, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



What exactly isn't working? Are you getting error messages?

PartisanEntity

10:01 pm on May 8, 2009 (gmt 0)

10+ Year Member



No matter what values I choose from the drop down menu, the data is not being sorted according to it.

LifeinAsia

10:05 pm on May 8, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I don't know PHP, but what happens if you take out the single quotes in the ORDER BY '$selector' '$sorter' part?

PartisanEntity

10:07 pm on May 8, 2009 (gmt 0)

10+ Year Member



That was it, thanks! I never know when to use those single quotes and when not.