Jump to content

Archived

This topic is now archived and is closed to further replies.

ashish4172

sorting thru column header in php

Recommended Posts

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

Share this post


Link to post
Share on other sites
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`)
)

[color=orange]-[/color][color=orange]-[/color]
[color=orange]-[/color][color=orange]-[/color] Dumping data for table `counter`
[color=orange]-[/color][color=orange]-[/color]

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

testing123.php
[code]<?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>[/code]

Share this post


Link to post
Share on other sites

×

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.