473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Splitting a Search into Pages

After trial, error, 2 days reading about PHP and some fine Single Malt,
have finally created a search from a database with multiple criteria as
below.

NOTE comments on tidying the code up are welcomed.

BUT.... if I want to split the search into groups of 10 and then (like
Google) prompt for page 2 of 10, 3 of 10 etc.

Anyone know of any tutorials on this?
Many thanks

Alec

<?php
$result = @mysql_query ("SELECT name, townname, town, townid, photo
FROM site01_towns, site01_details WHERE townname='$townsearch' AND
town=townid AND code='sleep'");
while ($row = mysql_fetch_array($result))
{
$photoname = $row['photo'];
$company = $row['name'];
?>

<div align="center">
<center>
<table border="0" cellpadding="0" cellspacing="0" width="400"
height="95">
<tr>
<td valign="top"><?php echo '<p>' . $row['name'] . '</p>'; ?></td>
<td valign="middle" width="140"><img src="images/<?php print
$photoname; ?>" border="0"/></td>
</tr>
</table>
</center>
</div>

<?php
}
?>

Sep 1 '05 #1
7 1873
What you need is 'LIMIT' in your query.
Limit CAN exist of (afaik) 2 variables:

"LIMIT 20, 10" would mean the query
skips the first 19 results, and then returns
the following 10.

So search for info about "LIMIT".

Greetings Frizzle.

Sep 1 '05 #2
That would only produce the "first 10 rows", but how would he then tell
the person "next 10 of 100" or however many rows he ultimately
produced?

Best solution would be for him to write a Pagination code routine. I
did just that, basically stuffing the entire resultset into a $_SESSION
variable and parsing it using integer values to only display "first 10
rows", "rows 20 - 29", etc.

Phil

Sep 1 '05 #3
1. look at PEAR. There's a good pagination class for doing just that. I
found it through "Object Oriented PHP Solutions - Volume 1"
(sitepoint).

2. You could do 2 queries: query1 = COUNT (gets you the number of total
rows selected); query2 = SELECT with LIMIT (select the rowset you need;
use the count++10;).

Sep 1 '05 #4
So the key is 'pagination'.

Have absolutely no idea what that is (sorry am totally new to this
stuff), but have found the link

http://bfl.rctek.com/source/?file=in...pagination.php

Have not tried this as yet, but is this what you mean?

Many thanks

Alec

Sep 2 '05 #5
That link won't help you too much. That include requires explicit page
numbering (you can get that by doing a COUNT query ("SELECT COUNT(*)
FROM [tablename] WHERE [?]"). This will give you a result as an integer
you can then test against.

Let's say the result of the count query is 22 (22 rows). You want to
display 5 rows/page. You then take your COUNT query and pass its result
along to your second query (the RESULT query) to get the current
appropriate rows.

I still think you should check out PEAR: Pager
(http://pear.php.net/package/Pager).

The learning curve will hurt, but not very much. There's good support
for PEAR packages, and your host may already have them installed.

Sep 2 '05 #6
> BUT.... if I want to split the search into groups of 10 and then (like
Google) prompt for page 2 of 10, 3 of 10 etc.

Here is the way I go about it. It might work for you.

//constant number of results per page.
$pagesize=25

//start at 0 then pass in $page=[some number] in the url.
$currentpage=3

//calculate number to skip by multiplying what page you are on
//by the page size.
$skip = $currentpage * $pagesize

//use calc_found_rows in mysql select and mysql will calculate
//the result count regardless of the limit in the sql

select calc_found_rows * from table where limit $skip, $pagesize

//use "select found_rows()" after running the above query
//and assign to $totalcount

$totalcount=150 // assigned from "select found_rows()"

Take $totalcount (150 in this case) divided by $pagesize (25) to get
the max number of pages to display on the page (6 in this case).

Create links on the webpage that pass in the page number.

1 2 3 4 5 6

example url

<a href="/webpage.php? ... &page=5">5</a>
where ... is the query string of what the user was searching on

This is not complete but should be enough to get you going somewhere.

Sep 2 '05 #7
Sorry guys but have now come a little unstuck with this after trying
some of the methods.

Its only day 6 of learning php......

So back to basics. The page below allows a user to type in a name of a
town, and search for companies from this town with certain criteria
(using two tables to cross check the town name).

What exactly do I need to split this search into groups of say 5
companies.

Sorry guys, but this is early days for me.... Thanks in advance

Alec

<title>New Page 1</title>
</head>

<body>

<?php
$connection = mysql_connect('localhost', 'uks49179', 'FWE4872xd');
$db = mysql_select_db("test", $connection);
?>

<?php $townsearch = $_GET['town']; ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<p align="center">
<input type="text" name="town" size="20" value="<?php echo
$townsearch; ?>" />&nbsp;
<input type="submit" value="GO" />
</form>

<?php
$result = @mysql_query ("SELECT name, townname, town, townid FROM
site01_towns, site01_details WHERE townname='$townsearch' AND
town=townid AND code='kids'");
while ($row = mysql_fetch_array($result))
{
$company = $row['name'];
echo $company;
}
?>

</body>

</html>

Sep 5 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: somaBoy MX | last post by:
I'm building a site where I need to pull very large blocks from a database. I would like to make navigation a little more user friendly by splitting text in pages which can then be navigated. I...
4
by: guitarromantic | last post by:
Hey everyone. Following advice found online, I figured out a basic way of splitting a long article into several pages, by exploding out from a <!--pagebreak--> code in my $content field. ...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
3
by: Bigalan | last post by:
Hello, i am relatively new to PHP and i am struggling with printing multiple search results on to different pages. The code below works ok but when you click on next page button, it brings up a blank...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.