Forum Moderators: coopster
Here is my code (personal data removed):
<?php
$hostname_LL = "#*$!#*$!x";
$database_LL = "#*$!#*$!x";
$username_LL = "#*$!#*$!x";
$password_LL = "#*$!#*$!x";
$LL = mysql_pconnect($hostname_LL, $username_LL, $password_LL) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_LL, $LL);
$query_sv = "SELECT * FROM saves ORDER BY date ASC";
$sv = mysql_query($query_sv, $LL) or die(mysql_error());
$row_sv = mysql_fetch_assoc($sv);
$totalRows_sv = mysql_num_rows($sv);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Customer Saves Records</title>
<style type="text/css">
<!--
.style3 {font-size: 12px; font-family: Arial, Helvetica, sans-serif; }
.style5 {font-size: 12px; font-family: Arial, Helvetica, sans-serif; font-weight: bold; }
.style6 {
font-size: 14px;
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
}
-->
</style>
</head>
<body>
<p class="style6">Customer Saves Records</p>
<table width="800" border="0">
<tr>
<td><span class="style5">Date</span></td>
<td><span class="style5">Employee</span></td>
<td><span class="style5">Service</span></td>
<td><span class="style5">Account #</span></td>
<td><span class="style5">Reason for cancel</span></td>
<td><span class="style5">Reason Code</span></td>
<td><span class="style5">Saved</span></td>
<td><span class="style5">Promo used</span></td>
<td><span class="style5">Value</span></td>
</tr>
<?php do { ?>
<tr>
<td height="25"><span class="style3"><?php echo $row_sv['date']; ?></span></td>
<td><span class="style3"><?php echo $row_sv['employee']; ?></span></td>
<td><span class="style3"><?php echo $row_sv['service']; ?></span></td>
<td><span class="style3"><?php echo $row_sv['account']; ?></span></td>
<td><span class="style3"><?php echo $row_sv['reason']; ?></span></td>
<td><span class="style3"><?php echo $row_sv['rcode']; ?></span></td>
<td><span class="style3"><?php echo $row_sv['saved']; ?></span></td>
<td><span class="style3"><?php echo $row_sv['promo']; ?></span></td>
<td><span class="style3"><?php echo $row_sv['value']; ?></span></td>
</tr>
<?php } while ($row_sv = mysql_fetch_assoc($sv)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($sv);
?>
Instead of this::
<td><span class="style5">Date</span></td>
You make it a link to the same page. So something like this::
<td><span class="style5"><a href="<? echo $_SERVER['PHP_SELF']."?orderby=Date";?>Date</a></span></td>
Once the user clicks on the link, he/she will be directed to the same page. You will then have something to retrieve the $_GET var to change your query.
Instead of this::
$query_sv = "SELECT * FROM saves ORDER BY date ASC";
You should have something like this.
if(isset($_GET['orderby'])){
$orderby = $_GET['orderby'];
$query_sv = "SELECT * FROM saves ORDER BY ".mysql_real_escape_string($orderby)." ASC";
}
//default query
else{
$query_sv = "SELECT * FROM saves ORDER BY date ASC";
}
Remember to check the $orderby variable for valid characters before running against the db.
if(isset($_GET['orderby'])){
$orderby = $_GET['orderby'];
$query_sv = "SELECT * FROM saves ORDER BY ".mysql_real_escape_string($orderby)." ASC";
}
//default query
else{
$query_sv = "SELECT * FROM saves ORDER BY date ASC";
}
to something like this ::
if(isset($_GET['sortby'])){
$sortby = $_GET['sortby'];
if($strcasecmp($sortby, "desc")==0){
$newsort = "ASC";
}
else{
$newsort = "DESC";
}
}
else{
$sortby = "ASC";
$newsort = "DESC";
}
if(isset($_GET['orderby'])){
$orderby = $_GET['orderby'];
$query_sv = "SELECT * FROM saves ORDER BY ".mysql_real_escape_string($orderby);if(strcasecmp($sortby, "desc")==0){
$query_sv.= " DESC";
}
else{
$query_sv.= " ASC";
}
}
//default query
else{
$query_sv = "SELECT * FROM saves ORDER BY date ASC";
}
Now change your links and give it another link parameter. So instead of this::
<td><span class="style5"><a href="<? echo $_SERVER['PHP_SELF']."?orderby=Date";?>Date</a></span></td>
You'll have this::
<td><span class="style5"><a href="<? echo $_SERVER['PHP_SELF']."?orderby=Date&sortby=$newsort";?>Date</a></span></td>
and this is the code for everything
<?php
$hostname_LL = "localhost";
$database_LL = "database";
$username_LL = "user";
$password_LL = "pass";
$LL = mysql_pconnect($hostname_LL, $username_LL, $password_LL) or trigger_error(mysql_error(),E_USER_ERROR);mysql_select_db($database_LL, $LL);
if(isset($_GET['sortby'])){
$sortby = $_GET['sortby'];
if(strcasecmp($sortby, "desc")==0){
$newsort = "ASC";
}
else{
$newsort = "DESC";
}
}
else{
$sortby = "ASC";
$newsort = "DESC";
}
if(isset($_GET['orderby'])){
$orderby = $_GET['orderby'];
$query_sv = "SELECT `Team`, `Record`, `At Home`, `Away`, `Division Record`, `Streak` FROM `statistics` ORDER BY ".mysql_real_escape_string($orderby);
if(strcasecmp($sortby, "desc")==0){
$query_sv.= " DESC";
}
else{
$query_sv.= " ASC";
}
}
//default query
else{
$query_sv = "SELECT `Team`, `Record`, `At Home`, `Away`, `Division Record`, `Streak` FROM `statistics` ORDER BY Team ASC"; }
$sv = mysql_query($query_sv, $LL) or die(mysql_error());
$row_sv = mysql_fetch_assoc($sv);
$totalRows_sv = mysql_num_rows($sv);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Stats</title>
</head>
<body>
<table width="800" border="0">
<tr>
<td><a href="<? echo $_SERVER['PHP_SELF']."?orderby=Team&sortby=$newsort";?>Team</a></td>
<td><a href="<? echo $_SERVER['PHP_SELF']."?orderby=Record&sortby=$newsort";?>Record</a></td>
<td><a href="<? echo $_SERVER['PHP_SELF']."?orderby=At Home&sortby=$newsort";?>At Home</a></td>
<td><a href="<? echo $_SERVER['PHP_SELF']."?orderby=Away&sortby=$newsort";?>Away</a></td>
<td><a href="<? echo $_SERVER['PHP_SELF']."?orderby=Division Record&sortby=$newsort";?>Division Record</a></td>
<td><a href="<? echo $_SERVER['PHP_SELF']."?orderby=Streak&sortby=$newsort";?>Streak</a></td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_sv['Team']; ?></td>
<td><?php echo $row_sv['Record']; ?></td>
<td><?php echo $row_sv['At Home']; ?></td>
<td><?php echo $row_sv['Away']; ?></td>
<td><?php echo $row_sv['Division Record']; ?></td>
<td><?php echo $row_sv['Streak']; ?></td>
</tr>
<?php } while ($row_sv = mysql_fetch_assoc($sv)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($sv);
?>
for some reason the code will not work showing the links..please help
[edited by: coopster at 3:08 pm (utc) on Sep. 2, 2008]
[edit reason] removed url TOS [webmasterworld.com] [/edit]
instead of this
<td><a href="<? echo $_SERVER['PHP_SELF']."?orderby=Streak&sortby=$newsort";?>Streak</a></td> i needed this
<td><a href="<? echo $_SERVER['PHP_SELF']."?orderby=Streak&sortby=$newsort";?>">Streak</a></td> Once agian thanks for everything this code rocks!