erichwmack Posted December 22, 2008 Share Posted December 22, 2008 I have a website that is php driven to display data and images in catagories that I have designated. But I want to add a search feature for people to use and display the results on a results.php page. I have a functional php search code but I can't figure out how to display the results on a seperate page. sorry if this code is long, any help will be appreciated <form name="form" action="test556.php" method="get"> <input type="text" name="q" > <input type="submit" name="Submit" value="Search" > </form> <?php // Get the search variable from URL $var = @$_GET['q'] ; $trimmed = trim($var); //trim whitespace from the stored variable // check for an empty string and display a message. if ($trimmed == "") { echo "<p>Please enter a search...</p>"; exit; } // check for a search parameter if (!isset($var)) { echo "<p>We dont seem to have a search parameter!</p>"; exit; } //the name of the database $chandle = mysql_connect("mysql", $dbuser, $dbpass) or die("Connection Failure to Database"); //echo "Connected to database server<br>"; mysql_select_db($dbname, $chandle) or die ($dbname . " Database not found." . $dbuser); //echo "Database " . $dbname . " is selected"; echo "<br>"; // how many rows to show per page $rowsPerPage = 15; // by default we show first page $pageNum = 1; // if $_GET['page'] defined, use it as page number if(isset($_GET['page'])) { $pageNum = $_GET['page']; } // counting the offset $offset = ($pageNum - 1) * $rowsPerPage; $query = "SELECT Thumbnail_image, Name, Alink FROM dinnerware WHERE Name LIKE \"%$trimmed%\" order by Name LIMIT $offset, $rowsPerPage"; $result = mysql_query($query) or die('Error, query failed'); $tdcount = 1; $numtd = 3; $total = count($result); if (empty($s)) { $s=0; } // display what the person searched for echo "<p>You searched for: "" . $var . ""</p>"; // begin to show results set echo "Results"; $table = '<table>'; // print the random numbers while($data = mysql_fetch_array($result)) { if ($total%2 != 0) { $total+=1; } for ($i=0;$i<count($result);$i++) { if ($counter%2 == 0) { // first column $table .= '<tr><td width=\"150\">' . $result[$i] . '</td>'; } else { // second column $table .= '<td width=\"150\">' . $result[$i] . '</td></tr>'; } $counter++; } if ($tdcount == 1) echo "<tr>"; echo "<td width=\"150\"><a href=".$data[Alink]."><img src=".$data[Thumbnail_image]." width=\"140\" height=\"112\"> </a><br> $data[Name] <br></td>"; if ($tdcount == $numtd) { echo "</tr>"; $tdcount = 1; } else { $tdcount++; } } // time to close up our table if ($tdcount!= 1) { while ($tdcount <= $numtd) { echo "<td width=\"150\"> </td>"; $tdcount++; } echo "</tr>"; } $table .= '</table>'; echo $table; echo '<br>'; // how many rows we have in database $query = "SELECT COUNT(Name) AS numrows FROM dinnerware WHERE Name LIKE \"%$trimmed%\" order by Name"; $result = mysql_query($query) or die('Error, query failed'); $row = mysql_fetch_array($result, MYSQL_ASSOC); $numrows = $row['numrows']; // how many pages we have when using paging? $maxPage = ceil($numrows/$rowsPerPage); $self = $_SERVER['PHP_SELF']; // creating 'previous' and 'next' link // plus 'first page' and 'last page' link // print 'previous' link only if we're not // on page one if ($pageNum > 1) { $page = $pageNum - 1; $prev = " <a href=\"$self?q=$var&page=$page\">[<]</a> "; $first = " <a href=\"$self?q=$var&page=1\">[<<<]</a> "; } else { $prev = ' [<] '; // we're on page one, don't enable 'previous' link $first = ' [<<<] '; // nor 'first page' link } // print 'next' link only if we're not // on the last page if ($pageNum < $maxPage) { $page = $pageNum + 1; $next = " <a href=\"$self?&q=$var&page=$page\">[>]</a> "; $last = " <a href=\"$self?q=$var&page=$maxPage\">[>>>]</a> "; } else { $next = ' [>] '; // we're on the last page, don't enable 'next' link $last = ' [>>>] '; // nor 'last page' link } // print the page navigation link echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last; ?> Link to comment https://forums.phpfreaks.com/topic/137950-display-quesry-results-in-a-new-web-page/ Share on other sites More sharing options...
waynew Posted December 22, 2008 Share Posted December 22, 2008 You need to use pagination. http://www.phpfreaks.com/tutorial/basic-pagination Link to comment https://forums.phpfreaks.com/topic/137950-display-quesry-results-in-a-new-web-page/#findComment-721034 Share on other sites More sharing options...
erichwmack Posted December 22, 2008 Author Share Posted December 22, 2008 Thanks for the reply waynwex. I have a pagination function within the code and I can click from p.1 to p.xxx BUT what I would like to have is a user that is on one of my website pages, say "mensapparel.php" page, use the search function on that page and have thier search results display on "results.php". If that makes sense Link to comment https://forums.phpfreaks.com/topic/137950-display-quesry-results-in-a-new-web-page/#findComment-721036 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.