473,573 Members | 2,753 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NEXT - PREVIOUS

Hello,
I'm working on a search application for my website. The website contains a
lot of pictures, and a search should return clickable thumbnails. No
problems there. My problem started when I wanted to build in NEXT and
PREVIOUS buttons, so that you only get 5 or 10 or 20 (I haven't made up my
mind yet) thumbnails at a time. I use a SQL LIMIT statement to achieve that.
After displaying the first 5 pictures I would like to see the next 5, etc.,
which is not happening with what I've written so far.
A test version can be found at
http://bluegumdata.com.au/pictures/search/search.php.
Suggestions please. Thanks, Marcel

The code :

<?php

//make a database connection
require('db.php ');

if (isset ($_POST['submit']))
{

if (isset($_GET['pageno']))
{
$pageno = $_GET['pageno'];
echo "Pageno : " . $pageno;
}
else
{
$pageno = 1;
echo "Pageno : " . $pageno;
}

//convert search string to lowcase
//strip search string of unwanted words and characters
//put stripped search string in $search
//keep the original term in $newterm
$newterm = $_POST['_searchterm'];

$_POST['_searchterm'] = strtolower($_PO ST['_searchterm']);
$_POST['_searchterm'] = " ".$_POST['_searchterm']." ";
$_POST['_searchterm'] = str_replace(' and ', ' ', $_POST['_searchterm']);
$_POST['_searchterm'] = str_replace(' - ', ' ', $_POST['_searchterm']);
$_POST['_searchterm'] = str_replace(' the ', ' ', $_POST['_searchterm']);
$_POST['_searchterm'] = str_replace(' a ', ' ', $_POST['_searchterm']);
$_POST['_searchterm'] = str_replace(' or ', ' ', $_POST['_searchterm']);
$_POST['_searchterm'] = str_replace(' ', ' ', $_POST['_searchterm']);
$_POST['_searchterm'] = trim($_POST['_searchterm']);

$search = $_POST['_searchterm'];

//split search string in separate words
//count the number of qwords
$words = explode(' ',$search);
$numterms = count($words);

//build up search string for database
$match = "keywords LIKE '%" . $words[0] . "%'";
for ($i = 1;
$i < $numterms;
$i++)
{
$match .= " AND keywords LIKE '%" . $words[$i] . "%'";
}

//count the number of matching records
$query = "SELECT COUNT(*) FROM pictures "
. "WHERE $match ";

$result = mysql_query($qu ery);
$query_data = mysql_fetch_row ($result);
$numrows = $query_data[0];

//set the numbers per page and the number of pages needed
$rows_per_page = 5;
$lastpage = ceil($numrows / $rows_per_page) ;

//create the LIMIT-clause and execute the query
$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page . ',' . $rows_per_page;

$query = "SELECT DISTINCT url, keyword1, title, thumbfile, country,
location, yearofpic "
. "FROM pictures "
. "WHERE $match "
. "ORDER BY yearofpic, imageid "
. "$limit ";

$result = mysql_query($qu ery);

//display results
//if there aren't any, display a message 'no results'
if (mysql_num_rows ($result))
{
echo "<p>You searched our website for: <b>" . $newterm ."</b> and this
is what we found...</p>\n";
echo "<p>";

while ($row = mysql_fetch_row ($result))
{
echo "<div class ='thumbnaildiv' >";
echo "<a href = '" . $row['0'] . "'><img src = '". $row['3'] ."'
border = '0'></a><br>";
echo "<b>What: </b>" . $row['2'] . ", ". $row['1'] . "</br><b>Where:
</b> ". $row['5'] . " , " . $row['4'] . "<br>";
echo "<b>When: </b>" . $row['6'];
echo "</div>";
}
echo "</p>\n";
echo "<br class='clearbot h'>";

//create NEXT and PREVIOUS buttons
//This will be expanded as soon as I get the NEXT button to work...
if ($pageno==1)
{
echo "<div class='navigati on'>";
echo "<ul class='horizont al'>";
echo "<li> <a
href='search.ph p?_searchterm=$ newterm?pageno= 2'><span>NEXT</span></a> </li>
";
echo "</ul>";
echo "</div>";
echo "<br class='clearbot h'>";
}
}
else
{
echo "<p>You searched our website for: <b>" . $newterm . "</b> but we
couldn't find anything..</p>\n";
echo "<p>";
}
}

