Jump to content
#StayAtHome ×

Archived

This topic is now archived and is closed to further replies.

tapantor24

Solving Search function with pagination problem

Recommended Posts

Look at this code. This is search function with pagination. I want to use this code in project of my client but problem is that the search result show on first page normally but when i press next button it's going wrong. It's show no category found. I spend many tiime to solve this but result is zero. Pls help me any one to solve thise..

 

<?php

//include out functions file giving us access to the protect() function made earlier

 

include "./include/database.php";

?>

 

<?php

 

$phone = ($_POST['phone']);

 

$productsPerRow = 2;

$perpage = 10;

 

if(isset($_GET["page"]))

 

{

 

$page = intval($_GET["page"]);

 

}

 

else

 

{

 

$page = 1;

 

}

 

$calc = $perpage * $page;

 

$start = $calc - $perpage;

$result = mysql_query("SELECT * FROM `bookmark` WHERE `phone` = '$phone' Limit $start, $perpage");

$rows = mysql_num_rows($result);

$columnWidth = (int)(100 / $productsPerRow);

if($rows>0){

$i=0;

?>

 

<table width='100%' border='0' align='center' cellpadding='5' cellspacing='1' class='bookmark'>

 

 

<?php

while($row = mysql_fetch_array($result))

{

//$row['Photo'] = "<img src="/user_image/ . $row['Photo']; ""/>

if ($i % $productsPerRow == 0) {

 

?>

<tr >

<?php

}

$Sub =$row['Sub'];

$url=$row['Link'];

$Phone =$row['phone'];

$info =$row['info'];

?>

<td width="$columnWidth%" >Subject:

<?php echo $row['Sub'];?><br/>

 

Link:

<?php echo $row['Link'];?><br/>

 

 

Mobile:

<?php echo $row['phone'];?><br/>

 

Information: <?php if($info != '') { ?>

<?php echo $row['info'];?>

<?php }else {

 

echo "N/A";

 

 

} ?>

</td>

<?php

if ($i % $productsPerRow == $productsPerRow - 1) {

echo '</tr>';

}

 

$i += 1;

}

if ($i % $productsPerRow > 0) {

echo '<td colspan="' . ($productsPerRow - ($i % $productsPerRow)) . '"> </td>';

}

 

} else {

?>

No products in this category

<?php

}

?>

</table>

 

<table width="100%" cellspacing="2" cellpadding="2" align="center" >

 

<tr>

 

<td align="center">

 

<?php

 

 

 

if(isset($page))

 

{

 

$result = mysql_query("select Count(*) As Total from bookmark WHERE `phone` = '$phone'");

 

$rows = mysql_num_rows($result);

 

if($rows)

 

{

 

$rs = mysql_fetch_array($result);

 

$total = $rs["Total"];

 

}

 

$totalPages = ceil($total / $perpage);

 

if($page <=1 )

 

{

 

echo "<span id='page_links' style='font-weight:bold;'>Prev</span>";

 

}

 

else

 

{

 

$j = $page - 1;

 

echo "<span><a id='page_a_link' href='info5.php?page=$j'>< Prev</a></span>";

 

}

 

for($i=1; $i <= $totalPages; $i++)

 

{

 

if($i<>$page)

 

{

 

echo "<span><a href='info5.php?page=$i' id='page_a_link'>$i</a></span>";

 

}

 

else

 

{

 

echo "<span id='page_links' style='font-weight:bold;'>$i</span>";

 

}

 

}

 

if($page == $totalPages )

 

{

 

echo "<span id='page_links' style='font-weight:bold;'>Next ></span>";

 

}

 

else

 

{

 

$j = $page + 1;

 

echo "<span><a href='info5.php?page=$j' id='page_a_link'>Next</a></span>";

 

}

 

}

 

?>

 

<td>

 

</tr>

 

</table>

<form action="info5.php" method="POST">

<input name="phone" id="phone" type="text" size="25" class="box" value="Type Mobile No…" onfocus="this.value=(this.value=='Type Mobile No…')? '' : this.value ;" />

<input type="submit" name="submit" class="box" id="submit" value="Search" />

</form>

Share this post


Link to post
Share on other sites

If you're sure that products *should* be found on a second page, have the script echo the values of $start and $perpage to you just before the query ... it may be that these values are not what you expect.

Also, please consider using the [ code ] BB tags for your code ;)

Share this post


Link to post
Share on other sites

It's a long database. It's will show 10 result each page. First page going fine but next show no category found. It's also going fine when i replace this WHERE `phone` = '$phone' to WHERE ’phone’ = ’01736659047 ’ or manually type any number from the database.

Share this post


Link to post
Share on other sites

web servers are stateless. they don't have any idea what happened on any page request before or after the current one.

 

you $phone value will only exist on the page request that the search form was submitted to. you need to pass the $phone value in the pagination links so that it is available on those page requests.

Share this post


Link to post
Share on other sites

Pls show the code how it will be. Thx

Come on now friend ... it's not THAT hard ;)

 

//lines like these will need changed:

echo "<span><a id='page_a_link' href='info5.php?page=$j'>< Prev</a></span>"; //needs phone parameter
echo "<span><a href='info5.php?page=$i' id='page_a_link'>$i</a></span>"; //needs phone parameter
 
//to this
echo "<span><a id='page_a_link' href='info5.php?page=$j&phone=$phone'>< Prev</a></span>"; //has phone parameter
echo "<span><a href='info5.php?page=$i&phone=$phone' id='page_a_link'>$i</a></span>"; //has phone parameter
 
//At the top, because you're only POSTing the first time (and using GET thereafter), you will need something like:
if (isset($_POST['phone'])) {
   $phone = $_POST['phone'];
 
} elseif (isset($_GET['phone']) {
 
   $phone = $_GET['phone'];
 
}

 

And you probably should consider some type of validation on the phone variable as well, unless you're certain that it won't be viewed by anyone except people in your own company.

Share this post


Link to post
Share on other sites

Many many thx friend @dalecosp

It's solve my problem. But how I apply it in many condition. Such as age between age. Thanks again.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.