943,568 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2046
  • PHP RSS
Sep 27th, 2008
0

Please help me with sorting column heading

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bunhok is offline Offline
6 posts
since Sep 2008
Sep 28th, 2008
0

Re: Please help me with sorting column heading

Click to Expand / Collapse  Quote originally posted by Bunhok ...
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
I would start out with something like this. You might find some of this a little confusing, let me know if you any questions.
php Syntax (Toggle Plain Text)
  1. <? include("connection.php"); ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  6. <title>Untitled Document</title>
  7. </head>
  8. <?
  9. $searchquery = ""; //if you intend to add search to this page
  10.  
  11. // columns that you can order by
  12. $orderbycols = array();
  13. $orderbycols["fname"] = "first name";
  14. $orderbycols["lname"] = "last name";
  15. $orderbycols["phone"] = "phone";
  16. $orderbycols["username"] = "user name";
  17. $orderbycols["address"] = "address";
  18.  
  19. // columns that are dificult to order by
  20. $nonorderbycols = array();
  21. $nonorderbycols["hobbies"] = "favorite hobbies";
  22. $nonorderbycols["signature"] = "signature";
  23.  
  24. // merge the column names for the query
  25. $querysel = array_merge($orderbycols, $nonorderbycols);
  26.  
  27. // declare order by variable
  28. $orderby = "";
  29. if(isset($_GET['order']) && in_array(strtolower($_GET['order']), array_keys($orderbycols)))
  30. {
  31. $orderby .= " order by '" . strtolower($_GET['order']) . "'";
  32. // if we are ordering by desc values
  33. if(isset($_GET['desc']) && $_GET['desc'] == "true")
  34. {
  35. $orderby .= " desc";
  36. }
  37. }
  38.  
  39. // main query
  40. $query = "select " . implode(", ", array_keys($querysel)) . " from table inner join other_table using(colname_pk)" . $searchquery . $orderby;
  41. $result = mysql_query($query);
  42. ?>
  43.  
  44. <body>
  45. <table>
  46. <tr>
  47. <?
  48. // columns that can be ordered
  49. foreach($orderbycols as $key=>$value)
  50. {
  51. ?>
  52. <td><?
  53. $hreforder = "";
  54. if(trim($orderby) != "" && strtolower($_GET['order']) == $key)
  55. {
  56. $hreforder = isset($_GET['desc']) && $_GET['desc'] == "true"?"":"&desc=true";
  57. }
  58. ?><a href="<? echo basename($_SERVER['PHP_SELF']); ?>?order=<? echo $key . $hreforder; ?>"><? echo ucwords($value); ?></a></td>
  59. <?
  60. }
  61. ?>
  62. <?
  63. // columns that cannot be ordered
  64. foreach($nonorderbycols as $key=>$value)
  65. {
  66. ?>
  67. <td><? echo ucwords($value); ?></td>
  68. <?
  69. }
  70. ?>
  71. </tr>
  72. <?
  73. // display database loop here
  74. ?>
  75. </table>
  76. </body>
  77. </html>
  78. <?
  79. mysql_close();
  80. ?>
Last edited by R0bb0b; Sep 28th, 2008 at 1:16 am.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Oct 5th, 2008
0

Re: Please help me with sorting column heading

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?

Click to Expand / Collapse  Quote originally posted by R0bb0b ...
PHP Syntax (Toggle Plain Text)
  1. <?
  2.  
  3. // display database loop here
  4. while($item = mysql_fetch_array($result)) ?>
  5.  
  6. <tr><td><?=$item['Jobid']?>"></td></tr>
  7.  
Thanks so much for supporting me.

Kindly regards,

Bunhok
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bunhok is offline Offline
6 posts
since Sep 2008
Oct 5th, 2008
0

Re: Please help me with sorting column heading

Did you modify the code I provided to match your database?
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Oct 5th, 2008
0

Re: Please help me with sorting column heading

Dear sir,

I modify ready but not sorting the databse now it display connectly
Quote ...
<? 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();
?>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bunhok is offline Offline
6 posts
since Sep 2008
Oct 5th, 2008
0

Re: Please help me with sorting column heading

This works, didn't realize that you would have capitals in your column names.
php Syntax (Toggle Plain Text)
  1. <?
  2. if($dbconn = mysql_connect('servername', 'username', 'pass'))
  3. {
  4. mysql_select_db('dbname', $dbconn);
  5. }
  6. ?>
  7. <html xmlns="http://www.w3.org/1999/xhtml">
  8. <head>
  9. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  10. <title>Untitled Document</title>
  11. </head>
  12. <?
  13. $searchquery = ""; //if you intend to add search to this page
  14.  
  15. // columns that you can order by
  16. $orderbycols = array();
  17. $orderbycols["Jobtitle"] = "Job Title";
  18. $orderbycols["Jobcompany"] = "Jobcompany";
  19.  
  20. // declare order by variable
  21. $orderby = "";
  22. if(isset($_GET['order']) && in_array($_GET['order'], array_keys($orderbycols)))
  23. {
  24. $orderby .= " order by " . strtolower($_GET['order']);
  25. // if we are ordering by desc values
  26. if(isset($_GET['desc']) && $_GET['desc'] == "true")
  27. {
  28. $orderby .= " desc";
  29. }
  30. }
  31.  
  32. // main query
  33. $query = "select " . implode(", ", array_keys($orderbycols)) . " from tbljob" . $searchquery . $orderby;
  34. echo $query . "<br />";
  35. $result = mysql_query($query);
  36. ?>
  37.  
  38. <body>
  39. <table>
  40. <tr>
  41. <?
  42. // columns that can be ordered
  43. foreach($orderbycols as $key=>$value)
  44. {
  45. ?>
  46. <td><?
  47. $hreforder = "";
  48. if(trim($orderby) != "" && $_GET['order'] == $key)
  49. {
  50. $hreforder = isset($_GET['desc']) && $_GET['desc'] == "true"?"":"&desc=true";
  51. }
  52. ?><a href="<? echo basename($_SERVER['PHP_SELF']); ?>?order=<? echo $key . $hreforder; ?>"><? echo ucwords($value); ?></a></td>
  53. <?
  54. }
  55. ?>
  56.  
  57. </tr>
  58. <?
  59. $bgc = "";
  60. while($item = mysql_fetch_array($result))
  61. {
  62. $bgc=($bgc=="#F7F7F7")?"#ffffff" : "#F7F7F7";
  63. ?>
  64. <tr>
  65. <td><?=$item['Jobtitle']?></td>
  66. <td><?=$item['Jobcompany']?></td>
  67. </tr>
  68. <?
  69. }
  70.  
  71. ?>
  72. </body>
  73. </html>
  74. <?
  75. mysql_close();
  76. ?>
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Oct 6th, 2008
0

Re: Please help me with sorting column heading

Dear Mr. R0bb0b,

Thanks so much for your kind support me all the time. Now it works fine.

Thanks you som much from the bottom of my heart.


Kindly regards,

Bunhok
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Bunhok is offline Offline
6 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Ternary Operator
Next Thread in PHP Forum Timeline: Passing variable sql php





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC