473,796 Members | 2,495 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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='$town search' AND
town=townid AND code='sleep'");
while ($row = mysql_fetch_arr ay($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"><im g src="images/<?php print
$photoname; ?>" border="0"/></td>
</tr>
</table>
</center>
</div>

<?php
}
?>

Sep 1 '05 #1
7 1889
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='$town search' AND
town=townid AND code='kids'");
while ($row = mysql_fetch_arr ay($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
2080
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 know how to split a string in chunks, but I need to find a method to make sure the text isn't split in the middle of a word. Any ideas? Thanks,
4
2710
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. http://www.scenepointblank.com/matt/dev/features/index.php?id=1&page=1 Sample above. The next/previous links were built with simple values:
2
3273
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 lines it worked quite well but srttok isnot working for multiple blank or commas. Can strtok do this kind of splitting if it cant what should i use . Unal
3
1507
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 screen. I think it might have something to do with resetting the $found variable. Can you have a quick look? any suggestions would be greatly appreciated. <? if ($_GET) $vars=$_GET; else $vars=$_POST; $found = $vars;
0
9683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10457
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10231
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10176
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9054
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.