Hi, I have a paginations script to display data from my database but i would like to paginate someones search results. I have this script but get an error that says "syntax error, unexpected '=', expecting '}'" for this if(!$start){$start = 0;}

<?php
include ("connect.php");
$per_page = 20;
$start = $_GET ['start'];
$record_count = mysql_num_rows (mysql_query("SELECT * FROM flats WHERE `location` LIKE '%$keyword%' OR
`location` LIKE '%$keyword%'));
$max_pages = $record_count / $per_page; // may come out as a decimal
if(!$start){$start = 0;}
$get = mysql_query("SELECT * FROM flats WHERE `location` LIKE '%$keyword%' OR
`location` LIKE '%$keyword%' ORDER BY date_posted DESC LIMIT $start, $per_page");
?>

Recommended Answers

All 18 Replies

Hey.

You forgot to close the string on line #6 of what you posted.

`location` LIKE '%$keyword%'"));

(The red " is the one you forgot)

thanks, I have one more problem. the prev 1 2 3 4 next is not giving the result i want. I get the error msg:
Notice: Undefined index: submit, Notice: Undefined index: search
you didnt submit a keyword.

I know that when i press page 2 it reads search.php again but I dont know how to solve it.

<header>
<?php
include ("connect.php");

$per_page = 20;
$start = $_GET ['start'];
$record_count = mysql_num_rows (mysql_query("SELECT * FROM flats WHERE `location` LIKE '%$keyword%' OR
`location` LIKE '%$keyword%'"));
$max_pages = $record_count / $per_page; // may come out as a decimal
if(!$start){$start =0;}
$get = mysql_query("SELECT * FROM flats WHERE `location` LIKE '%$keyword%' OR
`location` LIKE '%$keyword%' ORDER BY date_posted DESC LIMIT $start, $per_page");
?>

</header>
<body>
<td>
<tr>
<?php
 
include ('connect.php');
 
error_reporting(E_ALL);
ini_set('display_errors', '1');
 
$submit = $_GET['submit'];
$search = $_GET['search'];
$x=0;
$construct='';
$foundnum=0;  

if (!$submit)
   
  echo "you didnt submit a keyword.";  
 
else
 
{
 
if (strlen($search)<=2)
 
   echo "search term to short.";
 else  
{
  echo " You searched for <b>$search</b><hr size='1'>";
 
  //connect to our database
 
 $search_exploded = explode(" ",$search);
 
 
 foreach($search_exploded as $search_each)
 
{
 
// construct query
 
$x++;
if ($x==1)
    $construct .= " location LIKE '%$search_each%'";  
    else
    $construct .= " OR location LIKE '%$search_each%'";
     
      }

   // echo out construct
   
 $construct = "SELECT * FROM flats WHERE $construct";
 $run = mysql_query($construct);
 $foundnum = mysql_num_rows($run);
 
 
if ($foundnum==0)
  echo "No results found.";
else
{
   echo "$foundnum result found!<p>";
 while ($runrows = mysql_fetch_assoc($run))
 
{
 
// get data
 
   $select = $runrows['type'];
   $title = $runrows['title'];
   $location = $runrows['location'];
   $rent = $runrows['rent'];
   $description = $runrows['description'];
   $contactEmail = $runrows['contactEmail'];
   $number = $runrows['number'];
 
echo "
 
    $title
    <br>
    $select
    <br>
    $rent
    <br>
    $location
    <br>
    $description
    <br>
    $contactEmail
    <br>
    $number
   <hr>";
 
}      
 
 
      }
    }
  }
 
 
?>

</td>
</tr>

     <tr align="center">
     <td>
     
      <?php
$prev = $start - $per_page;
$next = $start + $per_page;

      if($start>0){
      echo "<a href='search.php?start=$prev'> Prev </a>";}
      $i=1;
      for ($x=0;$x<$record_count;$x=$x+$per_page)
      {
       if($start!=$x){
       echo "<a href='search.php?start=$x'>$i | </a>";
      }
       else{
      echo "<a href='search.php?start=$x'><b> $i | </b></a>";}
       $i++;
       }
      if($start<$record_count-$per_page){echo "<a href='search.php?start=$next'> Next </a>";}
      ?>

Just a note:
When you find the number of pages, you want to find the ceiling (or highest integer) of the division since 1.5 of a page is actually two.

$pages = ceil($item_count / $per_page);

Also, does the error message (notice) given you a line number?

i think you arenot pass searching  text for every page. you are passing only start veriable only. check once
i think you arenot pass searching  text for every page. you are passing only start veriable only. check once

What?

i think , when we click 2nd page the veraable $keyword,$search_each are null values.

$keyword,$search_each

. the results are not geting properly. if i am wrong leave this post and sorry ,if i am misguide you.

No that's perfectly correct, I just had a little trouble understanding you. So with that in mind, the links to other pages should include the search parameter. I don't know where the $keyword variable comes from, but it also should remain constant throughout the pagination.

Undefined index: submit in search.php on line 232

Notice: Undefined index: search in search.php on line 233
you didnt submit a keyword.

line 232,233 is
$submit = $_GET;
$search = $_GET;

When i press page 2 i think it reads the script again. i want this script to display the searched result only rather then all the data. How can i make the script paginate just the search result.

<?php
$prev = $start - $per_page;
$next = $start + $per_page;
 
      if($start>0){
      echo "<a href='search.php?start=$prev'> Prev </a>";}
      $i=1;
      for ($x=0;$x<$record_count;$x=$x+$per_page)
      {
       if($start!=$x){
       echo "<a href='search.php?start=$x'>$i | </a>";
      }
       else{
      echo "<a href='search.php?start=$x'><b> $i | </b></a>";}
       $i++;
       }
      if($start<$record_count-$per_page){echo "<a href='search.php?start=$next'> Next </a>";}
      ?>

Isn't this just a matter of putting the search string into the navigation links?

if($start>0) {
    echo "<a href='search.php?start=$prev&search=$search'> Prev </a>";
}

$i=1;
for ($x=0;$x<$record_count;$x=$x+$per_page) {
    if($start!=$x) {
        echo "<a href='search.php?start=$x&search=$search'>$i | </a>";
    }
    else {
        echo "<a href='search.php?start=$x&search=$search'><b> $i | </b></a>";
    }
    $i++;
}

if($start<$record_count-$per_page) {
    echo "<a href='search.php?start=$next&search=$search'> Next </a>";
}

You would also have to change line #32 to:

if (!$search)

(Empty strings are interpreted as false )

And, since you are testing the $search value later in the code, it would be OK to just silence the warnings for the empty indexes.

$submit = @$_GET['submit'];
$search = @$_GET['search'];

You could probably even remove the $submit altogether and just use $search .
(You should always avoid using submit buttons to test if a form was submitted. They are not always passed with the form.)

still the same problem when i press page 2, Undefined index: search in search.php on line 232
you didnt submit a keyword.

How does the URL look like after you press 2?

How does the URL look like after you press 2?

like this ; search.php?start=20

$per_page = 20;

it want to display the first page again but fails because there is no search. I want page 2 to display 20+ from the same search and page 3 40+.

any suggestion?

Then append the search variable to all of the pagination links. If the keyword is what's missing, also append this and add a snippet at the top to get the keyword from the URL like this:

if(isset(@$_GET['keyword']) $keyword = $_GET['keyword'];

If your code is still throwing notices like this when you allow users to access it, I would suggest turning error reporting off...

error_reporting(0); //Turn off all errors
error_reporting(E_ALL ^ E_NOTICE); //Allow fatal errors, but mute notices, this is the default setting for most PHP installations

If you do hide errors from users, but still want to be able to see them yourself, you can set up an errorlog file. Suppressing problem functions with @ will also work too as long as you don't have any surprise problems.

Hi, i have changed my code to the if(ISSET) but i still get the url start=20. sorry this is getting long winded.

<header>
<?php
include ("connect.php");

$per_page = 20;
$start = $_GET ['start'];
$record_count = mysql_num_rows (mysql_query("SELECT * FROM flats WHERE `location` LIKE '%$keyword%' OR
`location` LIKE '%$keyword%'"));
$max_pages = $record_count / $per_page; // may come out as a decimal
if(!$start){$start =0;}
$get = mysql_query("SELECT * FROM flats WHERE `location` LIKE '%$keyword%' OR
`location` LIKE '%$keyword%' ORDER BY date_posted DESC LIMIT $start, $per_page");
?>

</header>
<body>
<?php
 
include ('connect.php');
 
error_reporting(E_ALL);
ini_set('display_errors', '1');

$search = $_GET['search'];
$x=0;
$construct='';
$foundnum=0;  

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

if(isset($_GET['keyword'])){
 $keyword = $_GET['keyword'];

}else{

$keyword = $search;

}

if (strlen($search)<=2)
 
   echo "search term to short.";
 else  
{
  echo " You searched for <b>$search</b><hr size='1'>";
 
//connect to our database
 
 $search_exploded = explode(" ",$search);
 
foreach($search_exploded as $keyword)
 
{
 
// construct query
 
$x++;
if ($x==1)
    $construct .= " location LIKE '%$keyword%'";  
    else
    $construct .= " OR location LIKE '%$keyword%'";
     
      }
// echo out construct
   
 $construct = "SELECT * FROM flats WHERE $construct";
 $run = mysql_query($construct);
 $foundnum = mysql_num_rows($run);
 
 
if ($foundnum==0)
  echo "No results found.";
else
{
   echo "$foundnum result found!<p>";


 while ($runrows = mysql_fetch_assoc($run))
 
{
 
// get data
 
   $select = $runrows['type'];
   $title = $runrows['title'];
   $location = $runrows['location'];
   $rent = $runrows['rent'];
   $description = $runrows['description'];
   $contactEmail = $runrows['contactEmail'];
   $number = $runrows['number'];
 
echo "
 
    $title
    <br>
    $select
    <br>
    $rent
    <br>
    $location
    <br>
    $description
    <br>
    $contactEmail
    <br>
    $number
   <hr>";
 
}      
 
 
      }
 }   
  
 
 
?>

</td>
</tr>

     <tr align="center">
     <td>
     
      <?php

      if($start>0){
      echo "<a href='search.php?start=$prev'> Prev </a>";}
      $i=1;
      for ($x=0;$x<$record_count;$x=$x+$per_page)
      {
       if($start!=$x){
       echo "<a href='search.php?start=$x'>$i | </a>";
      }
       else{
      echo "<a href='search.php?start=$x'><b> $i | </b></a>";}
       $i++;
       }
      if($start<$record_count-$per_page){echo "<a href='search.php?start=$next'> Next </a>";}
      ?>
    
    </td>
    </tr>
   </table>
</body>
$search = $_GET['search']; ['start'];

I have tried to locate the variable search on submission but I cannot see it. It should be submitted via get. I'm I overlooking it somewhere in posted code?

$search = $_GET['search']; ['start'];

I have tried to locate the variable search on submission but I cannot see it. It should be submitted via get. I'm I overlooking it somewhere in posted code?

line 24

Might you try this?

<?php

      if($start>0){
      echo "<a href='search.php?start=$prev&search=$search'> Prev </a>";}
      $i=1;
      for ($x=0;$x<$record_count;$x=$x+$per_page)
      {
       if($start!=$x){
       echo "<a href='search.php?start=$x&search=$search'>$i | </a>";
      }
       else{
      echo "<a href='search.php?start=$x&search=$search'><b> $i | </b></a>";}
       $i++;
       }
      if($start<$record_count-$per_page){echo "<a href='search.php?start=$next&search=$search'> Next </a>";}
      ?>

Hi the problem is solved and it was in changing $record_count to $max_pages, thanks for your help.

<?php

      if($start>0){
      echo "<a href='search.php?start=$prev&search=$search'> Prev </a>";}
      $i=1;
      for ($x=0;$x<[B]$max_pages[/B];$x=$x+$per_page)
      {
       if($start!=$x){
       echo "<a href='search.php?start=$x&search=$search'>$i | </a>";
      }
       else{
      echo "<a href='search.php?start=$x&search=$search'><b> $i | </b></a>";}
       $i++;
       }
      if($start<[B]$max_pages[/B]-$per_page){echo "<a href='search.php?start=$next&search=$search'> Next </a>";}
?>
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.