Problem with paging in search form

Hello,
I’m learning php not from long time. I just try to create a search form with paging. My search form works good, but i have a problem with the paging. It show me only results from first page. When I click on second page php nothing show me. Can you help me? This is my code:

<?php
$connect = mysql_connect(‘127.0.0.1’,‘root’,‘’) or die(“Ne moje da se svarje s bazata danni”. mysql_error());
mysql_select_db(‘test’) or die(“Ne otkriva bazata danni”. mysql_error());
mysql_query(“SET NAMES ‘utf8’”);

$numrows=‘’;
$zaglavie=‘’;

$pgsize=3;
$total=mysql_query("SELECT COUNT('id') as 'num' FROM stoki");
$total= mysql_fetch_array($total);
echo $total['num'];
$max_pages=ceil($total['num']/$pgsize);
$pg=(isset($_GET["p"]) ? (int) $_GET["p"] : 1); 
$start=($pg-1)*$pgsize; 

$x=1;

if(isset($_POST[‘buton’]) || $x>1){
$field = $_POST[‘field’];
$radio = $_POST[‘radio’];
if($radio==“one”)
{$proba=“”;}
else{
$op = $_POST[‘op’];
$stoinost=explode(“:”,$op);
$proba=“AND (cena>”.$stoinost[0].“) AND (cena<”.$stoinost[1].“)”;
}
if($field==“”){printf("Въведете дума в полето! ");}

		else{ $pole=$field;
				$query = mysql_query("SELECT * FROM stoki WHERE ime_stoka like '%" . $field . "%' ".$proba." LIMIT $start,$pgsize ");
				$numrows = mysql_num_rows($query);
				
				//about result messege without limit
				$query1 = mysql_query("SELECT * FROM stoki WHERE ime_stoka like '%" . $field . "%' ".$proba."");
				$numrows1 = mysql_num_rows($query1);
				
		
				while($row = mysql_fetch_array($query))
				{
					$zaglavie=$row['title'];
					$field=$row['ime_stoka'];
					echo "&lt;a href='#'&gt;$zaglavie&lt;p&gt;&lt;/a&gt;";
					echo "$field&lt;p&gt;&lt;/p&gt;";
				}
				if($max_pages &gt;= 1){
					for($x=1; $x&lt;=$max_pages; $x++){
							echo '&lt;a href="?page='.$x.'"&gt;'.$x.'&lt;/a&gt; ';
						}
				}

		}

}

?>

<?php if(isset($_POST[‘buton’])&&$field){echo “<p>” .$numrows . " from " .$numrows1 . " results found for “. $pole .” </p>";
}
?>

The problem is here.

$pg=(isset($_GET[“p”]) ? (int) $_GET[“p”] : 1);

echo ‘<a href="?page=’.$x.‘">’.$x.'</a> ';

You’re not looking for the same variable you’re sending.

Ok I fixed this one–> $page=(isset($_GET[“page”]) ? (int) $_GET[“page”] : 1); and than I fixed $start=($page-1)*$pgsize;
but not working again. I think that the problem is hire if(isset($_POST[‘buton’]) || $x>1) but I don’t know how to fix it

$_POST[‘buton’])&&$field <-that looks wrong, could be pass by reference?

$_POST[‘buton’]) && $field <-add 2 spaces

I think that the proglem is hire if(isset($_POST[‘buton’]) || $x>1) but I don’t know how to fix it

add a line of debug temporarily


var_dump($_POST['buton']);

Take a look at what is being passed if you are doubtful.

and this is a waste of time as things stand at the moment:

|| $x>1

because you set $x=1; just before it.

When I do this $_POST[‘buton’]) && $field it’s show me Notice: Undefined variable: field in C:\xampp\htdocs\index.php on line 20
I added this line var_dump($_POST[‘buton’]);
but can you explain me what is it used? Just show me this: Notice: Undefined index: buton in C:\xampp\htdocs\index.php on line 19
NULL

I have to do IF (which when I click on the button or when I click on the some number of the page(1,2,3…)){…}but I’m not sure how?

I added this line var_dump($_POST[‘buton’]);
but can you explain me what is it used? Just show me this: Notice: Undefined index: buton in C:\xampp\htdocs\index.php on line 19
NULL

