| | |
Please help me with sorting column heading
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
•
•
Dear All,
I am a new web, I would like all of you kindly help me with code of sorting column heading.
Thanks in advance!
Kindly regards,
Bunhok
php Syntax (Toggle Plain Text)
<? include("connection.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <? $searchquery = ""; //if you intend to add search to this page // columns that you can order by $orderbycols = array(); $orderbycols["fname"] = "first name"; $orderbycols["lname"] = "last name"; $orderbycols["phone"] = "phone"; $orderbycols["username"] = "user name"; $orderbycols["address"] = "address"; // columns that are dificult to order by $nonorderbycols = array(); $nonorderbycols["hobbies"] = "favorite hobbies"; $nonorderbycols["signature"] = "signature"; // merge the column names for the query $querysel = array_merge($orderbycols, $nonorderbycols); // declare order by variable $orderby = ""; if(isset($_GET['order']) && in_array(strtolower($_GET['order']), array_keys($orderbycols))) { $orderby .= " order by '" . strtolower($_GET['order']) . "'"; // if we are ordering by desc values if(isset($_GET['desc']) && $_GET['desc'] == "true") { $orderby .= " desc"; } } // main query $query = "select " . implode(", ", array_keys($querysel)) . " from table inner join other_table using(colname_pk)" . $searchquery . $orderby; $result = mysql_query($query); ?> <body> <table> <tr> <? // columns that can be ordered foreach($orderbycols as $key=>$value) { ?> <td><? $hreforder = ""; if(trim($orderby) != "" && strtolower($_GET['order']) == $key) { $hreforder = isset($_GET['desc']) && $_GET['desc'] == "true"?"":"&desc=true"; } ?><a href="<? echo basename($_SERVER['PHP_SELF']); ?>?order=<? echo $key . $hreforder; ?>"><? echo ucwords($value); ?></a></td> <? } ?> <? // columns that cannot be ordered foreach($nonorderbycols as $key=>$value) { ?> <td><? echo ucwords($value); ?></td> <? } ?> </tr> <? // display database loop here ?> </table> </body> </html> <? mysql_close(); ?>
Last edited by R0bb0b; Sep 28th, 2008 at 1:16 am.
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
•
•
Join Date: Sep 2008
Posts: 6
Reputation:
Solved Threads: 0
Dear sir,
It still has an error with this code.
It appears like this.
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\working\sorting.php on line 74
What should we use loop? with while right?
Thanks so much for supporting me.
Kindly regards,
Bunhok
It still has an error with this code.
It appears like this.
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\working\sorting.php on line 74
What should we use loop? with while right?
•
•
•
•
PHP Syntax (Toggle Plain Text)
<? // display database loop here while($item = mysql_fetch_array($result)) ?> <tr><td><?=$item['Jobid']?>"></td></tr>
Kindly regards,
Bunhok
Did you modify the code I provided to match your database?
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
•
•
Join Date: Sep 2008
Posts: 6
Reputation:
Solved Threads: 0
Dear sir,
I modify ready but not sorting the databse now it display connectly
I modify ready but not sorting the databse now it display connectly
•
•
•
•
<? require("Dbconnect/db_connect.php"); ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<?
$searchquery = ""; //if you intend to add search to this page
// columns that you can order by
$orderbycols = array();
$orderbycols["Jobtitle"] = "Job Title";
$orderbycols["Jobcompany"] = "Jobcompany";
// declare order by variable
$orderby = "";
if(isset($_GET['order']) && in_array(strtolower($_GET['order']), array_keys($orderbycols)))
{
$orderby .= " order by '" . strtolower($_GET['order']) . "'";
// if we are ordering by desc values
if(isset($_GET['desc']) && $_GET['desc'] == "true")
{
$orderby .= " desc";
}
}
// main query
$query = "select " . implode(", ", array_keys($orderbycols)) . " from tbljob" . $searchquery . $orderby;
$result = mysql_query($query);
?>
<body>
<table>
<tr>
<?
// columns that can be ordered
foreach($orderbycols as $key=>$value)
{
?>
<td><?
$hreforder = "";
if(trim($orderby) != "" && strtolower($_GET['order']) == $key)
{
$hreforder = isset($_GET['desc']) && $_GET['desc'] == "true"?"":"&desc=true";
}
?><a href="<? echo basename($_SERVER['PHP_SELF']); ?>?order=<? echo $key . $hreforder; ?>"><? echo ucwords($value); ?></a></td>
<?
}
?>
</tr>
<?
$sql = "SELECT * FROM tbljob order by Jobid desc";
$result = mysql_query($sql);
while($item = mysql_fetch_array($result))
{
$bgc=($bgc=="#F7F7F7")?"#ffffff" : "#F7F7F7";
?>
<tr><td>
<?=$item['Jobtitle']?>
</tr>
<? }
?>
</body>
</html>
<?
mysql_close();
?>
This works, didn't realize that you would have capitals in your column names.
php Syntax (Toggle Plain Text)
<? if($dbconn = mysql_connect('servername', 'username', 'pass')) { mysql_select_db('dbname', $dbconn); } ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <? $searchquery = ""; //if you intend to add search to this page // columns that you can order by $orderbycols = array(); $orderbycols["Jobtitle"] = "Job Title"; $orderbycols["Jobcompany"] = "Jobcompany"; // declare order by variable $orderby = ""; if(isset($_GET['order']) && in_array($_GET['order'], array_keys($orderbycols))) { $orderby .= " order by " . strtolower($_GET['order']); // if we are ordering by desc values if(isset($_GET['desc']) && $_GET['desc'] == "true") { $orderby .= " desc"; } } // main query $query = "select " . implode(", ", array_keys($orderbycols)) . " from tbljob" . $searchquery . $orderby; echo $query . "<br />"; $result = mysql_query($query); ?> <body> <table> <tr> <? // columns that can be ordered foreach($orderbycols as $key=>$value) { ?> <td><? $hreforder = ""; if(trim($orderby) != "" && $_GET['order'] == $key) { $hreforder = isset($_GET['desc']) && $_GET['desc'] == "true"?"":"&desc=true"; } ?><a href="<? echo basename($_SERVER['PHP_SELF']); ?>?order=<? echo $key . $hreforder; ?>"><? echo ucwords($value); ?></a></td> <? } ?> </tr> <? $bgc = ""; while($item = mysql_fetch_array($result)) { $bgc=($bgc=="#F7F7F7")?"#ffffff" : "#F7F7F7"; ?> <tr> <td><?=$item['Jobtitle']?></td> <td><?=$item['Jobcompany']?></td> </tr> <? } ?> </body> </html> <? mysql_close(); ?>
“Be who you are and say what you feel because those who mind don't matter and those who matter don't mind.” - Dr. Seuss
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
-- The documentation is inevitable, you may get away with it for a little while but eventually you too will have to do the deed.
![]() |
Similar Threads
- Open In New Window Php (PHP)
- Sorting by dates (MySQL)
- Collapsible table sorting problem (JavaScript / DHTML / AJAX)
- javascript works in IE but not working in firefox (JavaScript / DHTML / AJAX)
- can anyone make this tree table sort correctly? (JavaScript / DHTML / AJAX)
- help in c++ program (C++)
- another one...selecing rows in datagrid and sorting with column headers (VB.NET)
Other Threads in the PHP Forum
- Previous Thread: Ternary Operator
- Next Thread: Passing variable sql php
| Thread Tools | Search this Thread |
.htaccess access alexa apache api array beginner binary broken cakephp checkbox class cms code convert cron curl database date directory display dropdown dynamic echo email encode error fairness file files folder form forms function functions google hack href htaccess html htmlspecialchars image include indentedsubcategory insert ip javascript joomla limit link login mail mail() menu methods mlm multiple multipletables mysql network newsletters object oop passwords paypal pdf php problem provider query radio random recursion redirect remote script search secure securephp server sessions simple sms source space sql syntax system table tutorial update upload url user validator variable video voteup web youtube





