Jump to content

x number of results per page


hakmir

Recommended Posts

Hi guys,

 

I have two files "searchrecipe.php" and "reciperesults.php" (with the help of MadTechie and Andy17)

Both files are working perfect (almost)

 

(almost): I don't want all the results in one page. I want 10 results (just and example) per page. I've been trying to do and I was so close but I couldn't.

 

Can anybody help me? Thanks in advance.

 

Here are the files

 

searchrecipe.php

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php $dbcnx = @mysql_connect('localhost', 'root', '');
if (!$dbcnx) {
    exit('<p>Unable to connect to the ' . 'database server at this time.</p>');
}

if (!@mysql_select_db('recipetime')) {
    exit('<p>Unable to locate the recipe ' . 'database at this time.</p>');
}

?>

<form id="form1" name="form1" method="post" action="reciperesults.php">
  <label><strong>SEARCH RECIPE</strong><br />
<br />
</label>
<p>Country<br />
  <label>
    <select name="country" id="country">
      <option>USA</option>
      <option>CANADA</option>
      <option>ENGLAND</option>
    </select>
    </label>
</p>
<p>Type of Food <br />
  <label>
    <select name="typeoffood" id="typeoffood">
      <option>DESERT</option>
      <option>ENTREE</option>
      <option>SPICY</option>
    </select>
  </label>
</p>
<p>
  <label>
  <input type="submit" name="Search" id="Search" value="Search" />
  </label>
</p>
</form>

</body>
</html>

 

reciperesults.php

 

<?php


if (!isset($_POST['Search']))
    die("FAILED: nothing selected");
$dbcnx = @mysql_connect('localhost', 'root', '');
if (!$dbcnx) {
    exit('<p>Unable to connect to the ' . 'database server at this time.</p>');
}

if (!@mysql_select_db('recipetime')) {
    exit('<p>Unable to locate the recipe' . 'database at this time.</p>');
}
$typeoffood = $_POST['typeoffood'];
$country = $_POST['country'];
$sql = "SELECT * FROM recipeform WHERE country='$country' AND typeoffood='$typeoffood'";
$result = mysql_query($sql);


echo "HERE IS ALL THE $typeoffood RECIPES FROM $country" . "<br>" . "<br>";



while ($rec = mysql_fetch_array($result)) {
    $recname = htmlspecialchars($rec['name']);
    $reccountry = htmlspecialchars($rec['country']);
    $rectypeoffood = htmlspecialchars($rec['typeoffood']);
    $recyourrecipe = htmlspecialchars($rec['yourrecipe']);


    echo "Name: <td>$recname</td>" . "<br>";
    echo "Country: <td>$reccountry</td>" . "<br>";
    echo "Typeoffood: <td>$rectypeoffood</td>" . "<br>";
    echo "Your Recipe: <td>$recyourrecipe</td>" . "<br>" . "<br>";


}

?>

Link to comment
Share on other sites

I checked the tutorial. It is working well (almost)

 

When I use this code there is no problem. It shows everything in my database page by page.

 

$sql = "SELECT * FROM recipeform  LIMIT $offset, $rowsperpage ";

 

But when I use this code (which I want to use) It shows first page with no problem. But when I click on page 2, 3, or other pages there is no data showing then when I click page 1 again it doesn't show anything either..

 

$sql = "SELECT * FROM recipeform WHERE typeoffood='$typeoffood' and country='$country'  LIMIT $offset, $rowsperpage ";

 

?

 

This is what I came up with

 

<?php
// database connection info
$conn = mysql_connect('localhost','root','') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('recipetime',$conn) or trigger_error("SQL", E_USER_ERROR);

// find out how many rows are in the table 
$sql = "SELECT COUNT(*) FROM recipeform";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 3;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;

// get the info from the db 
$typeoffood = $_POST['typeoffood'];
$country = $_POST['country'];
$sql = "SELECT * FROM recipeform WHERE typeoffood='$typeoffood' and country='$country'  LIMIT $offset, $rowsperpage ";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

// while there are rows to be fetched...


while ($list = mysql_fetch_assoc($result)) {
    $recname = htmlspecialchars($list['name']);
    $reccountry = htmlspecialchars($list['country']);
    $rectypeoffood = htmlspecialchars($list['typeoffood']);
    $recyourrecipe = htmlspecialchars($list['yourrecipe']);
   // echo data
  echo "Name: <td>$recname</td>" . "<br>";
    echo "Country: <td>$reccountry</td>" . "<br>";
    echo "Typeoffood: <td>$rectypeoffood</td>" . "<br>";
    echo "Your Recipe: <td>$recyourrecipe</td>" . "<br>" . "<br>";

} // end while

/******  build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = (($currentpage - $range) - 1); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
 echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      } // end else
   } // end if 
} // end for

// if not on last page, show forward and last page links	
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/
?>

 

 

 

 

Link to comment
Share on other sites

Just look at the script.  There's already vars in the script that do the same thing... Look how the links are made in the script.  Look how they are received in the script.  Just do your copy and paste thing and change the name to what you want and there you have it.  Don't really know what else we can tell you beyond that, except to learn to be more than a cut and paste guy....or hire someone to do it for you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.