?>
Jul 17 '05 #1
3 3044
Take a look at http://www.tonymarston.co.uk/php-mysql/pagination.html for a
description of pagination and how to do it.

--
Tony Marston

http://www.tonymarston.net
"Marcel" <in**@nospam-bluegumdata.com .au> wrote in message
news:Ql******** *******@news-server.bigpond. net.au...
Hello,
I'm working on a search application for my website. The website contains a
lot of pictures, and a search should return clickable thumbnails. No
problems there. My problem started when I wanted to build in NEXT and
PREVIOUS buttons, so that you only get 5 or 10 or 20 (I haven't made up my
mind yet) thumbnails at a time. I use a SQL LIMIT statement to achieve
that.
After displaying the first 5 pictures I would like to see the next 5,
etc.,
which is not happening with what I've written so far.
A test version can be found at
http://bluegumdata.com.au/pictures/search/search.php.
Suggestions please. Thanks, Marcel

The code :

<?php

//make a database connection
require('db.php ');

if (isset ($_POST['submit']))
{

if (isset($_GET['pageno']))
{
$pageno = $_GET['pageno'];
echo "Pageno : " . $pageno;
}
else
{
$pageno = 1;
echo "Pageno : " . $pageno;
}

//convert search string to lowcase
//strip search string of unwanted words and characters
//put stripped search string in $search
//keep the original term in $newterm
$newterm = $_POST['_searchterm'];

$_POST['_searchterm'] = strtolower($_PO ST['_searchterm']);
$_POST['_searchterm'] = " ".$_POST['_searchterm']." ";
$_POST['_searchterm'] = str_replace(' and ', ' ', $_POST['_searchterm']);
$_POST['_searchterm'] = str_replace(' - ', ' ', $_POST['_searchterm']);
$_POST['_searchterm'] = str_replace(' the ', ' ', $_POST['_searchterm']);
$_POST['_searchterm'] = str_replace(' a ', ' ', $_POST['_searchterm']);
$_POST['_searchterm'] = str_replace(' or ', ' ', $_POST['_searchterm']);
$_POST['_searchterm'] = str_replace(' ', ' ', $_POST['_searchterm']);
$_POST['_searchterm'] = trim($_POST['_searchterm']);

$search = $_POST['_searchterm'];

//split search string in separate words
//count the number of qwords
$words = explode(' ',$search);
$numterms = count($words);

//build up search string for database
$match = "keywords LIKE '%" . $words[0] . "%'";
for ($i = 1;
$i < $numterms;
$i++)
{
$match .= " AND keywords LIKE '%" . $words[$i] . "%'";
}

//count the number of matching records
$query = "SELECT COUNT(*) FROM pictures "
. "WHERE $match ";

$result = mysql_query($qu ery);
$query_data = mysql_fetch_row ($result);
$numrows = $query_data[0];

//set the numbers per page and the number of pages needed
$rows_per_page = 5;
$lastpage = ceil($numrows / $rows_per_page) ;

//create the LIMIT-clause and execute the query
$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page . ',' .
$rows_per_page;

$query = "SELECT DISTINCT url, keyword1, title, thumbfile, country,
location, yearofpic "
. "FROM pictures "
. "WHERE $match "
. "ORDER BY yearofpic, imageid "
. "$limit ";

$result = mysql_query($qu ery);

//display results
//if there aren't any, display a message 'no results'
if (mysql_num_rows ($result))
{
echo "<p>You searched our website for: <b>" . $newterm ."</b> and this
is what we found...</p>\n";
echo "<p>";

while ($row = mysql_fetch_row ($result))
{
echo "<div class ='thumbnaildiv' >";
echo "<a href = '" . $row['0'] . "'><img src = '". $row['3'] ."'
border = '0'></a><br>";
echo "<b>What: </b>" . $row['2'] . ", ". $row['1'] . "</br><b>Where:
</b> ". $row['5'] . " , " . $row['4'] . "<br>";
echo "<b>When: </b>" . $row['6'];
echo "</div>";
}
echo "</p>\n";
echo "<br class='clearbot h'>";

//create NEXT and PREVIOUS buttons
//This will be expanded as soon as I get the NEXT button to work...
if ($pageno==1)
{
echo "<div class='navigati on'>";
echo "<ul class='horizont al'>";
echo "<li> <a
href='search.ph p?_searchterm=$ newterm?pageno= 2'><span>NEXT</span></a>
</li>
";
echo "</ul>";
echo "</div>";
echo "<br class='clearbot h'>";
}
}
else
{
echo "<p>You searched our website for: <b>" . $newterm . "</b> but we
couldn't find anything..</p>\n";
echo "<p>";
}
}

