Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7541919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:01:13+00:00 2026-05-30T08:01:13+00:00

Right, day 8 learning php and I feel that I have tried to do

  • 0

Right, day 8 learning php and I feel that I have tried to do things in the wrong order…. I was fine up until yesterday. I have set myselft the task of creating a fake online book shop (seems popuar with all learn php books). First I created my products and listed them all on a page from the database.. easy, then I used a dropdown box to order the results… hard but did it eventually. Then tried to paginate, easy, then tried to join my ordering code and pagination code: most painful few hours of my life but complete. The I utilised a search bar.. easier then thought, an now I am trying to mix this search bar with my pagination and ordering code and my hair is falling out by the handful! I really wish I had attempted to learn all three at once but would have been a bit of a handful/headful.

So my problem is… It paginates… yay, it orders… yay however when I first see the results from a search… It paginates ok (shows me how many pages there are which is always the correct value) however when I click to view the 2nd page it the stops showing me the results of the search, instead takes me to page 2 of ALL products in the database. The same also happens if I try and order the list, it changes to an ordered list of all the products and tells me the search criteria is empty!

Sorry for such a long winded question, I just cant work out where I have slipped up!

Thank you soo much for anybody who can help!

Heres the code:

booksearch.php:

(have taken out all the irrelevent code!)

<?php $query1 = "SELECT *
      FROM booktable";

// execute the query

$results1 = mysql_query($query1)
                or die(mysql_error());

// count the number of rows that are selected from the table

$field = mysql_num_fields( $results1 );

// echo $field;

        for ( $i = 0; $i < $field; $i++ ) 
    {
         $names[$i] = mysql_field_name( $results1, $i );
        }
?>
<form name="searchbook" action="productssearch.php" method="get">
    <br />
    Got For It:<input type="text" id="searchbar" name="searchterm" value="Enter anything and we will search on title, author, publisher and ISBN"></input>
    <br /><br />
    <input type="submit" name="Search" value=" - Search - "></input>
  </form>

productssearch.php:

require "dbconn.php";
$term = $_GET['searchterm'];
$records_per_page = 10;
//If user set the sort order, save to session var - else set default
if(@$_POST['order'])
{    
$_SESSION['order'] = trim($_POST['order']);
}

elseif(!isset($_SESSION['order']))
{    
$_SESSION['order'] = 'bookname';
}

//Determine the total records and pages
$query = mysql_query ("SELECT COUNT(bookname)
          FROM booktable
          WHERE bookname LIKE '%".$term."%'
          OR bookisbn LIKE '%".$term."%'
          OR bookpub LIKE '%".$term."%'
          OR bookauthor LIKE '%".$term."%'");



$total_records = mysql_result($query, 0);
$total_pages = ceil($total_records / $records_per_page);

//Set the page to load
$page = (isset($_GET['page'])) ? (int) $_GET['page'] : 1;
if($page<1 || $page>$total_pages)
{    
$page = 1;
}

//Create limit var for query
$limit_start = ($page-1) * $records_per_page;

//Create and run query for records on current page
$query = mysql_query ("SELECT bookname, bookauthor, bookpub, bookisbn
          FROM booktable
          WHERE bookname LIKE '%".$term."%'
          OR bookisbn LIKE '%".$term."%'
          OR bookpub LIKE '%".$term."%'
          OR bookauthor LIKE '%".$term."%'          
          ORDER BY ".mysql_real_escape_string($_SESSION['order'])." ASC
          LIMIT $limit_start, $records_per_page");


//Prepare output
//**************************************************************
$numrow = mysql_num_rows($query);  
//Create pagination links
$navLinks = '';

//Prev page
if($page > 1)
{    
$prevPage = $page - 1;    
$navLinks .= "<a href='productssearch.php?page=$prevPage'>Prev</a> ";
}
else
{    
$navLinks .= "Prev ";
}

//Individual pages
for($p=1; $p<=$total_pages; $p++)
{
$pageNo = ($p == $page) ? "<b>{$p}</b>" : $p;
$navLinks .= "<a class='pagin' href='productssearch.php?page=$p'>$pageNo</a> ";
}

//next page
if($page < $total_pages)
{    
$nextPage = $page + 1;    
$navLinks .= "<a href='productssearch.php?page=$nextPage'>Next</a>";
}
else
{    
$navLinks .= "Next";
}
?>

(IRRELEVANT STUFF FROM MIDDLE REMOVED)

<div id="mid"> 
<?php

echo "<table>";
echo "<tr>";
echo "<td>";

echo "</td>";
echo "<td class='toprow'>";
echo "Book Title";
echo "</td>";
echo "<td class='toprow'>";
echo "Book Author";
echo "</td>";
echo "<td class='toprow'>";
echo "Book Publisher";
echo "</td>";
echo "<td class='toprow'>";
echo "Book ISBN";
echo "</td>";
echo "<td>";

echo "</th>";
echo "</tr>";
while ($row = mysql_fetch_assoc($query))
{
 $bookname = $row['bookname'];
 $bookauthor = $row['bookauthor'];
 $bookpub = $row['bookpub'];
 $bookisbn = $row['bookisbn'];
  // send the values to the browser as a row in a html table
  echo "<tr>";

  echo "<tr>";
  echo "<td>";
  echo "<a href='addtolist.php?bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>Add to basket</a>";

  echo "</td>";

 echo "<td>";
  echo $bookname;
  echo "</td>";

  echo "<td>";
  echo $bookauthor;
  echo "</td>";

  echo "<td>";
  echo $bookpub;
  echo "</td>";

  echo "<td>";
  echo $bookisbn;
  echo "</td>";

  echo "</tr>";


}
?>
</table>


<?php 
echo "<br />";
echo $navLinks; ?>

Please note I am a real beginner so I am not learning how to make secure or safe code.,.. I havnt even encrypted user and admin passwords yet… just trying to learn the mechanics of the script!

Thank you all again for taking the time to read my “windy” question!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:01:15+00:00Added an answer on May 30, 2026 at 8:01 am

    To all who are reading this: I’m keeping it SIMPLE, as Phil has commented he’s a beginner. We all know there are considerations that are NOT being taken into account in this answer!

    Phil, the answer lies in the construction of your navlinks code.

    When you click one of the navlinks, it passes the limits for your pagination, but does NOT pass the search term.

    You would need to modify your navlinks to be along the lines of:

    $navLinks .= "<a href='productssearch.php?page=$prevPage&searchterm=$term'>Prev</a> ";
    

    and

    $navLinks .= "<a class='pagin' href='productssearch.php?page=$psearchterm=$term'>$pageNo</a> ";  
    

    and

    $navLinks .= "<a href='productssearch.php?page=$nextPage&searchterm=$term'>Next</a>";  
    

    etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day! I right now have a function the drags an element from a
I have an application that has been running fine since its launch over a
I have a program that generates the following output: ┌───────────────────────┐ │10 day weather forecast│
G'day all, After learning on here that the Java console doesn't support keyboard input
I have been learning about move constructors over the last day or so, trying
G'Day Mates - What is the right way (excluding the argument of whether it
Sooo...it's only sort of programming related, but I figure it's election day, right? Is
Right now, I keep all of my projects on my laptop. I'm thinking that
Right, initially ran: c:\regsvr32 Amazing.dll then, (accidentally - I might add) I must have
Right now I have a database (about 2-3 GB) in PostgreSQL, which serves as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.