topflight Posted January 30, 2009 Share Posted January 30, 2009 I am curious how can I order a table by information for instance if I have a roster page and the user clicks on ID I want it to order the table by the ID how can I do that this is one of my codes please help. <style title="text/css"> .rostertxt { font-face: BankGothic Md BT; font-size: 12; color: #0076C8; } .style8 {font-size: 12px} </style> <font class="rostertxt"> <table width="100%" height="48" align="left"> <tr bgcolor="#D3D3D3"> <td width="139"><span class="style8">PID</span></td> <td width="209"><span class="style8">Pilot Name</span></td> <td width="248"><span class="style8">Rating</span></td> <td width="285"><span class="style8">Hub</span></td> </tr> <? include 'db.php'; ?> <?php if($_GET[hub]){$plist = mysql_query("SELECT * FROM pilots WHERE status <'2' AND hub='$_GET[hub]' ORDER BY login ASC");} else { $plist = mysql_query("SELECT * FROM `pilots` WHERE status < '2' ORDER BY login ASC"); } $plrows = mysql_num_rows($plist); if($plrows<'0'){ echo'No Pilots Are Currently In Database'; } else { echo "<font class=navtxt><b>Pilot Roster:: We Currently have $plrows Pilots </B></font><br>"; while($data = mysql_fetch_assoc($plist)){ $hub = $data["hub"]; ?> <tr bgcolor="#EEEEEE" > <td><span class="style8"><a href="?page=profile&login=<? echo"{$data["login"]}"?>"><? echo "ASA$data[login]"; ?></td> <td><span class="style8"> <?php = "$data[fname]"; ?> <?= "$data[lname]"; ?> </span></td> <td><span class="style8"> <?php = "$data[rating]"; ?> </span></td> <td><span class="style8"> <?php = ""; if($hub=='KSEA'){echo'Seattle';}elseif($hub=='KPDX'){echo'Portland';}elseif($hub=='KLAX'){echo'Las Angles';}elseif($hub=='PANC'){echo'Anchorage';}?> </span></td> </tr> <?php } ?> <?php } ?> </table> </font> Link to comment https://forums.phpfreaks.com/topic/143063-how-to-order-a-table-by-information/ Share on other sites More sharing options...
ShoeLace1291 Posted January 30, 2009 Share Posted January 30, 2009 Try this code. Then add an anchor that links to something like page.php?hub=$var&sort=name Change any table fields necessary. <style title="text/css"> .rostertxt { font-face: BankGothic Md BT; font-size: 12; color: #0076C8; } .style8 {font-size: 12px} </style> <font class="rostertxt"> <table width="100%" height="48" align="left"> <tr bgcolor="#D3D3D3"> <td width="139"><span class="style8">PID</span></td> <td width="209"><span class="style8">Pilot Name</span></td> <td width="248"><span class="style8">Rating</span></td> <td width="285"><span class="style8">Hub</span></td> </tr> <? include 'db.php'; ?> <?php if($_GET['sort']){ $sort = $_GET['sort']; } else { $sort = "login"; //This is your default } if($_GET[hub]){$plist = mysql_query("SELECT * FROM pilots WHERE status <'2' AND hub='$_GET[hub]' ORDER BY ".$sort." ASC");} else { $plist = mysql_query("SELECT * FROM `pilots` WHERE status < '2' ORDER BY ".$sort." ASC"); } $plrows = mysql_num_rows($plist); if($plrows<'0'){ echo'No Pilots Are Currently In Database'; } else { echo "<font class=navtxt><b>Pilot Roster:: We Currently have $plrows Pilots </B></font><br>"; while($data = mysql_fetch_assoc($plist)){ $hub = $data["hub"]; ?> <tr bgcolor="#EEEEEE" > <td><span class="style8"><a href="?page=profile&login=<? echo"{$data["login"]}"?>"><? echo "ASA$data[login]"; ?></td> <td><span class="style8"> <?php = "$data[fname]"; ?> <?= "$data[lname]"; ?> </span></td> <td><span class="style8"> <?php = "$data[rating]"; ?> </span></td> <td><span class="style8"> <?php = ""; if($hub=='KSEA'){echo'Seattle';}elseif($hub=='KPDX'){echo'Portland';}elseif($hub=='KLAX'){echo'Las Angles';}elseif($hub=='PANC'){echo'Anchorage';}?> </span></td> </tr> <?php } ?> <?php } ?> </table> </font> Link to comment https://forums.phpfreaks.com/topic/143063-how-to-order-a-table-by-information/#findComment-750293 Share on other sites More sharing options...
phparray Posted January 30, 2009 Share Posted January 30, 2009 topflight Hybrid Kill3r's answer should work for you. To learn from this study the sql statements. He has added ORDER BY ".$sort." ASC" to them. The order by clause will sort the selected rows on the mysql database before the information is returned. To go in reverse order you would use DESC in the place of ASC. Link to comment https://forums.phpfreaks.com/topic/143063-how-to-order-a-table-by-information/#findComment-750304 Share on other sites More sharing options...
Prismatic Posted January 30, 2009 Share Posted January 30, 2009 never ever ever ever ever take user input and directly insert it into a query. <style title="text/css"> .rostertxt { font-face: BankGothic Md BT; font-size: 12; color: #0076C8; } .style8 {font-size: 12px} </style> <font class="rostertxt"> <table width="100%" height="48" align="left"> <tr bgcolor="#D3D3D3"> <td width="139"><span class="style8">PID</span></td> <td width="209"><span class="style8">Pilot Name</span></td> <td width="248"><span class="style8">Rating</span></td> <td width="285"><span class="style8">Hub</span></td> </tr> <?php include 'db.php'; if($_GET['sort']) { $sort = $_GET['sort']; } else { $sort = "login"; //This is your default } if($_GET['hub']) { $plist = mysql_query("SELECT * FROM pilots WHERE status <'2' AND hub='". mysql_real_escape_string($_GET['hub']) ."' ORDER BY ". mysql_real_escape_string($sort) ." ASC"); } else { $plist = mysql_query("SELECT * FROM `pilots` WHERE status < '2' ORDER BY ". mysql_real_escape_string($sort) ." ASC"); } $plrows = mysql_num_rows($plist); if($plrows<'0') { echo'No Pilots Are Currently In Database'; } else { echo "<font class=navtxt><b>Pilot Roster:: We Currently have $plrows Pilots </B></font><br>"; while($data = mysql_fetch_assoc($plist)) { $hub = $data["hub"]; ?> <tr bgcolor="#EEEEEE" > <td><span class="style8"><a href="?page=profile&login=<? echo $data["login"] ?>"><? echo "ASA". $data['login']; ?></td> <td><span class="style8"><?php echo $data['fname'] ." ". $data['lname']; ?></span></td> <td><span class="style8"><?php echo $data['rating']; ?></span></td> <td><span class="style8"> <?php if($hub=='KSEA') { echo'Seattle'; } elseif($hub=='KPDX') { echo'Portland'; } elseif($hub=='KLAX') { echo'Las Angles'; } elseif($hub=='PANC') { echo'Anchorage'; } ?> </span></td> </tr> <?php } ?> <?php } ?> </table> </font> Link to comment https://forums.phpfreaks.com/topic/143063-how-to-order-a-table-by-information/#findComment-750351 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.