Please help me with sorting column heading

Reply

Join Date: Sep 2008
Posts: 6
Reputation: Bunhok is an unknown quantity at this point 
Solved Threads: 0
Bunhok Bunhok is offline Offline
Newbie Poster

Please help me with sorting column heading

 
0
  #1
Sep 27th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Please help me with sorting column heading

 
0
  #2
Sep 28th, 2008
Originally Posted by Bunhok View 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
I would start out with something like this. You might find some of this a little confusing, let me know if you any questions.
  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.
“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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 6
Reputation: Bunhok is an unknown quantity at this point 
Solved Threads: 0
Bunhok Bunhok is offline Offline
Newbie Poster

Re: Please help me with sorting column heading

 
0
  #3
Oct 5th, 2008
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?

Originally Posted by R0bb0b View Post
  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Please help me with sorting column heading

 
0
  #4
Oct 5th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 6
Reputation: Bunhok is an unknown quantity at this point 
Solved Threads: 0
Bunhok Bunhok is offline Offline
Newbie Poster

Re: Please help me with sorting column heading

 
0
  #5
Oct 5th, 2008
Dear sir,

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();
?>
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 849
Reputation: R0bb0b is on a distinguished road 
Solved Threads: 67
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Please help me with sorting column heading

 
0
  #6
Oct 5th, 2008
This works, didn't realize that you would have capitals in your column names.
  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. ?>
“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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 6
Reputation: Bunhok is an unknown quantity at this point 
Solved Threads: 0
Bunhok Bunhok is offline Offline
Newbie Poster

Re: Please help me with sorting column heading

 
0
  #7
Oct 6th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC