Jump to content


Photo

Solving Search function with pagination problem


Best Answer dalecosp, 05 December 2013 - 03:07 PM

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&amp;phone=$phone'>< Prev</a></span>"; //has phone parameter

echo "<span><a href='info5.php?page=$i&amp;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.

Go to the full post


  • Please log in to reply
8 replies to this topic

#1 tapantor24

tapantor24

    Member

  • Members
  • PipPip
  • 12 posts

Posted 04 December 2013 - 07:33 AM

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)) . '">&nbsp;</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&hellip;" onfocus="this.value=(this.value=='Type Mobile No&hellip;')? '' : this.value ;" />
<input type="submit" name="submit" class="box" id="submit" value="Search" />
</form>

#2 dalecosp

dalecosp

    Advanced Member

  • Members
  • PipPipPip
  • 300 posts
  • LocationJoplin, MO

Posted 04 December 2013 - 09:31 AM

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 ;)


Edited by dalecosp, 04 December 2013 - 09:32 AM.

"God doesn't play dice" --- Albert Einstein
"Perl is hardly a paragon of beautiful syntax." --- Weedpacket

#3 tapantor24

tapantor24

    Member

  • Members
  • PipPip
  • 12 posts

Posted 04 December 2013 - 08:27 PM

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.

Edited by tapantor24, 04 December 2013 - 08:30 PM.


#4 mac_gyver

mac_gyver

    Advanced Member

  • Administrators
  • 2,242 posts

Posted 05 December 2013 - 05:34 AM

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.


multi-purpose programming fool and resident naysayer [We try not be negative in replies, but telling someone what they're doing wrong, while staying politically correct, isn't always going to happen.]

#5 tapantor24

tapantor24

    Member

  • Members
  • PipPip
  • 12 posts

Posted 05 December 2013 - 09:06 AM

Pls show the code how it will be. Thx

#6 White_Lily

White_Lily

    Advanced Member

  • Members
  • PipPipPip
  • 521 posts

Posted 05 December 2013 - 10:12 AM

is the search terms stored in a session or is the whole ting powered by javascript?


"In order to be irreplaceable, one needs to be different from another."

CMS Development: http://cms.janedeaslart.co.uk/


#7 mac_gyver

mac_gyver

    Advanced Member

  • Administrators
  • 2,242 posts

Posted 05 December 2013 - 10:29 AM

i recommend using http_build_query() to build the url query string from various different sources (i.e. a search value and pagination values) - ref: http://it1.php.net/http_build_query


multi-purpose programming fool and resident naysayer [We try not be negative in replies, but telling someone what they're doing wrong, while staying politically correct, isn't always going to happen.]

#8 dalecosp

dalecosp

    Advanced Member

  • Members
  • PipPipPip
  • 300 posts
  • LocationJoplin, MO

Posted 05 December 2013 - 03:07 PM   Best Answer

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&amp;phone=$phone'>< Prev</a></span>"; //has phone parameter

echo "<span><a href='info5.php?page=$i&amp;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.


Edited by dalecosp, 05 December 2013 - 03:08 PM.

"God doesn't play dice" --- Albert Einstein
"Perl is hardly a paragon of beautiful syntax." --- Weedpacket

#9 tapantor24

tapantor24

    Member

  • Members
  • PipPip
  • 12 posts

Posted 06 December 2013 - 03:18 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com