Jump to content

Sorting columns


wooowooo

Recommended Posts

Hi

 

Below is the code I have developed to pull recorts from my customer database and sort them assendingly by the user clicking onto the column heading however I would like to make it so they can choose weather to view them assendingly or decendingly - I know that I should add a link and use the GET method so the title and weather its assending or desending but I dont know how to implement it into my code, could anyone show me?

 

Many thanks, im new to php and not all that good!

 

<?php
$default_sort = 'CustomerID';
$allowed_order = array ('CustomerID','Name', 'Address','Postcode','Telephone','Email');

if (!isset ($_GET['order']) || 
    !in_array ($_GET['order'], $allowed_order)) {
    $order = $default_sort;
} else {
    $order = $_GET['order'];
}

// connect to db



$query = "SELECT * FROM table ORDER BY $order";
$result = mysql_query ($query);


$numrows = mysql_num_rows($result);
if ($numrows == 0) {
    echo "No data to display!";
    exit;
}


$row = mysql_fetch_assoc ($result);
echo "<TABLE border=1>Click on a column heading to sort the column assendingly <p> \n";
echo "<TR>\n";
foreach ($row as $heading=>$column) {
    
    echo "<TD><b>";
    if (in_array ($heading, $allowed_order)) {
        echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading\">$heading</a>";
    } else {
        echo $heading;
    }                
    echo "</b></TD>\n";
}
echo "</TR>\n";


mysql_data_seek ($result, 0);
while ($row = mysql_fetch_assoc ($result)) {
    echo "<TR>\n";
    foreach ($row as $column) {
        echo "<TD>$column</TD>\n";
    }
    echo "</TR>\n";
}
echo "</TABLE>\n";
?>


Link to comment
https://forums.phpfreaks.com/topic/83429-sorting-columns/
Share on other sites

<?php
// url format  index.php?sort=1&by=Name
$default_sort = 'CustomerID';
$allowed_order = array ('CustomerID','Name', 'Address','Postcode','Telephone','Email');

if (in_array($_GET['by'], $allowed_order)){
$order =($_GET['sort']==1)?'asc':'desc';
$val = $_GET['by'].'  '.$order;
}
else{
$val = $default_sort.'  desc';
}
$query = "SELECT * FROM table ORDER BY $val";
$result = mysql_query ($query);


$numrows = mysql_num_rows($result);
if ($numrows == 0) {
    echo "No data to display!";
    exit;
}


$row = mysql_fetch_assoc ($result);
echo "<TABLE border=1>Click on a column heading to sort the column assendingly <p> \n";
echo "<TR>\n";
foreach ($row as $heading=>$column) {
    
    echo "<TD><b>";
    if (in_array ($heading, $allowed_order)) {
        echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading\">$heading</a>";
    } else {
        echo $heading;
    }                
    echo "</b></TD>\n";
}
echo "</TR>\n";


mysql_data_seek ($result, 0);
while ($row = mysql_fetch_assoc ($result)) {
    echo "<TR>\n";
    foreach ($row as $column) {
        echo "<TD>$column</TD>\n";
    }
    echo "</TR>\n";
}
echo "</TABLE>\n";
?>

maybe

Link to comment
https://forums.phpfreaks.com/topic/83429-sorting-columns/#findComment-424462
Share on other sites

I was going to ask you if my post the other day works (http://www.phpfreaks.com/forums/index.php/topic,173931.msg769601.htm), but then I realized that when I posted that the other day, I had been working in one file for multiple scripts with exit statements, and I managed to paste the entire thing x.x.  Try that script above the first exit.

 

Ehhh just to clarifiy, I'll repost it...

 

<?php

$default_sort = 'CustomerID';
$allowed_order = array ('CustomerID','Name', 'Address','Postcode','Telephone','Email');

if (!isset ($_GET['order']) || 
    !in_array ($_GET['order'], $allowed_order)) {
    $order = $default_sort;
} else {
    $order = $_GET['order'];
}


mysql_connect ('localhost','user','pass');
mysql_select_db ('database');


$ad = (isset($_GET['ad']) && $_GET['ad'] == 'desc') 'desc' : 'asc';
$query = "SELECT * FROM table ORDER BY $order $ad";
$result = mysql_query ($query);


$numrows = mysql_num_rows($result);
if ($numrows == 0) {
    echo "No data to display!";
    exit;
}


$row = mysql_fetch_assoc ($result);
echo "<TABLE border=1>Click on a column heading to sort the column assendingly <p> \n";
echo "<TR>\n";
foreach ($row as $heading=>$column) {
        echo "<TD>";
    if (in_array ($heading, $allowed_order)) {
	$tad = ($ad == 'asc' && $order == $heading) ? 'desc' : 'asc';
        echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading&ad=$tad\">$heading[/url]";
    } else {
        echo $heading;
    }                
    echo "</TD>\n";
}
echo "</TR>\n";

mysql_data_seek ($result, 0);
while ($row = mysql_fetch_assoc ($result)) {
    echo "<TR>\n";
    foreach ($row as $column) {
        echo "<TD>$column</TD>\n";
    }
    echo "</TR>\n";
}
echo "</TABLE>\n";

Link to comment
https://forums.phpfreaks.com/topic/83429-sorting-columns/#findComment-424471
Share on other sites

Archived

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

×
×
  • 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.