User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 428,375 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,510 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 805 | Replies: 25 | Solved
Reply
Join Date: Apr 2008
Posts: 48
Reputation: helraizer is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 3
helraizer helraizer is offline Offline
Light Poster

Re: Problem with page navigation

  #11  
Jun 3rd, 2008
Originally Posted by Suhacini View Post
Actually am using this for search.In my db I am storing few words A-Z but not in alphbetic order.so when i wanted to search 'A',then all data from my db related to 'A' must display.Here am able to display A1 to A10 in 1st page.In 2nd page it should start with A11 but it is displaying the data from my db whose Id=11.Am I clear to you now?


I see, that makes a bit more sense now. So 11 could be K or something.. Well, I think your search is working perfectly (that I know of) but you're using LIKE %$trimmed%

Let's say that $trimmed is your search for A, it should return A1 - A10 then A11-A20 (or w/e) then if where id 11 dispname is TALC 'N' POWDER - it'll pick it up because it has an A in it. the %A% means anything before the A and anything after the A so TALC would get in but ZINC wouldn't. So if you just want to narrow your search to A--- on disp name.

Use this:

  1. <?php
  2.  
  3. include ('conn.php');
  4.  
  5.  
  6. // if $_GET['page'] defined, use it as page number
  7. if(isset($_GET['page']))
  8. {
  9. $page = $_GET['page'];
  10. } else { // if $_GET['page'] is not defined then $page == 1
  11. $page = 1;
  12. }
  13. // counting the offset
  14.  
  15.  
  16. //$searchresult = array();
  17. $var = $_POST['keyword'] ;
  18. $trimmed = mysql_real_escape_string(trim($var));
  19.  
  20. // rows to return
  21.  
  22.  
  23. $str = "SELECT * FROM `medlist` WHERE `dispname` LIKE '$trimmed%'";
  24. echo $str.'<br>';
  25. $res= mysql_query($str);
  26. if(!$res){die(mysql_error());}
  27. $num_rows= mysql_num_rows($res);
  28. echo $num_rows.'<br>';
  29.  
  30. // how many rows to show per page
  31. $rowsPerPage =10;
  32. $maxPage = ceil($num_rows/$rowsPerPage);
  33. //$page=(isset($_GET['page']))?$_GET['page']:1;
  34. //$offset=($page-1)*$rowsPerPage;
  35.  
  36.  
  37. if ($page < 1) {
  38. $page = 1;
  39. } elseif ($page > $maxPage) {
  40. $page = $maxPage;
  41. }
  42.  
  43. $max = 'LIMIT ' . ($page - 1) * $rowsPerPage . ',' . $rowsPerPage;
  44.  
  45. $query = "SELECT * FROM `medlist` WHERE `dispname` LIKE '$trimmed%' $max";
  46. $result = mysql_query($query);
  47. //<a href=page.php?search=$string&offset=$offset>
  48. if(!$result){die(mysql_error());}
  49. while($data = mysql_fetch_array($result)) {
  50.  
  51. echo ' <tr> <td colspan="3" > <a href="'.$data['MedName'].'" >' . $data['dispname'].' </a> </td>';
  52. echo'</tr>';
  53. }
  54. echo "<tr><td align='left' colspan='2'>Result Pages:";
  55. for($i=1;$i<=$maxPage;$i++)
  56. {
  57.  
  58. echo "<a href=pagno.php?rowsPerPage=$rowsPerPage&page=$i>".$i."&nbsp;|&nbsp;";
  59. }
  60. echo "</td>
  61. <td>Showing page <strong>".$page."</strong> of <strong>".$maxPage."</strong> pages </td>
  62. </tr>";
  63.  
  64.  
  65.  
  66. ?>

Does that help?
Reply With Quote  
Join Date: May 2008
Location: Hyderabad,India
Posts: 89
Reputation: Suhacini is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Re: Problem with page navigation

  #12  
Jun 3rd, 2008
If there is no 'A' anywhere in the word also its displaying.I changed my code to the above one,but nothing has changed.same output.
Suhasini
Reply With Quote  
Join Date: Apr 2008
Posts: 48
Reputation: helraizer is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 3
helraizer helraizer is offline Offline
Light Poster

Re: Problem with page navigation

  #13  
Jun 3rd, 2008
Humm.....

Could you give me the structure of the table in your database?

Like

id INT Auto_Increment
dispname (VARCHAR)

or whatever it may be? That way I can have a play in my own db.

