m00ch0 Posted August 15, 2007 Share Posted August 15, 2007 I'm trying to find out if its possible to create a button to sort a certain column ascending and descending so you click the arrow pointing down it does descending and up for ascending \/ /\ \/ /\ \/ /\ \/ /\ \/ /\ | Scan | Manufacturer | Description | Code | Part Number | Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/ Share on other sites More sharing options...
php_tom Posted August 15, 2007 Share Posted August 15, 2007 Sure, make each arrow a link, so if someone clicks the down arrow over Code, it goes to index.php?sort=Code&order=desc Then when you do the SQL query, just slap in a "ORDER BY Code ASC" as figured out from the $_GET[] global. Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-324604 Share on other sites More sharing options...
m00ch0 Posted August 15, 2007 Author Share Posted August 15, 2007 sorry I dont understand ??? i'm just starting with all of this Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-324639 Share on other sites More sharing options...
MadTechie Posted August 15, 2007 Share Posted August 15, 2007 OK see you SQL statement ie "SELECT * FROM table" if you add "ORDER BY stuff ASC" thus becoming "SELECT * FROM table ORDER BY stuff ASC" the list will be in ascending order (by field stuff) so you have a hyperlink on the page on the stuff text ie <a href=mysite.com\page.php?sort='stuff'&order=ASC now you SQL statement can be chaged to "SELECT * FROM table ORDER BY ".$_GET['sort']." ".$_GET['order'] make sense so far ? Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-324644 Share on other sites More sharing options...
m00ch0 Posted August 15, 2007 Author Share Posted August 15, 2007 I think I see after some playin with it let me just check it out This may take awhile as the interface I've created will look strange if not done correctly Thanks for the help so far Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-324668 Share on other sites More sharing options...
php_tom Posted August 15, 2007 Share Posted August 15, 2007 MadTechie: <a href=mysite.com\page.php?sort='stuff'&order=ASC I would not do this, it opens you up to all kinds of SQL hacks. E.g., mysite.com\page.php?sort=stuff&order=ASC; DROP TABLE stuff; -- Instead, make the link one of <a href=mysite.com\page.php?sort=stuff&order=up <a href=mysite.com\page.php?sort=stuff&order=down Then in PHP: if($_GET['order'] == "up") SELECT * FROM TABLE ORDER BY stuff ASC elseif($_GET['order'] == "down") SELECT * FROM TABLE ORDER BY stuff DESC else die("Invalid parameter error!"); I think this is much more secure. Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-324706 Share on other sites More sharing options...
m00ch0 Posted August 15, 2007 Author Share Posted August 15, 2007 that may be a more secure method but it confuses me by how am I going to change what its ordering by each time? meaning the stuff? maybe this is the wrong way to go about it, whats the easiest way to sort different columns? Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-324730 Share on other sites More sharing options...
php_tom Posted August 15, 2007 Share Posted August 15, 2007 Presumably you have an SQL table with columns "| Scan | Manufacturer | Description | Code | Part Number |", and you're fetching rows from the table and displaying them. What you want is a button at the top of each column that will sort it by that column. Try this: <table> <tr> <td> <a href='thispage.php?sortby=Scan&sortorder=up'><img src='uparrow.jpg'></a> <a href='thispage.php?sortby=Scan&sortorder=down'><img src='downarrow.jpg'></a> </td> <td> <a href='thispage.php?sortby=Manufacturer&sortorder=up'><img src='uparrow.jpg'></a> <a href='thispage.php?sortby=Manufacturer&sortorder=down'><img src='downarrow.jpg'></a> </td> <td> <a href='thispage.php?sortby=Description&sortorder=up'><img src='uparrow.jpg'></a> <a href='thispage.php?sortby=Description&sortorder=down'><img src='downarrow.jpg'></a> </td> <td> <a href='thispage.php?sortby=Code&sortorder=up'><img src='uparrow.jpg'></a> <a href='thispage.php?sortby=Code&sortorder=down'><img src='downarrow.jpg'></a> </td> <td> <a href='thispage.php?sortby=PartNumber&sortorder=up'><img src='uparrow.jpg'></a> <a href='thispage.php?sortby=PartNumber&sortorder=down'><img src='downarrow.jpg'></a> </td> </tr> <tr> <td><b>Scan</b></td> <td><b>Manufacturer</b></td> <td><b>Description</b></td> <td><b>Code</b></td> <td><b>Part Number</b></td> </tr> <?php // First do mysql_connect(), mysql_select_db() if($_GET['sortby'] != "") $column = " ORDER BY ".$_GET['sortby']; else $column = ""; if($_GET['sortorder'] == "up") $ordr = " ASC"; if($_GET['sortorder'] == "down") $ordr = " DESC"; else $order = ""; $res = mysql_query("SELECT * FROM parts".$column.$order); while($row = mysql_fetch_row($res)) { for($i=0;$i<count($row);$i++) { echo "<td>".$row[$i]."</td>"; } } ?> </tr> </table> Hope that helps you. Post back if anything is unclear. Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-324750 Share on other sites More sharing options...
MadTechie Posted August 15, 2007 Share Posted August 15, 2007 yes its insecure but lets get the understanding done first.. then when that makes sense were secure it Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-324809 Share on other sites More sharing options...
m00ch0 Posted August 16, 2007 Author Share Posted August 16, 2007 trying to get "SELECT * FROM table ORDER BY ".$_GET['sort']." ".$_GET['order'] Parse error: syntax error, unexpected T_VARIABLE ------------- mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM table ORDER BY ".$_GET['sort']." ".$_GET['order'] $result=mysql_query($sql); $count=mysql_num_rows($result); ---------------- is that the correct way in which it should be Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-325656 Share on other sites More sharing options...
MadTechie Posted August 16, 2007 Share Posted August 16, 2007 Parse error: syntax error, unexpected T_VARIABLE is normally somehthing like ; thing ie $sql="SELECT * FROM table ORDER BY ".$_GET['sort']." ".$_GET['order']; Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-325661 Share on other sites More sharing options...
m00ch0 Posted August 16, 2007 Author Share Posted August 16, 2007 the more I add to my code the more errors I get I'm now getting what am i doing wrong it seems like it should work?!? Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/zendurl/public_html/m/xxxxx/sc_testing_with_delete_and_sort.php on line 36 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/zendurl/public_html/m/xxxxx/sc_testing_with_delete_and_sort.php on line 62 <html> <head> <title>Displaying MySQL Data</title> <link href="tables.css" rel="stylesheet" type="text/css"> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <style type="text/css"> <!-- body { background-color: #6cba00; } .style1 { font-family: Verdana; color: #391D4C; } --> </style> </head> <body> <p> <?php $host="localhost"; // Host name $username="user"; // Mysql username $password="pass"; // Mysql password $db_name="xxxxx"; // Database name $tbl_name="table"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql=$sql="SELECT * FROM table ORDER BY ".$_GET['sort']." ".$_GET['order']; $result=mysql_query($sql); $count=mysql_num_rows($result); ?> <table align="center" width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><table width="100%" border="0" cellspacing="3" cellpadding="0"> <tr> <td><img src="logo.gif"></td> </tr> </table> </td> </tr> <tr> <td> <form name="form1" method="post" action=""> <table border="0" cellpadding="2" cellspacing="2"> <tr> <td width="35" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana"> should be a arrow here a href="http://www.zendurl.com/xxxxx/sc_testing_with_delete_and_sort.php?sort='scan'&order=ASC">Scan</a></font></td> <td width="250" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Manufacturer</font></td> <td width="250" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Description </font></td> <td width="45" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Code</font></td> <td width="200" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Part Number</font></td> <td width="60" nowrap="nowrap" bgcolor="391d4c" align="center"><font color="#6cba00" size="1" face="Verdana">Delete</font></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr class="data"> <td><? echo $rows['scan']; ?></td> <td><? echo $rows['manufacturer']; ?></td> <td><? echo $rows['description']; ?></td> <td><? echo $rows['code']; ?></td> <td><? echo $rows['partno']; ?></td> <td align="center"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td> </tr> <?php } ?> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td align="center" class="data"><input name="delete" type="submit" id="delete" value="Delete"></td> </tr> <?php if(isset($_POST['delete'])) { foreach($_POST['checkbox'] as $row_id) { $sql = "DELETE FROM $tbl_name WHERE id='$row_id'"; $result = mysql_query($sql); if($result === FALSE) die("Error occured deleting row ".$row_id."!"); } echo "<meta http-equiv=\"refresh\" content=\"0;URL=sc_testing_with_delete.php\">"; } ?> </table> </form> <?php if (!isset($_POST['submit'])) { ?> <form action="" method="post"> <table border="0" cellpadding="2" cellspacing="2"> <tr> <td width="35" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Scan</font></td> <td width="250" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Manufacturer</font></td> <td width="250" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Description</font></td> <td width="45" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Code</font></td> <td width="200" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Part number</font></td> <td width="60" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Submit</font></td> </tr> <tr class="data"> <td ><font color="#6cba00" size="1" face="Verdana"><input name="scan" type="text" size="5" maxlength="4" class="data"></font></td> <td><font color="#6cba00" size="1" face="Verdana"><input name="manufacturer" type="text" class="data" size="40"> </font></td> <td><font color="#6cba00" size="1" face="Verdana"><input name="description" type="text" class="data" size="40"> </font></td> <td><font color="#6cba00" size="1" face="Verdana"><select name="code" class="data"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> </select></font></td> <td><font color="#6cba00" size="1" face="Verdana"><input name="partno" type="text" class="data" size="32"> </font></td> <td><font color="#6cba00" size="1" face="Verdana"><input type="submit" name="submit" value="Add Part" class="data"> </font></td> </tr> </table> </form> <?php } else { $scan = $_POST['scan']; $manufacturer = $_POST['manufacturer']; $description = $_POST['description']; $code = $_POST['code']; $partno = $_POST['partno']; mysql_query("INSERT INTO sc_parts (scan, manufacturer, description, code, partno) VALUES ('$scan', '$manufacturer', '$description', '$code', '$partno')"); echo "<br><p class=\"style1\">"; echo "The new part has been added to the database!!"; echo "<br>"; echo "<br>"; echo "<a href=\"sc_testing_with_delete.php\" title=\"Sc_testing_with_delete page\">Click here to add another part</a><br/>"; echo "</p>"; } ?> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-325686 Share on other sites More sharing options...
mmarif4u Posted August 16, 2007 Share Posted August 16, 2007 I think like this: Missing " at the end. $sql="SELECT * FROM table ORDER BY ".$_GET['sort']." ".$_GET['order']""; Other u r showing all the data from table using while loop, use 2 buttons and a form with it. But may it will be more complex so simple way is to use GET for type of situations, But remember make sure ur URL is going to be secure as mentioned before in a post that Such kind of url are open to SQL injections. Hope this will help. Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-325694 Share on other sites More sharing options...
m00ch0 Posted August 16, 2007 Author Share Posted August 16, 2007 $sql="SELECT * FROM table ORDER BY ".$_GET['sort']." ".$_GET['order']""; adding the two extra "" Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/zendurl/public_html/m/xxxxx/sc_testing_with_delete_and_sort.php on line 32 its definitly the $sql line as its 32 Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-325699 Share on other sites More sharing options...
mmarif4u Posted August 16, 2007 Share Posted August 16, 2007 Ah sori forgot to put . Try this: $sql="SELECT * FROM table ORDER BY ".$_GET['sort']." ".$_GET['order'].""; Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-325700 Share on other sites More sharing options...
m00ch0 Posted August 16, 2007 Author Share Posted August 16, 2007 I just keep getting error after error Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/zendurl/public_html/m/xxxxx/sc_testing_with_delete_and_sort.php on line 36 Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/zendurl/public_html/m/xxxxx/sc_testing_with_delete_and_sort.php on line 62 this is really starting to annoy me and I dont like asking you guys all the time and this is the easiest way to do it my god! Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-325704 Share on other sites More sharing options...
mmarif4u Posted August 16, 2007 Share Posted August 16, 2007 Can u post ur updated code. Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-325707 Share on other sites More sharing options...
m00ch0 Posted August 16, 2007 Author Share Posted August 16, 2007 <html> <head> <title>Displaying MySQL Data</title> <link href="tables.css" rel="stylesheet" type="text/css"> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <style type="text/css"> <!-- body { background-color: #6cba00; } .style1 { font-family: Verdana; color: #391D4C; } --> </style> </head> <body> <p> <?php $host="localhost"; // Host name $username="m00ch00"; // Mysql username $password="m00ch00"; // Mysql password $db_name="m00ch00_zendurl0"; // Database name $tbl_name="sc_parts"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM table ORDER BY ".$_GET['sort']." ".$_GET['order'].""; $result=mysql_query($sql); $count=mysql_num_rows($result); ?> <table align="center" width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><table width="100%" border="0" cellspacing="3" cellpadding="0"> <tr> <td><img src="logo.gif"></td> </tr> </table> </td> </tr> <tr> <td> <form name="form1" method="post" action=""> <table border="0" cellpadding="2" cellspacing="2"> <tr> <td width="35" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana"><a href="http://www.zendurl.com/m00ch00/sc_testing_with_delete_and_sort.php?sort='scan'&order=ASC">Scan</a></font></td> <td width="250" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Manufacturer</font></td> <td width="250" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Description </font></td> <td width="45" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Code</font></td> <td width="200" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Part Number</font></td> <td width="60" nowrap="nowrap" bgcolor="391d4c" align="center"><font color="#6cba00" size="1" face="Verdana">Delete</font></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr class="data"> <td><? echo $rows['scan']; ?></td> <td><? echo $rows['manufacturer']; ?></td> <td><? echo $rows['description']; ?></td> <td><? echo $rows['code']; ?></td> <td><? echo $rows['partno']; ?></td> <td align="center"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td> </tr> <?php } ?> <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td align="center" class="data"><input name="delete" type="submit" id="delete" value="Delete"></td> </tr> <?php if(isset($_POST['delete'])) { foreach($_POST['checkbox'] as $row_id) { $sql = "DELETE FROM $tbl_name WHERE id='$row_id'"; $result = mysql_query($sql); if($result === FALSE) die("Error occured deleting row ".$row_id."!"); } echo "<meta http-equiv=\"refresh\" content=\"0;URL=sc_testing_with_delete.php\">"; } ?> </table> </form> <?php if (!isset($_POST['submit'])) { ?> <form action="" method="post"> <table border="0" cellpadding="2" cellspacing="2"> <tr> <td width="35" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Scan</font></td> <td width="250" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Manufacturer</font></td> <td width="250" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Description</font></td> <td width="45" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Code</font></td> <td width="200" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Part number</font></td> <td width="60" nowrap="nowrap" bgcolor="391d4c"><font color="#6cba00" size="1" face="Verdana">Submit</font></td> </tr> <tr class="data"> <td ><font color="#6cba00" size="1" face="Verdana"><input name="scan" type="text" size="5" maxlength="4" class="data"></font></td> <td><font color="#6cba00" size="1" face="Verdana"><input name="manufacturer" type="text" class="data" size="40"> </font></td> <td><font color="#6cba00" size="1" face="Verdana"><input name="description" type="text" class="data" size="40"> </font></td> <td><font color="#6cba00" size="1" face="Verdana"><select name="code" class="data"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> </select></font></td> <td><font color="#6cba00" size="1" face="Verdana"><input name="partno" type="text" class="data" size="32"> </font></td> <td><font color="#6cba00" size="1" face="Verdana"><input type="submit" name="submit" value="Add Part" class="data"> </font></td> </tr> </table> </form> <?php } else { $scan = $_POST['scan']; $manufacturer = $_POST['manufacturer']; $description = $_POST['description']; $code = $_POST['code']; $partno = $_POST['partno']; mysql_query("INSERT INTO sc_parts (scan, manufacturer, description, code, partno) VALUES ('$scan', '$manufacturer', '$description', '$code', '$partno')"); echo "<br><p class=\"style1\">"; echo "The new part has been added to the database!!"; echo "<br>"; echo "<br>"; echo "<a href=\"sc_testing_with_delete.php\" title=\"Sc_testing_with_delete page\">Click here to add another part</a><br/>"; echo "</p>"; } ?> </td> </tr> </table> <p> </p> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-325713 Share on other sites More sharing options...
AndyB Posted August 16, 2007 Share Posted August 16, 2007 Surely $sql="SELECT * FROM table ORDER BY ".$_GET['sort']." ".$_GET['order'].""; should be $sql="SELECT * FROM $tbl_name ORDER BY ".$_GET['sort']." ".$_GET['order']; Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-325729 Share on other sites More sharing options...
m00ch0 Posted August 16, 2007 Author Share Posted August 16, 2007 changed the sql line but still same two errors appear Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-325746 Share on other sites More sharing options...
lightningstrike Posted August 16, 2007 Share Posted August 16, 2007 Perhaps you should echo the query back. Perhaps you are attempting to order by a non-existent column in your table. Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-325748 Share on other sites More sharing options...
m00ch0 Posted August 16, 2007 Author Share Posted August 16, 2007 I'm moving onto another part of my site at the moment I'm going to close this and say its solved as its not as important as a other features thanks for all your help guys anyway my bad code writting doesnt help at all Quote Link to comment https://forums.phpfreaks.com/topic/65045-solved-simple-sort-ascending-descending-buttons/#findComment-325791 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.