Split record to several pages

I am new to PHP, want your help :)

1. in product.php, i have:

$db = mysql_connect("localhost", "root") or die("Cound not connect!");
mysql_select_db("visitama",$db) or die("Cound not select database!");
$result = mysql_query("SELECT * FROM products WHERE cat_id='$pid' AND brand_id='$brand' order by model",$db) or die("Could not query!");

while ($myrow = mysql_fetch_array($result)) {
echo "$myrow[model]";
echo "$myrow[desc]";
echo "$myrow[price]";

mysql_close() or die("Could not close connection!");

It means, if I have 100 records, then it will list all 100 records in 1 page.
How to list only 15 records in 1 page, and the remaining will be listed in the next page

at the bottom of list, should look like this:
Page: 1 | 2 | 3 | 4

2. Does my coding above need the URL looks like this:
why if i type product.php?gid=1 (without brand=1)
it shows an error "Notice: Undefined index: brand in D:\Website\product.php on line 4"

Who is Participating?
These are NOT errors (FATAL). These are not warnings (HEY! Deal with me!). They are notices (Your code sucks, but I can live with it!) (sort of thing).

You can :

1 - Add ...

error_reporting(E_ALL & ~E_NOTICE);

to the top of your PHP code which will hide the notices in this code.

2 - Set error_reporting to E_ALL & ~E_NOTICE in php.ini which will hide the notices in all code (unless overridden in the php file).
to 1: mysql offers a very neat sql parameter called "LIMIT". to display the first 15 records u'ld write
LIMIT 0,15 (where 0 means the offset, e.g. at which row output should begin & 15 the number of rows) at the end of the sql statement. the next page would have LIMIT 15,15 and so on... pass a variable via url to make the navigation links (e.g. ?page=2) and use that variable to feed the offset parameter.

to 2: this is normal behaviour as u use a variable that isn't defined when not set in the url. to bypass that error u may first check if the varibale is set
e.g. if (isset($_REQUEST["brand"])) $brand=$_REQUEST["brand"];
Solution 1
$db_addr = 'localhost';            // address of MySQL server.
$db_user = 'user';            // Username to access server.
$db_pass = 'password';            // Password access server.
$db_name = 'MyDatabase';      // Name of database to connect to.
$connect = @mysql_connect("$db_addr", "$db_user", "$db_pass");

if (!($connect)) // If no connect, error and exit().
echo("<p>Unable to connect to the database server.</p>");

if (!(@mysql_select_db($db_name))) // If can't connect to database, error and exit().
echo("<p>Unable to locate the $db_name database.</p>");

// to avoid 'Notice: Undefined index:' error  ;-)
if (isset($_REQUEST["pid"]))   { $pid = $_REQUEST["pid"]; }
if (isset($_REQUEST["brand"])) { $brand = $_REQUEST["brand"]; }

if (!($limit)){ $limit = 10; } // Default results per-page.
if (!($page)){ $page = 0;} // Default page value.

$numresults = mysql_query("SELECT * FROM products WHERE cat_id='$pid' AND brand_id='$brand' ORDER BY model ASC"); // the query.
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.

if ($numrows == 0){
  echo("No results found matching your query - $query"); // bah, modify the "Not Found" error for your needs.

$pages = intval($numrows/$limit); // Number of results pages.

// $pages now contains int of pages, unless there is a remainder from division.

if ($numrows%$limit) { $pages++; } // has remainder so add one page

//$current = ($page/$limit) + 1; // Current page number
$current = intval($page/$limit) + 1; // Current page number to make output a bit nicer

if (($pages < 1) || ($pages == 0)) { $total = 1; } // If $pages is less than one or equal to 0, total pages is 1.

else { $total = $pages;} // Else total pages is $pages value.

$first = $page + 1; // The first result.

if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) { $last = $page + $limit; } //If not last results page, last result equals $page plus $limit.
else{ $last = $numrows; } // If last results page, last result equals total number of results.

//escape from PHP mode.
<title>Product Display</title>
<table width="100%" border="0">
  <td width="50%" align="left">Results <b><?=$first?></b> - <b><?=$last?></b> of <b><?=$numrows?></b>
  <td width="50%" align="right">Page <b><?=$current?></b> of <b><?=$total?></b></td>
  <td colspan="2" align="right">&nbsp;</td>
  <td colspan="2" align="right">Results per-page: <a href="<?=$PHP_SELF?>?cat_id=<?=$pid?>&brand_id=<?=$brand;?>&page=<?=$page?>&limit=5">5</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=10">10</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=20">20</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=50">50</a>
//Go back into PHP mode.

// Now we can display results.
$results = mysql_query("SELECT * FROM products WHERE cat_id='$pid' AND brand_id='$brand' ORDER BY model ASC LIMIT $page, $limit");
while ($data = mysql_fetch_array($results))
  <p><?=$data["model"]?> - <?=$data["desc"]?><br><?=$data["price"]?></p>
<p align="center">
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?cat_id=<?=$pid?>&brand_id=<?=$brand;?>&page=$back_page&limit=$limit\">back</a>    \n");}

for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
 $ppage = $limit*($i - 1);
 if ($ppage == $page){
 echo("<b>$i</b> \n");} // If current page don't give link, just text.
 echo("<a href=\"$PHP_SELF?cat_id=<?=$pid?>&brand_id=<?=$brand;?>&page=$ppage&limit=$limit\">$i</a> \n");}

if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo("    <a href=\"$PHP_SELF?cat_id=<?=$pid?>&brand_id=<?=$brand;?>&page=$next_page&limit=$limit\">next</a>\n");}

Solution 2
if (isset($_REQUEST["brand"])) {

Hope this helps!
eweiweiAuthor Commented:
i get the following errors:

Notice: Undefined variable: limit in D:\Website\product.php on line 24
Notice: Undefined variable: page in D:\Website\product.php on line 25
Notice: Undefined variable: PHP_SELF in D:\Website\product.php on line 100
Notice: Undefined variable: PHP_SELF in D:\Website\product.php on line 105

what's that mean?
r u using php 4.2.x or 4.3.x ????
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.