By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,715 Members | 1,773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,715 IT Pros & Developers. It's quick & easy.

NEXT - PREVIOUS

P: n/a
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($_POST['_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($query);
$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($query);

//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='clearboth'>";

//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='navigation'>";
echo "<ul class='horizontal'>";
echo "<li> <a
href='search.php?_searchterm=$newterm?pageno=2'><s pan>NEXT</span></a> </li>
";
echo "</ul>";
echo "</div>";
echo "<br class='clearboth'>";
}
}
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
Share this Question
Share on Google+
3 Replies


P: n/a
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($_POST['_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($query);
$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($query);

//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='clearboth'>";

//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='navigation'>";
echo "<ul class='horizontal'>";
echo "<li> <a
href='search.php?_searchterm=$newterm?pageno=2'><s pan>NEXT</span></a>
</li>
";
echo "</ul>";
echo "</div>";
echo "<br class='clearboth'>";
}
}
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

P: n/a
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($_POST['_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($query);
$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($query);

//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='clearboth'>";

//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='navigation'>";
echo "<ul class='horizontal'>";
echo "<li> <a
href='search.php?_searchterm=$newterm?pageno=2'><s pan>NEXT</span></a> </li>
";
echo "</ul>";
echo "</div>";
echo "<br class='clearboth'>";
}
}
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*******@attglobal.net
==================
Jul 17 '05 #3

P: n/a
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("SELECT * FROM packages WHERE
pkg_id < '$pkg_id'
ORDER BY pkg_id DESC LIMIT 1", $link);
if ($prevrow = @mysql_fetch_array($prevresult)) {
$prevpkg_id=$prevrow[pkg_id];
} else {
$prevpkg_id=0; // signifies no entry
}

//Is there a next package?
$nextresult = mysql_query("SELECT * FROM packages WHERE
pkg_id > '$pkg_id'
ORDER BY pkg_id LIMIT 1", $link);
if ($nextrow = @mysql_fetch_array($nextresult)) {
$nextpkg_id=$nextrow[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("SELECT * 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_array($prevresult)) {
$prevpkg_id=$prevrow[pkg_id];
} else {
$prevpkg_id=0;
}

//Is there a next package?
$nextresult = mysql_query("SELECT * 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_array($nextresult)) {
$nextpkg_id=$nextrow[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 discussion thread is closed

Replies have been disabled for this discussion.