Jump to content

[SOLVED] Simple Sort ascending descending buttons


m00ch0

Recommended Posts

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 |

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 ?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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  :-[

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.