Tutorials
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOtherTutorials

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old February 18th, 2004, 10:11 AM
nemo nemo is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Long Island, NY USA
Posts: 24 nemo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sorting Database Results with PHP By Matt Wade

Tech details:
OS = Redhat Linux ver. 9 - 2.4.20-8
Apache Webserver ver. 2.0.40
PHP ver. 4.2.2
MySql Database ver. 3.23.54

------------------------------------
First of all, THANK YOU for the wonderful tutorial. I am a real newbie when it comes to building PHP and Database apps from scratch. Thanks to all you great folks I have been able to build, well almost build a workable inventory system. I have picked up bits and pieces from many different scripts here and put them all together and actaully learned a lot and had some fun along the way. Now I am stuck. I'll past the slightly modified MATT WADE script below. The only thing I added was for ME some more comments and two POSTED variables that I get from an HTML form.
The script runs fine and outputs the data from the database table searched using the two variables in the query. It displays fine. Now the stumble, at the top of the colums of the search results are the clickable links to re-order, if I click on one of the links I get returned
-------------------------------------
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/manuals/search_form2.php on line 39
Rows returned
No data to display!
Your search type selected was
Your search string input was
-------------------------------------

What I think this is telling me is once the script goes to do a re-order, my two variables (posted from the html form) that I declared at the top of the script are no longer valid. So it returns an empty result.

Is there such a way to store those two variable for use again? Like I said I am a total newbie and I am only assuming that is what is happening becuase of what is returned. Could anyone give some guidence? Any help would be greatly appreciated.

Thanks so much
Jim (e-mail: jim.combs@nbc.com)
-------------------------------------
Code:
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" />
<!-- based on Matt Wade tutorial at http://codewalkers.com/tutorials/21/1.html -->
<title>Manual and Document Inventory and Location Database Search Results</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
/* config.php defines connection and database name variables */
include("config.php");
$Link = mysql_connect($Host,$User,$Password);
/* variables POSTED from HTML form */
$searchtype=$_POST['searchtype'];
$searchstring=$_POST['searchstring'];
/* set the allowed order by columns */
$default_sort = 'Manufacturer';
$allowed_order = array ('id','Manufacturer','Model','Location');

/* connect to the database */
mysql_connect($Host,$User,$Password);
mysql_select_db ($DBname);

/* if order is not set, or it is not in the allowed list */
/* then set it to a default value. Otherwise, set it to what */
/* was passed in with the GET when a reorder was clicked. */
if (!isset ($_GET['order']) || 
    !in_array ($_GET['order'], $allowed_order)) {
    $order = $default_sort;
} else {
    $order = $_GET['order'];
}

/* construct and run the query with variables POSTED from HTML form */
$query = "SELECT * FROM $TableName WHERE $searchtype LIKE '%$searchstring%' ORDER BY $order";

$result = mysql_query($query);

/* make sure data was retrieved */
$numrows = mysql_num_rows($result);

echo "$numrows Rows returnedn";
echo "<BR>n";
echo "<BR>n";

if ($numrows == 0) {
    echo "No data to display! n";
	echo "n <BR><BR> Your search type selected was <B>$searchtype</B> n";
	echo "n <BR><BR> Your search string input was <B>$searchstring</B> n";
    exit;
}

