Jump to content

And Desending?


wooowooo

Recommended Posts

Anyone know how I can modify the code below so you can choice weather to sort assendlingly or dessendingly? I have tried a few things such as adding "up" and "down" arrows but cannt seem to do it :( Im not all that good at php as you may have guessed!

 

<code>

 

<?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');

 

 

$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";

?>

 

</code>

 

Many thanks

Link to comment
https://forums.phpfreaks.com/topic/82949-and-desending/
Share on other sites

$query = "SELECT * FROM table ORDER BY $order";

 

To make it srt ASC it would simply be:

 

$query = "SELECT * FROM table ORDER BY $order ASC";

 

And descending would simply be

$query = "SELECT * FROM table ORDER BY $order DESC";

 

....  So... you could do something like:

 

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

Link to comment
https://forums.phpfreaks.com/topic/82949-and-desending/#findComment-421881
Share on other sites

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";
}

 

Should work.

Link to comment
https://forums.phpfreaks.com/topic/82949-and-desending/#findComment-421905
Share on other sites

mysql_data_seek ($result, 0);

while ($row = mysql_fetch_assoc ($result)) {

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

}

 

    foreach ($row as $column) {

        echo "<TD>$column</TD>\n";

    }

    echo "</TR>\n";

}

echo "</TABLE>\n";

?>

 

Is that correct because it still wont work :(

Link to comment
https://forums.phpfreaks.com/topic/82949-and-desending/#findComment-421915
Share on other sites

<?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";


exit;


$orig_array = array('corbin', 'corbin', 'john', 'mark', 'mark', 'paul', 'paul', 'mary', 'mary', 'mary');
$count_array = array();
foreach($orig_array as $v) {
     (isset($count_array[$v])) ? $count_array[$v]++ : $count_array[$v] = 1;
}
print_r($count_array);

exit;

if (eregi('http:', $question)) {
die ("Please enter question");
}
if(!$email == "" && (!strstr($email,"@") || !strstr($email,"."))) {
echo "<h2>Please enter valid email</h2>\n";
$badinput = "<h2>Feedback was NOT submitted</h2>\n";
echo $badinput;
die ("Go back! ! ");
}

if(empty($firstname) || empty($email) || empty($question )) {
echo "<h2>Please fill in all fields</h2>\n";
die ("Use back! ! ");
}

$showform = true;
$error = '';

if($_POST) {
//pretend $question, $email and $firstname are defined
if(eregi('http:', $question)) {
	$error = 'Please enter question';
}
elseif(empty($email) || empty($firstname) || empty($question)) {
	$error = 'Please fill in all fields.';
}
elseif(!strstr($email, '@') || !strstr($email, '.')) {
	$error = 'Please enter a valid email.';
}
else {
	//success!!!!
	$showform = false;
}
}
else {
$question = $email = $firstname = '';
}

if($showform != false) {
if(!empty($error)) {
	echo $error;
}
echo <<<HERE
<!-- form stuff here -->
HERE;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/82949-and-desending/#findComment-421932
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.