Split record to several pages

I am new to PHP, want your help :)

1. in product.php, i have:
<?
$pid=$_REQUEST["pid"];
$brand=$_REQUEST["brand"];

$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:
  product.php?gid=1&brand=1
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"

Thanks.
eweiweiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

djerratikCommented:
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"];
wise0neCommented:
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>");
exit();
}

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>");
exit();
}

// 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.
  exit();
}

$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.
?>
<html>
<head>
<title>Product Display</title>
</head>
<body>
<table width="100%" border="0">
 <tr>
  <td width="50%" align="left">Results <b><?=$first?></b> - <b><?=$last?></b> of <b><?=$numrows?></b>
  </td>
  <td width="50%" align="right">Page <b><?=$current?></b> of <b><?=$total?></b></td>
 </tr>
 <tr>
  <td colspan="2" align="right">&nbsp;</td>
 </tr>
 <tr>
  <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>
  </td>
 </tr>
</table>
<?
//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.
 else{
 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");}
?>
</p>
</body>
</html>



Solution 2
----------
if (isset($_REQUEST["brand"])) {
  $brand=$_REQUEST["brand"];
}



-------------------
Hope this helps!
e-njoy
Imran
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?
wise0neCommented:
r u using php 4.2.x or 4.3.x ????
wise0neCommented:
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).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.