html form

// This is the dynamic drop down menu
<form action="search.php" method="post">

<?php
$select_query        =  "Select distinct category from books";
$select_query_run    =   mysql_query($select_query);
$select_query_array  =   mysql_fetch_array($select_query_run) ;
 $cat = $select_query_array['category'];
echo "<select name='name'>";
while ($select_query_array=   mysql_fetch_array($select_query_run) )
{

  echo "<option value='".htmlspecialchars($select_query_array["category"])."' >".htmlspecialchars($select_query_array["category"])."</option>";
}

echo "</select>";
?>
// search field with search button
   <input type="text" name="searchvalue" size="5" id="searchfield" title="searchfield" onFocus="clearText(this)"/>
     <input type="submit" name="submit" value="" alt="Search" id="searchbutton" title="Search" />
</form>

SEARCH.PHP file

<?php 
@session_start();
include 'connections.php';
$search = $_POST['searchvalue'];
$name = $_POST['name'];
if(strlen($search)<=1)
{


echo "Search term too short";
}
else{
echo "You searched for <b>$search</b> <hr size='1'></br>";

$construct = "SELECT * FROM books WHERE category = '$name'  AND (title LIKE '%$search%' or author LIKE '%$search%')";    


$run = mysql_query($construct);

$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1.";
else
{
echo "$foundnum results found !<p>";
 }

$per_page = 2;
$start = $_GET['start'];// getting page number from url,
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0;
$getquery = mysql_query("SELECT * FROM books WHERE $construct LIMIT $start, $per_page");
while($runrows = mysql_fetch_assoc($run)) // This while loop will prints search reasults,
{
echo '<div class="news_box">
                                <a href="#">';
                                if($runrows ['cover_page_img']=="")
                                {
                                    echo '<img class="news_image" src="'.INDEX_URL.'images/book_open.jpg" alt="image" width="90" height="90"/>';
                                }
                                else{
                                    echo '<img class="news_image" src="'.INDEX_URL.'images/BookCoverImgs/'.$row['cover_page_img'].'" alt="image" width="90" height="90"/>';
                                }
                                echo '                          
                                </a>
                            <h3><a href="#">'.$runrows ['title'].'</a></h3>
                                <p> '.substr($runrows ['edition'],0,150).'.......</p>
                                <div class="more float_r"><a href="#">Read more</a></div>
                            <div class="cleaner"></div>
                            </div>';

 }
$prev = $start - $per_page;
$next = $start + $per_page;

$adjacents = 3;
$last = $max_pages - 1;

if($max_pages > 1)
{  
//previous button
if (!($start<=0))
echo " <a href='search.php?search=$search&submit&start=$prev'>Prev</a> ";   

//pages
if ($max_pages < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
{
$i = 0;  
for ($counter = 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='search.php?search=$search&submit=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit&start=$i'>$counter</a> ";
} 
$i = $i + $per_page;                
}
}
elseif($max_pages > 5 + ($adjacents * 2))    //enough pages to hide some
{
//close to beginning; only hide later pages
if(($start/$per_page) < 1 + ($adjacents * 2))       
{
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($i == $start){
echo " <a href='search.php?search=$search&submit=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=$i'>$counter</a> ";
}
$i = $i + $per_page;                                      
}

}
//in middle; hide some front and some back
elseif($max_pages - ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
{
echo " <a href='search.php?search=$search&submit=0'>1</a> ";
echo " <a href='search.php?search=$search&submit&start=$per_page'>2</a> .... ";

$i = $start;                
for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
{
if ($i == $start){
echo " <a href='search.php?search=$search&submit&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit&start=$i'>$counter</a> ";
}  
$i = $i + $per_page;               
}

}
//close to end; only hide early pages
else
{
echo " <a href='search.php?search=$search&submit=0'>1</a> ";
echo " <a href='search.php?search=$search&submit&start=$per_page'>2</a> .... ";

$i = $start;               
for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='search.php?search=$search&submit&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit&start=$i'>$counter</a> ";  
}
$i = $i + $per_page;             
}
}
}

//next button
if (!($start >=$foundnum-$per_page))
echo " <a href='search.php?search=$search&submit=$next'>Next</a> ";   
}  
echo "</center>";
}

