Solved

Split record to several pages

Posted on 2003-10-23
8
269 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
  • 3
8 Comments
 
LVL 1

Expert Comment

by:djerratik
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
r u using php 4.2.x or 4.3.x ????
0
 
LVL 2

Accepted Solution

by:
wise0ne earned 250 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now