|
Posted by Tony Marston on 06/05/05 11:29
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" <info@nospam-bluegumdata.com.au> wrote in message
news:Qlyoe.3134$F7.394@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'><span>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>";
> }
> }
>
> ?>
>
>
Navigation:
[Reply to this message]
|