Sam
Reply With Quote  
Join Date: May 2008
Location: Hyderabad,India
Posts: 89
Reputation: Suhacini is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Re: Problem with page navigation

  #14  
Jun 4th, 2008
My table structure is:
Id bigint(20) Auto_Inc Not Null
MedName Varchar(60)
dispname Varchar(60)
alphabet Varcahr(15)
Suhasini
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 554
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 57
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Posting Pro

Re: Problem with page navigation

  #15  
Jun 4th, 2008
so what exactly was wrong with the code i typed up for you.

the reason you getting the problem like i told you earlier is that you are not sending the search term along with each link in the script. the $_POST['keyword'] which contains the term on the first page is reset once you click a link. making it not show the same results from before with the different limit.

here is my code i made for you:

(others, please check for errors. i haven't found any, but i like to miss little things.)

<?php

if (isset($_POST['submit'])) {
	$var = trim($_POST['keyword']);
	header('Location: pageno.php?keyword=' . $var);
	die();
}

?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PgNo</title>
<link href="links.css" rel="stylesheet" type="text/css" />
</head>
<body>
	<table border="1" width="100%">
		<tr>
			<td align="center">
				<form method="post" action="pagno.php">
					<input type="text" size="40%" id="keyword" name="keyword" />
					<input type="submit" name="submit" value="Search" />
				</form>
			</td>
		</tr>
		<tr> 
			<td class="imag" id="def">| <a href="home.html"> Home </a> | <a href="dc.php"> D & C </a>| <a href="st.php"> S & T</a> | <a href="medi.php"> Med</a> |<a href="hc.html"> HC</a> |<a href="news.html"> News </a>|</td>
		</tr>
	</table>
	<table border="1" align="center">
		<tr>
			<th align="left"> Search Results :</th>
		</tr>
		<tr>
			<td align="center">
<?php

//make sure you add your database connection here

//Set Rows Per Page
$rowsPage = 10;

if (isset($_GET['keyword'])) {
	$word = mysql_real_escape_string($_GET['keyword']);
	if ($word !== '') {
		if (isset($_GET['page'])) {
			$page = $_GET['page'];
		}
		else {
			$page = 1;
		}
		$start = ($rowsPage * $page) - $rowsPage;
		$sql   = "SELECT * FROM `medlist` WHERE `dispname` LIKE '%" . $word . "%'";
		$query = mysql_query($sql);
		$total = mysql_num_rows($query);
		$pages = ceil($total/$rowsPage);
		if ($pages > 1) {
			$limit = ' LIMIT ' . $start . ', ' . $rowsPage;
		}
		else {
			$limit = '';
		}
		$sql   = "SELECT * FROM `medlist` WHERE `dispname` LIKE '%" . $word . "%'" . $limit;
		$query = mysql_query($sql);
		echo '<table border="0" cellspacing="0" cellpadding="3">';
		while ($row = mysql_fetch_assoc($query)) {
			echo '<tr>';
			echo '<td><a href="' . $row['MedName'] . '">' . $row['dispname'] . '</a></td>';
			echo '</tr>';
		}
		echo '</table>';
		echo '<center>Pages:&nbsp;';
		$i = 1;
		while ($i < count($pages)) {
			echo '<a href="pagno.php?keyword=' . $word . '&page=' . $i . '">' . ($page == $i ? '<font color="#FF0000">' . $i . '</font>' : $i) . '</a>&nbsp;&nbsp;&nbsp;';
		$i++;
		}
		echo '</center>';
	}
}

?>
			</td>
		</tr>
	</table>
</body>
</html>
Last edited by kkeith29 : Jun 4th, 2008 at 1:11 am.
Reply With Quote  
Join Date: May 2008
Location: Hyderabad,India
Posts: 89
Reputation: Suhacini is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Re: Problem with page navigation

  #16  
Jun 4th, 2008
Hey Kkeith29 some problem with this code...I dont know what it is..but it is not dislpaying anything.
Suhasini
Reply With Quote  
Join Date: May 2008
Location: Hyderabad,India
Posts: 89
Reputation: Suhacini is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Re: Problem with page navigation

  #17  
Jun 4th, 2008
Sam,In the code we have I found the problem.We have 2 queries
$str = "SELECT * FROM `medlist` WHERE `dispname` LIKE '%".$trimmed."%'";
$query = "SELECT * FROM `medlist` WHERE `dispname` LIKE '%".$trimmed."%'".$max ;
When I print them,In 1st page they are printing :
SELECT * FROM `medlist` WHERE `dispname` LIKE '%a%'  &
SELECT * FROM `medlist` WHERE `dispname` LIKE '%a%' limit 0,10 
In 2nd page they are printing
SELECT * FROM `medlist` WHERE `dispname` LIKE '%%'  &
SELECT * FROM `medlist` WHERE `dispname` LIKE '%%' limit 10,10 
So the problem here is in 2nd page the keyword is not there so it is displaying Id 11 from db.I changed the a href tag which is in the for loop :
echo "<a href=pagno.php?rowsPerPage=$rowsPerPage&page=$i&trimmed=$trimmed&offset=$offset>".$i."&nbsp;|&nbsp;";
I have added keyword and offset values but even this doesnt bring any change in the output.
What can be done next?
Suhasini
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 554
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 57
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Posting Pro

Re: Problem with page navigation

  #18  
Jun 4th, 2008
finally i got sick of this stuff and tested it on my server.

i created the table and filled it with random values.

here is the test script url: http://www.banditssoftball.org/pagno.php

to find a result, type in a number into the search box.

i made a mistake on my last script i posted for you. i used count() for some reason, use to it i guess, when i was echoing out the pages.
Last edited by kkeith29 : Jun 4th, 2008 at 3:07 am.
Reply With Quote  
Join Date: May 2008
Location: Hyderabad,India
Posts: 89
Reputation: Suhacini is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Suhacini Suhacini is offline Offline
Junior Poster in Training

Re: Problem with page navigation

  #19  
Jun 4th, 2008
ya its the same i need...did u use the same script which you posted for me?
Suhasini
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 554
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 57
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Posting Pro

Re: Problem with page navigation

  #20  
Jun 4th, 2008
yes, the one i typed for you (with minor changes).

here it is:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PgNo</title>
<link href="links.css" rel="stylesheet" type="text/css" />
</head>
<body>
	<table border="1" width="100%">
		<tr>
			<td align="center">
				<form method="get" action="pagno.php">
					<input type="text" size="40%" id="keyword" name="keyword" />
					<input type="submit" value="Search" />
				</form>
			</td>
		</tr>
		<tr> 
			<td class="imag" id="def">| <a href="home.html"> Home </a> | <a href="dc.php"> D & C </a>| <a href="st.php"> S & T</a> | <a href="medi.php"> Med</a> |<a href="hc.html"> HC</a> |<a href="news.html"> News </a>|</td>
		</tr>
	</table>
	<table border="1" align="center">
		<tr>
			<th align="left"> Search Results :</th>
		</tr>
		<tr>
			<td align="center">
<?php

//Make sure you input your database connection here

//Set Rows Per Page
$rowsPage = 10;

if (isset($_GET['keyword'])) {
	$word = mysql_real_escape_string($_GET['keyword']);
	if ($word !== '') {
		if (isset($_GET['page'])) {
			$page = $_GET['page'];
		}
		else {
			$page = 1;
		}
		$start = ($rowsPage * $page) - $rowsPage;
		$sql   = "SELECT * FROM `medlist` WHERE `dispname` LIKE '%" . $word . "%'";
		$query = mysql_query($sql);
		$total = mysql_num_rows($query);
		$pages = ceil($total/$rowsPage);
		if ($pages > 1) {
			$limit = ' LIMIT ' . $start . ', ' . $rowsPage;
		}
		else {
			$limit = '';
		}
		$sql   = "SELECT * FROM `medlist` WHERE `dispname` LIKE '%" . $word . "%'" . $limit;
		$query = mysql_query($sql);
		echo '<table border="0" cellspacing="0" cellpadding="3">';
		while ($row = mysql_fetch_assoc($query)) {
			echo '<tr>';
			echo '<td><a href="' . $row['MedName'] . '">' . $row['dispname'] . '</a></td>';
			echo '</tr>';
		}
		echo '</table>';
		echo '<center>Pages:&nbsp;';
		$i = 1;
		while ($i <= $pages) {
			echo '<a href="pagno.php?keyword=' . $word . '&page=' . $i . '">' . ($page == $i ? '<font color="#FF0000">' . $i . '</font>' : $i) . '</a>&nbsp;&nbsp;&nbsp;';
		$i++;
		}
		echo '</center>';
	}
}

?>
			</td>
		</tr>
	</table>
</body>
</html>
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 7:04 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC