Solved

Split record to several pages

Posted on 2003-10-23
8
279 Views
Last Modified: 2013-12-12
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.
0
Comment
Question by:eweiwei
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
8 Comments
 
LVL 1

Expert Comment

by:djerratik
ID: 9606064
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"];
0
 
LVL 2

Expert Comment

by:wise0ne
ID: 9613048
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
0
 

Author Comment

by:eweiwei
ID: 9614864
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?
0
 
LVL 2

Expert Comment

by:wise0ne
ID: 9615686
r u using php 4.2.x or 4.3.x ????
0
 
LVL 2

Accepted Solution

by:
wise0ne earned 250 total points
ID: 9615739
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).
0

Featured Post

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question