/* Grab the first row and start the table display with results of query */
$row = mysql_fetch_assoc ($result);
echo "<TABLE border=1>n";
echo "<TR>n";
foreach ($row as $heading=>$column) {

/* check if the heading is in the $allowed_order variable */
/* array. If it is, make it into a link so that we can */
/* select a reorder by this 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";

/* reset the $result set back to the first row and */ 
/* display the data again once a reorder link has been clicked */
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";
echo "<BR>n";
echo "Your search type was: <b>$searchtype</b> n";
echo "<BR>n";
echo "Your search string was: <b>$searchstring</b> n";
?> 
<?php
/* the mysql_close(); statement alone closes last opened connection */
mysql_close();
?>
<!-- My footer -->
<br>
<br>
<center><input type="button" value="Return Home" onClick=location.href="index.html"></center>
<br>
<hr align="center" width="90%" size="2">
<center><p class="credit"> Manuals Database beta version .01</p></center>
</body>
</html>


Reply With Quote
  #2  
Old February 18th, 2004, 10:28 AM
CodeKadiya CodeKadiya is offline
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Colombo,Sri Lanka
Posts: 2,313 CodeKadiya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
Send a message via Yahoo to CodeKadiya
RE: Sorting Database Results with PHP By Matt Wade

somehow $_POST['searchtype'] and $_POST['searchstring'] does not have values in them.. can we see the html code of the form where you enter those in textfields? there should be the problem

Reply With Quote
  #3  
Old February 18th, 2004, 10:29 AM
nawlej nawlej is offline
Moderator
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Dallas, Tx. USA
Posts: 2,008 nawlej User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 7 m 51 sec
Reputation Power: 4
RE: Sorting Database Results with PHP By Matt Wade

Can you post a link to show what it looks like before you try to reorder it? Id like to see what the link is passing.

Reply With Quote
  #4  
Old February 18th, 2004, 11:17 AM
nemo nemo is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Long Island, NY USA
Posts: 24 nemo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Sorting Database Results with PHP By Matt Wade

Hi there thank you for the reply. Below is the HTML form that the two variable get POSTED from. This part works as the scripts runs once fine and outputs the correct search and returns the proper results, It's only when one of the hyperlinked RE-ORDER colums gets clicked. And sorry I can't show you the actual site the database and web site are on an internal intranet, but I can show you what the RE-ORDER clickable link is trying to pass.

----------------------------------------
The HTML form that generates the two variables....
[CODE]
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css" />
<title>Manual and Document Inventory and Location Database </title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<!-- search_form2.php is the handler for the database search -->
<form method="POST" action="search_form2.php">
<h2>Search for</h2>
<b>Which field to search in? </b>
<BR>
<BR>
<input type="radio" name="searchtype" value="Manufacturer" checked>Manufacturer
<br>
<input type="radio" name="searchtype" value="Model">Model
<br>
<input type="radio" name="searchtype" value="Location">Location
<br>
<input type="radio" name="searchtype" value="Comments">Comments</td>
<br>
<input type="radio" name="searchtype" value="id">id number</td>
<BR>
<BR>
<input type="text" name="searchstring" size="35">
<BR>
<BR>
<input type="submit" value="submit">
<input type="reset" name="reset" value="reset">
<input type="button" value="return home" onClick=location.href="index.html">
</form>
<br>
<br>
<br>
<hr align="center" width="90%" size="2">
<center><p class="credit">Manuals Database beta version .01</p></center>
</body>
</html>
----------------------------------------
The link of one of the clickable colums to reorder looks like this.....

http://3.199.11.15/manuals/search_form2.php?order=Model
----------------------------------------

the script is pasted into my first message. Once again Thank You for the reply!
Have a great day
Jim (e-mail: jim.combs@nbc.com)

Reply With Quote
  #5  
Old February 18th, 2004, 11:21 AM
CodeKadiya CodeKadiya is offline
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Colombo,Sri Lanka
Posts: 2,313 CodeKadiya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
Send a message via Yahoo to CodeKadiya
RE: Sorting Database Results with PHP By Matt Wade

the link that you posted is broken. check again

Reply With Quote
  #6  
Old February 18th, 2004, 11:25 AM
nemo nemo is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Long Island, NY USA
Posts: 24 nemo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: RE: Sorting Database Results with PHP By Matt Wade


Quote:
the link that you posted is broken. check again


Sorry you missed it, at the top of the message I said that the web site and database are on an INTRANET and is only available internally. Meaning outside the building it can't be seen. I did copy the link and what is shown is what is trying to be passed back to the script.

Thanks
Have a great day
Jim (e-mail: jim.combs@nbc.com)

Reply With Quote
  #7  
Old February 18th, 2004, 11:34 AM
CodeKadiya CodeKadiya is offline
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Colombo,Sri Lanka
Posts: 2,313 CodeKadiya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
Send a message via Yahoo to CodeKadiya
RE: Sorting Database Results with PHP By Matt Wade

Use this code... I modified it.. should work now as I hope.
php Code:
Original - php Code
  1. <html>
  2. <head>
  3. <link rel="stylesheet" href="style.css" type="text/css" />
  4. <!-- based on Matt Wade tutorial at http://codewalkers.com/tutorials/21/1.html -->
  5. <title>Manual and Document Inventory and Location Database Search Results</title>
  6. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  7. </head>
  8. <body>
  9. <?php
  10. /* config.php defines connection and database name variables */
  11. include("config.php");
  12. $Link = mysql_connect($Host,$User,$Password);
  13. /* variables POSTED from HTML form */
  14. if($_POST['searchtype']){
  15. $searchtype=$_POST['searchtype'];
  16. $searchstring=$_POST['searchstring'];
  17. }else{
  18. $searchtype=$_GET['searchtype'];
  19. $searchstring=$_GET['searchstring'];
  20. }
  21. /* set the allowed order by columns */
  22. $default_sort = 'Manufacturer';
  23. $allowed_order = array ('id','Manufacturer','Model','Location');
  24.  
  25. /* connect to the database */
  26. mysql_connect($Host,$User,$Password);
  27. mysql_select_db ($DBname);
  28.  
  29. /* if order is not set, or it is not in the allowed list */
  30. /* then set it to a default value. Otherwise, set it to what */
  31. /* was passed in with the GET when a reorder was clicked. */
  32. if (!isset ($_GET['order']) ||
  33. !in_array ($_GET['order'], $allowed_order)) {
  34. $order = $default_sort;
  35. } else {
  36. $order = $_GET['order'];
  37. }
  38.  
  39. /* construct and run the query with variables POSTED from HTML form */
  40. $query = "SELECT * FROM $TableName WHERE $searchtype LIKE '%$searchstring%' ORDER BY $order";
  41.  
  42. $result = mysql_query($query);
  43.  
  44. /* make sure data was retrieved */
  45. $numrows = mysql_num_rows($result);
  46.  
  47. echo "$numrows Rows returnedn";
  48. echo "<BR>n";
  49. echo "<BR>n";
  50.  
  51. if ($numrows == 0) {
  52. echo "No data to display! n";
  53. echo "n <BR><BR> Your search type selected was <B>$searchtype</B> n";
  54. echo "n <BR><BR> Your search string input was <B>$searchstring</B> n";
  55. }
  56.  
  57. /* Grab the first row and start the table display with results of query */
  58. $row = mysql_fetch_assoc ($result);
  59. echo "<TABLE border=1>n";
  60. echo "<TR>n";
  61. foreach ($row as $heading=>$column) {
  62.  
  63. /* check if the heading is in the $allowed_order variable */
  64. /* array. If it is, make it into a link so that we can */
  65. /* select a reorder by this column */
  66. echo "<TD><b>";
  67. if (in_array ($heading, $allowed_order)) {
  68. echo "<a href="{$_SERVER['PHP_SELF']}?order=$heading&searchtype=$searchtype&searchstring=$searchstring">$heading</a>";
  69. } else {
  70. echo $heading;
  71. }
  72. echo "</b></TD>n";
  73. }
  74. echo "</TR>n";
  75.  
  76. /* reset the $result set back to the first row and */
  77. /* display the data again once a reorder link has been clicked */
  78. mysql_data_seek ($result, 0);
  79. while ($row = mysql_fetch_assoc ($result)) {
  80. echo "<TR>n";
  81. foreach ($row as $column) {
  82. echo "<TD>$column</TD>n";
  83. }
  84. echo "</TR>n";
  85. }
  86. echo "</TABLE>n";
  87. echo "<BR>n";
  88. echo "Your search type was: <b>$searchtype</b> n";
  89. echo "<BR>n";
  90. echo "Your search string was: <b>$searchstring</b> n";
  91. ?>
  92. <?php
  93. /* the mysql_close(); statement alone closes last opened connection */
  94. ?>
  95. <!-- My footer -->
  96. <br>
  97. <br>
  98. <center><input type="button" value="Return Home" onClick=location.href="index.html"></center>
  99. <br>
  100. <hr align="center" width="90%" size="2">
  101. <center><p class="credit"> Manuals Database beta version .01</p></center>
  102. </body>
  103. </html>


Reply With Quote
  #8  
Old February 19th, 2004, 05:53 AM
nemo nemo is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Location: Long Island, NY USA
Posts: 24 nemo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Sorting Database Results with PHP By Matt Wade

Thank you very much that did the trick, I wish I could understand more what you did.... Again Thank you!

Have a great day
Jim

Reply With Quote
  #9  
Old February 19th, 2004, 06:57 AM
CodeKadiya CodeKadiya is offline
Codewalkers Regular (2000 - 2499 posts)
 
Join Date: Apr 2007
Location: Colombo,Sri Lanka
Posts: 2,313 CodeKadiya User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 4
Send a message via Yahoo to CodeKadiya
RE: Sorting Database Results with PHP By Matt Wade

Glad to hear you solved it. I played a trick there. Here is how it works. It first checks whether there are any values posted from another page to this page. If there are values posted that mean this is the first time user comes to this page and haven't click re-order yet. Then it displays $_POST['searchtype'] so on... If there are no posted values that means user have clicked re-order link.. And can you spot the difference I have made in re-order link? I pass the variable values as a query string.. Hope I was clear.