?>

Jul 17 '05 #2
Marcel wrote:
Hello,
I'm working on a search application for my website. The website contains a
lot of pictures, and a search should return clickable thumbnails. No
problems there. My problem started when I wanted to build in NEXT and
PREVIOUS buttons, so that you only get 5 or 10 or 20 (I haven't made up my
mind yet) thumbnails at a time. I use a SQL LIMIT statement to achieve that.
After displaying the first 5 pictures I would like to see the next 5, etc.,
which is not happening with what I've written so far.
A test version can be found at
http://bluegumdata.com.au/pictures/search/search.php.
Suggestions please. Thanks, Marcel

The code :

<?php

//make a database connection
require('db.php ');

if (isset ($_POST['submit']))
{

if (isset($_GET['pageno']))
{
$pageno = $_GET['pageno'];
echo "Pageno : " . $pageno;
}
else
{
$pageno = 1;
echo "Pageno : " . $pageno;
}

//convert search string to lowcase
//strip search string of unwanted words and characters
//put stripped search string in $search
//keep the original term in $newterm
$newterm = $_POST['_searchterm'];

$_POST['_searchterm'] = strtolower($_PO ST['_searchterm']);
$_POST['_searchterm'] = " ".$_POST['_searchterm']." ";
$_POST['_searchterm'] = str_replace(' and ', ' ', $_POST['_searchterm']);
$_POST['_searchterm'] = str_replace(' - ', ' ', $_POST['_searchterm']);
$_POST['_searchterm'] = str_replace(' the ', ' ', $_POST['_searchterm']);
$_POST['_searchterm'] = str_replace(' a ', ' ', $_POST['_searchterm']);
$_POST['_searchterm'] = str_replace(' or ', ' ', $_POST['_searchterm']);
$_POST['_searchterm'] = str_replace(' ', ' ', $_POST['_searchterm']);
$_POST['_searchterm'] = trim($_POST['_searchterm']);

$search = $_POST['_searchterm'];

//split search string in separate words
//count the number of qwords
$words = explode(' ',$search);
$numterms = count($words);

//build up search string for database
$match = "keywords LIKE '%" . $words[0] . "%'";
for ($i = 1;
$i < $numterms;
$i++)
{
$match .= " AND keywords LIKE '%" . $words[$i] . "%'";
}

//count the number of matching records
$query = "SELECT COUNT(*) FROM pictures "
. "WHERE $match ";

$result = mysql_query($qu ery);
$query_data = mysql_fetch_row ($result);
$numrows = $query_data[0];

//set the numbers per page and the number of pages needed
$rows_per_page = 5;
$lastpage = ceil($numrows / $rows_per_page) ;

//create the LIMIT-clause and execute the query
$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page . ',' . $rows_per_page;

$query = "SELECT DISTINCT url, keyword1, title, thumbfile, country,
location, yearofpic "
. "FROM pictures "
. "WHERE $match "
. "ORDER BY yearofpic, imageid "
. "$limit ";

$result = mysql_query($qu ery);

//display results
//if there aren't any, display a message 'no results'
if (mysql_num_rows ($result))
{
echo "<p>You searched our website for: <b>" . $newterm ."</b> and this
is what we found...</p>\n";
echo "<p>";

while ($row = mysql_fetch_row ($result))
{
echo "<div class ='thumbnaildiv' >";
echo "<a href = '" . $row['0'] . "'><img src = '". $row['3'] ."'
border = '0'></a><br>";
echo "<b>What: </b>" . $row['2'] . ", ". $row['1'] . "</br><b>Where:
</b> ". $row['5'] . " , " . $row['4'] . "<br>";
echo "<b>When: </b>" . $row['6'];
echo "</div>";
}
echo "</p>\n";
echo "<br class='clearbot h'>";

//create NEXT and PREVIOUS buttons
//This will be expanded as soon as I get the NEXT button to work...
if ($pageno==1)
{
echo "<div class='navigati on'>";
echo "<ul class='horizont al'>";
echo "<li> <a
href='search.ph p?_searchterm=$ newterm?pageno= 2'><span>NEXT</span></a> </li>
";
echo "</ul>";
echo "</div>";
echo "<br class='clearbot h'>";
}
}
else
{
echo "<p>You searched our website for: <b>" . $newterm . "</b> but we
couldn't find anything..</p>\n";
echo "<p>";
}
}

