Jump to content


Photo

sorting thru column header in php


  • Please log in to reply
1 reply to this topic

#1 ashish4172

ashish4172
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 26 June 2006 - 09:02 AM

hi!

there is a table with 4 columns.I would like to implement sorting asc and desc on click of col headers using php.

how can i do it????
any idea
most welcome


thanks
ashish

#2 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 26 June 2006 - 12:54 PM

I'm not good at explaination, hope code will do the explaination.
Below is what I just scripted, hope this helps you.

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--]CREATE TABLE `counter` (
`id` int(5) NOT NULL auto_increment,
`FileName` varchar(50) NOT NULL,
`FileURL` varchar(255) NOT NULL,
`Count` int(5) NOT NULL,
PRIMARY KEY (`id`)
)

--
-- Dumping data for table `counter`
--

[span style=\'color:blue;font-weight:bold\']INSERT[/span] INTO `counter` VALUES (1, 'File name 1', '', 10);
[span style=\'color:blue;font-weight:bold\']INSERT[/span] INTO `counter` VALUES (2, 'File name 2', '', 20);
[span style=\'color:blue;font-weight:bold\']INSERT[/span] INTO `counter` VALUES (3, 'File name 3', '', 30);
[span style=\'color:blue;font-weight:bold\']INSERT[/span] INTO `counter` VALUES (4, 'File name 4', '', 40); [!--sql2--][/div][!--sql3--]

testing123.php
<?php
$host = "localhost";
$username = "root";
$password = "";
$db = "testing123";

mysql_connect($host,$username,$password) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());

$type = $_GET['type'];
$order = $_GET['order'];

if ($order == 'asc') {
    $orderin = 'desc';
    } elseif ($order == 'desc') {
        $orderin = 'asc';
        } else {
            $orderin = 'asc';
            }

?>
<html>
<head>
</head>
<body>
                <table width="234" border="1">
                  <tr>
                  <?php
                    echo "<td width='55'><div align='center'><strong><a href='testing123.php?type=id&order=$orderin'>Index</a></strong></div></td>";
                    echo "<td width='70'><div align='center'><strong><a href='testing123.php?type=FileName&order=$orderin'>File Name</a></strong></div></td>";
                    echo "<td width='55'><div align='center'><strong><a href='testing123.php?type=count&order=$orderin'>Count</a></strong></div></td>";
                    echo "<td width='55'><div align='center'><strong><a href='testing123.php?type=FileURL&order=$orderin'>Download</a></strong></div></td>";
                    ?>
                  </tr>
                  <?php
                  if (isset($type)) {
                      if (isset($order)) {
                  $query = "SELECT * FROM counter ORDER BY $type $order";
                      } else {
                        $query = "SELECT * FROM counter ORDER BY $type";
                        }
                  } else {
                  $query = "SELECT * FROM counter ORDER BY id DESC";
                  }
                  $result = mysql_query($query) or die("Query failed: " . mysql_error());
                  while ($row=mysql_fetch_array($result))
                  {
                      $id = $row['id'];
                    $FileName = $row['FileName'];
                    $count = $row['Count'];
                    $FileURL = $row['FileURL'];
                  echo "<tr>";
                      echo "<td><div align='center'>$id</div></td>";
                    echo "<td><div align='center'>$FileName</div></td>";
                    echo "<td><div align='center'>$count</div></td>";
                    echo "<td><div align='center'>Click Here</div></td>";
                  echo "</tr>";
                  }
                  ?>
                </table>
</body>
</html>


Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users