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