?>


Well, for one things, are you POSTing the form or GETting it? If the
former, $_GET['pageno'] will not be defined. If the latter,
$_POST['submit'] won't be defined.

Also, check your braces. If $_POST['submit'] isn't set, you'll skip
everything.

Just a couple of things I saw on at a quick glance. There may be others.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Jul 17 '05 #3
Tony Marston wrote:
Take a look at http://www.tonymarston.co.uk/php-mysql/pagination.html for a
description of pagination and how to do it.

Thanks for that clear demonstration. I have constructed several of these
sort of mechanisms and use the same basic method, but I think my scripts
are a bit more convoluted. I like your method.

To add to that, I often provide a link from every entry (every row) on
these "index" pages that opens a new page displaying that one entry in
it's entirety. I also include Next and Prev links on that page to the
next and previous individual entries, so the user does not have to go
back to the index page to get to the next and previous individual entries.

[This could be a page that allows modification of individual database
entries, or in the case of a photo gallery it merely displays the
individual photo (displaying n thumbnails per index (selection) page,
and clicking on a thumbnail opens a new window displaying the full size
version of that photo - with next previous links to the next and
previous single photo).]

I hope I can explain the rest of this in a comprehensible manner.
Although I have found a solution that does what I want, I am wondering
if it is really the only way.

The problematic requirement is that I want the next and previous links
on the second page (with the individual entry) to point to the next and
previous entries in the same order as they are on the index page.

If I order my select queries (index page and secondary page) using a
field where all entries are unique (like an index that is auto
incremented), I have no problem using the following queries for previous
and next on the second page (with the individual entries):