It means that you are trying to reference a variable from your form called ‘buton’, maybe check your html form, could it be called “button” otherwise you have spelt it wrong.

The naming of your variables is critical - they must match up otherwise you will find such errors.

The same goes for $field, you have not set $field anywhere that this script can find, so you cannot read it - hence the notice.

I checked the variables and I didn’t have a problem. And before I tried to use paging my search form working good. (think)

Do you have another proposals for my problem?

Please replace the following code:


$query = mysql_query("SELECT * FROM stoki WHERE ime_stoka like '%" . $field . "%' ".$proba." LIMIT $start,$pgsize ");

with:

$querystring = "SELECT * FROM stoki WHERE ime_stoka like '%" . $field . "%' ".$proba." LIMIT $start,$pgsize ";
echo $querystring;
$query = mysql_query($querystring);

Then please paste the output of your SELECT query here.

I fixed the code. My friend helped me and now paging works :slight_smile:
This is the new code but can you help me how to put ‘…’ in the begining and in the end of the page numbers when you don’t see last page number? For example: …3 4 5…

$numrows=‘’;
$zaglavie=‘’;

$pgsize=3;
$page=(isset($_GET[“page”]) ? (int) $_GET[“page”] : 1);
$start=($page-1)*$pgsize;

if(isset($_REQUEST[‘buton’]))
{
$field = $_REQUEST[‘field’]; $field = htmlentities($field);$field = htmlspecialchars($field,ENT_QUOTES);
$radio = $_REQUEST[‘radio’];

if($radio=="one") $proba=""; 
else
{	
    $op = $_REQUEST['op'];
    $stoinost=explode(":",$op);
    $proba="AND (cena&gt;".$stoinost[0].") AND (cena&lt;".$stoinost[1].")";
}

if($field=="") printf("&#1042;&#1098;&#1074;&#1077;&#1076;&#1077;&#1090;&#1077; &#1076;&#1091;&#1084;&#1072; &#1074; &#1087;&#1086;&#1083;&#1077;&#1090;&#1086;! ");
else
{
    $total=mysql_query("SELECT COUNT('id') as 'num' FROM stoki WHERE ime_stoka like '%{$field}%' {$proba}");
    $total= mysql_fetch_array($total);
    $max_pages=ceil($total['num']/$pgsize);
    
    $pole=$field;
    $query = mysql_query("SELECT * FROM stoki WHERE ime_stoka like '%" . $field . "%' ".$proba." LIMIT $start,$pgsize ");
    $numrows = mysql_num_rows($query);

    //about result messege without limit
    $query1 = mysql_query("SELECT * FROM stoki WHERE ime_stoka like '%" . $field . "%' ".$proba."");
    $numrows1 = mysql_num_rows($query1);

    while($row = mysql_fetch_array($query))
    {
        $zaglavie=$row['title'];
        $field=$row['ime_stoka'];
        echo "&lt;a href='#'&gt;$zaglavie&lt;p&gt;&lt;/a&gt;";
        echo "$field&lt;p&gt;&lt;/p&gt;";
    }

    parse_str($_SERVER['QUERY_STRING'],$_array);
    if($max_pages &gt;= 1)
    {
				
        for($x=$page-1; ; $x++)
        {
			if($x &lt; 1) continue;
			if($x &gt; $page+2 || $x &gt; $max_pages) break;
			
            $_array['page'] = $x;
			if($x!=$max_pages){
				if($x==$page){echo '&lt;strong&gt;&lt;a href="?'.http_build_query($_array).'"&gt;'.$x.'&lt;/a&gt;&nbsp;&lt;/strong&gt;';}
				else{
				echo '&lt;a href="?'.http_build_query($_array).'"&gt;'.$x.'...&lt;/a&gt;&nbsp;';}
			}
        }
   }
}

}

This is the new code but can you help me how to put ‘…’ in the begining and in the end of the page numbers when you don’t see last page number? For example: …3 4 5…

So, lets break your sentence down into logic a bit…

IF you wouldnt see 1, then put some dots before you do the middle section.
IF you wouldnt see the last number, then put some dots after the middle section.

yes,but I am not sure how exactly I should write because I use a cycle and after all number there is a dots