if (in_array ($heading, $allowed_order)) {
echo "<TD><b><a href=\"{$_SERVER['PHP_SELF']}?order=$heading&direction=DESC\">$heading</a> </b></TD>";
} else { }
This is the URL of my headings.
I pick up $direction using GET.
$direction = @$_GET['direction'];
if ($direction !== "DESC") $direction = "ASC";
I am able to define the direction of the sorting by typing either DESC or ASC in the "direction= " part in the URL above.
Just for fun / testing I defined direction=7 in the URL and the results turn out ASC.
However, when I click on my heading links, I am not able to switch from ASC / DESC.
Here's a URL if it helps:
http://www.recordingreview.com/soundcard/soundcard_search/browse_new.php
Here's my code:
<body>
<?php
if (isset($_POST['advanced'])) {
// define variables from advanced form
$input=intval($_POST['inputs']);
$preamp= intval($_POST['preamps']);
$connections=$_POST['connection'];
$hizinputs= intval($_POST['hizinput']);
$midis=$_POST['midi'];
$softwares=$_POST['software'];
$monitorings=$_POST['monitoring'];
$prices=$_POST['price'];
$bundled=$_POST['bundled'];
$SPDIF=$_POST['SPDIF'];
$AES=$_POST['AES'];
$ADAT=$_POST['ADAT'];
$MAC=$_POST['MAC'];
$PC=$_POST['PC'];
echo "We used the advanced form";
} else {
// define variables from session variables
$input=intval($_SESSION['inputs']);
$preamp= intval($_SESSION['preamps']);
$connections=$_SESSION['connection'];
$hizinputs= intval($_SESSION['hizinput']);
$midis=$_SESSION['midi'];
$softwares=$_SESSION['software'];
$monitorings=$_SESSION['monitoring'];
$prices=$_SESSION['price'];
$bundled=$_SESSION['bundled'];
$SPDIF=$_SESSION['SPDIF'];
$AES=$_SESSION['AES'];
$ADAT=$_SESSION['ADAT'];
$MAC=$_SESSION['MAC'];
$PC=$_SESSION['PC'];
echo "We used the wizard tutoral thingy";
}
//Get the page number to show
/* set the allowed order by columns */
$page=$_GET["page"];
$default_sort = 'price';
$allowed_order = array ('brand', 'model','inputs', 'preamps', 'connection', 'hizinput', 'midi', 'software', 'monitoring', 'price', 'PC', 'Mac', 'SPDIF', 'AES', 'ADAT', );
/* if order is not set, or it is not in the allowed
* list, then set it to a default value. Otherwise,
* set it to what was passed in. */
if (!isset ($_GET['order']) ||
!in_array ($_GET['order'], $allowed_order)) {
$order = $default_sort;
} else {
$order = $_GET['order'];
}
/* test */
$direction = @$_GET['direction'];
if ($direction !== "DESC") $direction = "ASC";
/* connect to db */
edited
/* setup math for limit */
If($page == "") $page=1; //If no page number is set, the default page is 1
$Pagelimit=($page-1)*$Limit;
echo "$Pagelimit is the pagelimit";
$Limit = 5; //Number of results per page
/* construct and run our query */
$query = "SELECT * FROM test5 WHERE inputs >= $input AND
preamps >= $preamp AND hizinput >= $hizinputs AND connection LIKE '$connections%' AND midi LIKE '$midis%' AND bundled LIKE'$bundled%' AND monitoring LIKE '$monitorings%' AND price LIKE '$prices%' AND SPDIF LIKE '$SPDIF%' AND AES LIKE '$AES%' AND ADAT LIKE '$ADAT%' AND MAC LIKE '$MAC%' AND PC LIKE '$PC%' ORDER BY $order $direction LIMIT $Pagelimit,$Limit";
$result = mysql_query ($query);
/* make sure data was retrieved */
$numrows = mysql_num_rows($result);
if ($numrows == 0) {
echo "No data to display!";
exit;
}
/* calculate pages */
$NumberOfPages=ceil($numrows/$Limit);
echo "<br>Number of pages: $NumberOfPages";
/* now grab the first row and start the table */
$row = mysql_fetch_assoc ($result);
echo "<TABLE border=1>\n";
echo "<TR>\n";
foreach ($row as $heading=>$column) {
/* check if the heading is in our allowed_order
* array. If it is, hyperlink it so that we can
* order by this column */
if (in_array ($heading, $allowed_order)) {
echo "<TD><b><a href=\"{$_SERVER['PHP_SELF']}?order=$heading&direction=7\">$heading</a> </b></TD>";
} else { }
echo "\n";
}
echo "</TR>\n";
/* reset the $result set back to the first row and
* display the data */
mysql_data_seek ($result, 0);
while ($row = mysql_fetch_assoc ($result)) { ?>
<?php
if(($i % 2) == 1) //odd
{echo "<tr class=\"td1\">";}
else //even
{echo "<tr class=\"td2\">";}
?>
<td><?php echo $row['brand'] ?></td>
<td><h5><a href="product.php?id=<?php echo $row['id'] ?>"><?php echo $row['model'] ?></a> </h5></td>
<td bgcolor=""><? echo $row['inputs']; ?></td>
<td><? echo $row['preamps']; ?></td>
<td><? echo $row['hizinput']; ?></td>
<td><? echo $row['connection']; ?></td>
<td><? echo $row['midi']; ?></td>
<td><? echo $row['software']; ?></td>
<td><? echo $row['monitoring']; ?></td>
<td><? echo $row['price']; ?></td>
<td><? echo $row['PC']; ?></td>
<td><? echo $row['Mac']; ?></td>
<td><? echo $row['SPDIF']; ?></td>
<td><? echo $row['AES']; ?></td>
<td><? echo $row['ADAT']; ?></td>
<? $i = $i + 1;
?>
</tr> <? }
echo "</TABLE>\n";
?>
</body>