(dbase table = packages; - table index, pkg_id is auto incremented; this
entry's index is $pkg_id)

//Is there a previous package?
$prevresult = mysql_query("SE LECT * FROM packages WHERE
pkg_id < '$pkg_id'
ORDER BY pkg_id DESC LIMIT 1", $link);
if ($prevrow = @mysql_fetch_ar ray($prevresult )) {
$prevpkg_id=$pr evrow[pkg_id];
} else {
$prevpkg_id=0; // signifies no entry
}

//Is there a next package?
$nextresult = mysql_query("SE LECT * FROM packages WHERE
pkg_id > '$pkg_id'
ORDER BY pkg_id LIMIT 1", $link);
if ($nextrow = @mysql_fetch_ar ray($nextresult )) {
$nextpkg_id=$ne xtrow[pkg_id];
} else {
$nextpkg_id=0; // signifies no entry
}

But, if I order the entries on the index page using a field that is not
unique (in this case - pkg_date, where many entries can have the same
pkg_date), I can not find a way to obtain the next and previous entries
(on the second page) in the same order as they appear on the index page.
I have tried the following, but it does not work:

//Is there a previous package?
$prevresult = mysql_query("SE LECT * FROM packages WHERE
pkg_date <= '$row[pkg_date]' AND
pkg_id != $pkg_id
ORDER BY pkg_date, DESC LIMIT 1", $link);
if ($prevrow = @mysql_fetch_ar ray($prevresult )) {
$prevpkg_id=$pr evrow[pkg_id];
} else {
$prevpkg_id=0;
}

//Is there a next package?
$nextresult = mysql_query("SE LECT * FROM packages WHERE
pkg_date >= '$row[pkg_date]' AND
pkg_id != $pkg_id
ORDER BY pkg_date ASC LIMIT 1", $link);
if ($nextrow = @mysql_fetch_ar ray($nextresult )) {
$nextpkg_id=$ne xtrow[pkg_id];
} else {
$nextpkg_id=0;
}

I think I see the inherent flaw in this (although I'm at my limit of
understanding/confusion).

What I end up doing is keeping track of the row number of the entries on
my index page and passing that as an extra variable to the second page
(where the entry is displayed). Then I can use the same query as I use
on the index page, but limit the result to the next row number and the
prev row number. What I don't like about that solution is that it I have
another variable (row_number) I need to keep passing up and down the
chain (should I go on to other pages).

Is that simply the only way to do it, or is there another way to do what
I want without having to pass the result's row number to keep track?

--
*************** **************
Chuck Anderson • Boulder, CO
http://www.CycleTourist.com
Integrity is obvious.
The lack of it is common.
*************** **************
Jul 17 '05 #4

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

Similar topics

10
10952
by: george | last post by:
Can anyone help? I query a database and return a result on the column "reference". There might be 7 listings. Each row is displayed in a table, with links through to a detail page. I am working on having a "previous" record and a "next" record link on the detail page. This code below works, for the "next" record, by searching the values...
5
9461
by: Bob Bedford | last post by:
Does exist a next prev function in PHP/Mysql. I've a website with returns lot of records (say about 200). The user may browse trough the list or returned records, but he has to click the record result then click back's browser in order to see the next record. I'd like to provide a "see next" and "see previous" record in the details itself. How...
13
14714
by: Joseph Garvin | last post by:
When I first came to Python I did a lot of C style loops like this: for i in range(len(myarray)): print myarray Obviously the more pythonic way is: for i in my array: print i
24
2833
by: Ian Rastall | last post by:
I do a site where I have a previous and next link at the bottom of every page. It looks like: <p><a href="foo01.html">Previous</a> | <a href="foo03.html">Next</a></p> Seeing as they're always in sequential order, is there a way to automagically go to the previous file or the next file using JS? Ian --
3
1787
by: dekern | last post by:
Good afternoon all, I guess I am missing the benefit of using datasets with Crystal. For years I have written wrapper apps that used the Pull method and I let Crystal do all the sql work. Now I have an issue where the queries run for 4+ minutes so I don't want Crystal doing repeated legwork. I was under the impression that once the...
1
2410
by: Reb | last post by:
Hi all, I have not successfully found a Javascript sample for getting next and previous links from a file... I figured that someone must have solved this problem already! ... here is what I'm trying to do. I have a multi-framed file, with navigation in a top bar. Within each content directory, I have a file called nav.txt that lists the...
3
1877
by: md9108 | last post by:
I have a previous page and next page coded. The next page works. The previous page gives me an http: 404 error and I can't figure it out. The error is on the line "<td align=left> <!-- This is the "previous" button. This retrieves the previous page of documents for the query. --> <%SaveQuery = FALSE%> <%if CurrentPage > 1 and...
1
1795
by: cbanks | last post by:
Hello Ladies/Gents, im in need of some serious help here.. I have a tool that allows users to view information on a form and make changes to recordsets. This tool has buttons that allow a user to cruise to the next or previous record. The problem i am having is when they click next record it moves to the next record and checks to see if the...
8
10807
by: GaryDean | last post by:
I have a Wizard page and need to affect the next and previous buttons from my code-behind. I've googled around and found two solutions, and neither appear to work. I can access the SideBarList steps successfully with the following code... Control myContainer = (Control)Wizard1.FindControl("SideBarContainer"); DataList mySideBarList =...
1
7799
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...
0
8080
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6430
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...
1
5604
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...
0
5296
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3737
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...
0
3742
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2226
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1048
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...

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.