I AM GETTING SEARCH RESUTLS SUCCESSFULLY, BUT I WANT TO PRINT ONLY TWO RESULTS PER PAGE MY PROBLEM IS ITS PRINTING ALL RESULTS IN ONE PAGE BUT I AM GETTING PAGINATION RESULTS (LIKE "1 2 NEXT") UNDER THE PAGE, WHY IS THIS HAPPINING PLEASE ANYONE HELP ME BY ANALYSING MY CODE..

Recommended Answers

All 14 Replies

Have you checked and validated the values of your $start and $per_page variables?

On a side note
Also, your query isn't secure. You should escape user input in queries using mysql_real_escape_string(). Better yet, you should use mysqli, because the "regular" mysql functions are deprecated.

I don't know how to validated tose variables

Well, before they are used, you could execute some code like:

echo '<p>The value of $start is: ' . $start . ', and the value of $per_page is: ' . $per_page . '</p>';

for every search I am getting below output..
The value of $start is: 0, and the value of $per_page is: 1

Well, it looks like only one record should be returned per page load then. Can you confirm that this loop

while($runrows = mysql_fetch_assoc($run)) // This while loop will prints search reasults,

is indead only run one time - for one search result? (Before the line, add $results_loop_counter = 0;, and at the end of the while() loop, add $results_loop_counter++;. Then, after the while() loop, add echo 'Times run: ' . $results_loop_counter . '<br>';).

not its not running one time for one search result, (ex: if for xyz search it have 3 records and loop is running 3 times for single search and printing all 3 results in single page) I placed your code and o/p is
Times run: 3

Ah, I see what you're probably doing wrong now. Your $construct query is ok (apart from the fact that you're not properly escaping user input). Your $getquery function is not ok, though: you insert a whole SELECT query into your WHERE clause, which is not what you want, if I look at the rest of your code. Therefore, you may want to replace your $getquery line with the following:

$query_with_limit = $construct . ' LIMIT ' . mysql_real_escape_string($start) .',' . mysql_real_escape_string($per_page);
$getquery = mysql_query($query_with_limit);

and the $run in

while($runrows = mysql_fetch_assoc($run)) // This while loop will prints search reasults,

by $getquery. Could you check if that makes a difference?

cool.. and yes its make difference I am almost at end but again the problem is if i click next button, always printing "Search term is short" eventhough I have records in next page.. (ex: if, for xyz results i have 3 results and i set 2 records per page and its working fine but the remaining result is not printing if i click next button)

Well, obviously you need to pass on a parameter when the "next" button is pressed that tells you which page you're loading. Then you need to do page_number * results_per_page to caclulate the offset for your query.

will you please help me how to do that??

$offset = ($start- 1) * $per_page;
      $query_with_limit = $construct .$offset . ' LIMIT ' . mysql_real_escape_string($start) .',' . mysql_real_escape_string($per_page);
    $getquery = mysql_query($query_with_limit);

I used above code it's not working
even I tried this

 $offset = ($start- 1) * $per_page;
    $query_with_limit = $construct . ' LIMIT ' . mysql_real_escape_string($offset) .',' . mysql_real_escape_string($per_page);
$getquery = mysql_query($query_with_limit);

This time I am getting error

Well, if you are getting an error, it would be helpful if you would post that error so that we can see what goes wrong, where, and why :).

On top of that, I can help you build your scripts, but I cannot build your scripts for you. Have you written all the code in your first post yourself? Do you understand what is happening? If so, what do you not understand in my previous post? I'll quote that post below:

Well, obviously you need to pass on a parameter when the "next" button is pressed that tells you which page you're loading. Then you need to do page_number * results_per_page to caclulate the offset for your query.

I changed code to this.

if($start)
$start= ($start- 1) * $per_page;
else
$start=0;

   $query_with_limit = $construct . ' LIMIT ' . mysql_real_escape_string($start) .',' . mysql_real_escape_string($per_page);
    $getquery = mysql_query($query_with_limit);

getting search results and printing results as we set in $per_page. but my problem is when I click next button I am not getting remaining records in next page. I next page it is printing "search term too short"

Yes, that is because when you press "next", a new page is loaded and no post values are submitted, which means that $search will be empty on the next page load. Hence the "search term too short" message.

Stuff that gets submitted through the URL cannot be retrieved using $_POST; you need to use $_GET.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.