Hi Everyone,
I'm at the end of the rope and I'm stuck on this. I'll try and make this easy for everyone.
I'm building a recipe site with about one million recipes in it. I'm new to PHP but so
far have been doing well with it.
After a member logs into my site by submitting a username and password their directed to the main page.
In my main page I have a search form built in where a member can input a search using a text box and a dropdown box to search by specifics.
The search form seems to be working fine.
Upon submit the form directs the user to "Search.php". In the "Search.php" I've also included the form for additional searches.
If I enter the word "Appetizers in the form and select the dropdown "Category" then click "Submit" the search works. I have the LIMIT set to "5" record returns. And upon submitt I get the first five records.
However my pagination is not working. The <PREV> and <NEXT> links do not function. They are black in color and are not clickable.
I'm assuming that I have a problem somewhere in the fact that the code is not forwarding the query to the next pages, or there is a problem with the query somehow.
I've researched this on the site and have gone to the tutorial "Easy as PREV 123 NEXT" I've followed many tutorials and have tried many adaptations of code.
I'm really new to PHP and I'm lost at this point.
Can anyone please help me with this code.
My code is as follows.
Here is the form outside and on top of the <?php
<form name="search" method="GET" action="<?=$PHP_SELF?>">
<font size=2 color=navy face=arial>Seach For:</font><input type="text" name="frank" size="30" />
<font size=2 color=navy face=arial> In:</font>
<Select NAME="sinatra">
<option value="title">Title</option>
<option value="ingredients">Ingredients</option>
<option value="category">Category</option>
<option value="id">Record Number</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form
[/COLOR]
I'll break the code down for you for an easy lookover.
I had to add the below string variables because I was getting an SQL syntax error. I followed a previous example from this site which seemed to fix the problem.
[COLOR=Blue][CODE]@mysql_connect("localhost", "user", "password") or die("ERROR--CAN'T CONNECT TO SERVER");
@mysql_select_db("recipes") or die("ERROR--CAN'T CONNECT TO DB");
$find = "frank";
$field = "sinatra"; [/CODE] [/COLOR]
Here are the limit strings and count query.
$limit =5;
$query_count = "SELECT count(*) FROM contents WHERE upper($field) LIKE '%$find%'";
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);
if(empty($page)){
$page = 1;
}
$limitvalue = $page * $limit - ($limit);
[/COLOR]
Here is my query and while loop.
$query = "SELECT * FROM contents WHERE upper($field) LIKE '%$find%' LIMIT $limit, $limitvalue";
$result = mysql_query($query) or die("Error: " . mysql_error());
if(mysql_num_rows($result) == 0){
echo("Your Search Has No Results. Try Again!");
}
echo ("<table align='center' width='55%'>");
while($row = mysql_fetch_array($result)){
[/COLOR]
Here are my return values in table form.
echo "<tr><td><font size=2 color=navy face=arial>";
echo "<font size=2 color=navy face=arial>Record ID:</font> ";
echo $row['id'];
echo "</font></td></tr>";
echo "<tr><td><font size=2 color=navy face=arial>";
echo "<font size=2 color=navy face=arial>Category:</font> ";
echo $row['category'];
echo "</font></td></tr>";
echo "<tr><td><font size=2 color=blue face=arial><b>";
echo "<font size=2 color=navy face=arial><b>Title:</b></font> ";
echo $row['title'];
echo "</font></td></tr>";
echo "<tr><td><font size=2 color=navy face=arial>";
echo "<font size=2 color=navy face=arial><b>Ingredients:</b></font> ";
echo $row['ingredients'];
echo "</font></td></tr>";
echo "<tr><td><font size=2 color=navy face=arial>";
echo "<font size=2 color=navy face=arial><b>Directions:</b></font> ";
echo $row['directions'];
echo "</font</td></tr>";
echo "<tr><td><br><br></td></tr>";
}
echo("</table>");
[/COLOR]
Here is the pagaination code.
if($page != 1){
$pageprev = $page--;
echo("<a href=\"search.php&page=$pageprev\">PREV".$limit."</a> ");
}else{
echo("PREV".$limit." ");
}
$numofpages = $totalrows / $limit;
for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"search.php?page=$i\">$i</a> ");
}
}
if(($totalrows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"searh.php?page=$i\">$i</a> ");
}
}
if(($totalrows - ($limit * $page)) > 0){
$pagenext = $page++;
echo("<a href=\"search.php?page=$pagenext\">NEXT".$limit."</a>");
}else{
echo("NEXT".$limit);
}
mysql_free_result($result);
?>
[/COLOR]
Here is the entire code if you would like to see it all.
[CODE]<?php
@mysql_connect("localhost", "milton", "785epx") or die("ERROR--CAN'T CONNECT TO SERVER");
@mysql_select_db("recipes") or die("ERROR--CAN'T CONNECT TO DB");
$find = "frank";
$field = "sinatra";
$limit =5;
$query_count = "SELECT count(*) FROM contents WHERE upper($field) LIKE '%$find%'";
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);
if(empty($page)){
$page = 1;
}
$limitvalue = $page * $limit - ($limit);
$query = "SELECT * FROM contents WHERE upper($field) LIKE '%$find%' LIMIT $limit, $limitvalue";
$result = mysql_query($query) or die("Error: " . mysql_error());
if(mysql_num_rows($result) == 0){
echo("Your Search Has No Results. Try Again!");
}
echo ("<table align='center' width='55%'>");
while($row = mysql_fetch_array($result)){
echo "<tr><td><font size=2 color=navy face=arial>";
echo "<font size=2 color=navy face=arial>Record ID:</font> ";
echo $row['id'];
echo "</font></td></tr>";
echo "<tr><td><font size=2 color=navy face=arial>";
echo "<font size=2 color=navy face=arial>Category:</font> ";
echo $row['category'];
echo "</font></td></tr>";
echo "<tr><td><font size=2 color=blue face=arial><b>";
echo "<font size=2 color=navy face=arial><b>Title:</b></font> ";
echo $row['title'];
echo "</font></td></tr>";
echo "<tr><td><font size=2 color=navy face=arial>";
echo "<font size=2 color=navy face=arial><b>Ingredients:</b></font> ";
echo $row['ingredients'];
echo "</font></td></tr>";
echo "<tr><td><font size=2 color=navy face=arial>";
echo "<font size=2 color=navy face=arial><b>Directions:</b></font> ";
echo $row['directions'];
echo "</font</td></tr>";
echo "<tr><td><br><br></td></tr>";
}
echo("</table>");
if($page != 1){
$pageprev = $page--;
echo("<a href=\"search.php&page=$pageprev\">PREV".$limit."</a> ");
}else{
echo("PREV".$limit." ");
}
$numofpages = $totalrows / $limit;
for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"search.php?page=$i\">$i</a> ");
}
}
if(($totalrows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"searh.php?page=$i\">$i</a> ");
}
}
if(($totalrows - ($limit * $page)) > 0){
$pagenext = $page++;
echo("<a href=\"search.php?page=$pagenext\">NEXT".$limit."</a>");
}else{
echo("NEXT".$limit);
}
mysql_free_result($result);
?> [/CODE][/COLOR]
I would greatly appreciate any help you great guys or gals can help me with.
This is the last code of the site that I need to finish before I can start adding content.
Thank very much in advance.