|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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>
|
|
#2
|
|||
|
|||
|
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
|
|
#3
|
|||
|
|||
|
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.
|
|
#4
|
|||
|
|||
|
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) |
|
#5
|
|||
|
|||
|
RE: Sorting Database Results with PHP By Matt Wade
the link that you posted is broken. check again
|
|
#6
|
|||
|
|||
|
RE: RE: Sorting Database Results with PHP By Matt Wade
Quote:
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) |
|
#7
|
|||||
|
|||||
|
RE: Sorting Database Results with PHP By Matt Wade
Use this code... I modified it.. should work now as I hope.
php Code:
|
|
#8
|
|||
|
|||
|
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 |
|
#9
|
|||
|
|||
|
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.
|