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 391,608 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 2,596 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: 677 | Replies: 25 | Solved
Reply
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

Problem with page navigation

  #1  
May 19th, 2008
I have problem with page navigation.My problem is that am getting all the records in one page. $rowsPerPage = 10; I have 30 records, all are showed in one page...not the 10 records.Here is my code.

<?php
include ('conn.php');

// how many rows to show per page
$rowsPerPage = 10;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
	$pageNum = $_GET['page'];
}


// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;

$q = "show tables";
$r = mysql_query($q);
//$searchresult = array();
$var = $_POST['keyword'] ;
  $trimmed = trim($var);
  
  // rows to return
$limit=10; 

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }
  
 while($data = mysql_fetch_array($r)) {
	$table = $data[0]; // get the tablename
	$query = "select * from ".$table." where dispname like '%".$trimmed."%'";
    $result = mysql_query($query);	
	 while($data = mysql_fetch_array($result)) {
	 	//	$searchresult[] = $rows['name'];
         //$var =$rows['dispname'];
		  
 echo '  <tr> <td> <a href="'.$data['MedName'].'" >' . $data['dispname'].' </a> </td>';
		 echo'</tr>'; 
         	
	}  
}
//$query  = "SELECT val FROM randoms LIMIT $offset, $rowsPerPage";
//$result = mysql_query($query) or die('Error, query failed');

while(list($val) = mysql_fetch_array($result))
{
	echo "$val <br>";
}

echo '<br>';


$query   = "select * from ".$table." where dispname like '%".$trimmed."%'";
$result  = mysql_query($query) or die('Error, query failed');
$row     = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

$maxPage = ceil($numrows/$rowsPerPage);

$self = $_SERVER['PHP_SELF'];




if ($pageNum > 1)
{
	$page = $pageNum - 1;
	$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
	
	$first = " <a href=\"$self?page=1\">[First Page]</a> ";
} 
else
{
	$prev  = ' [Prev] ';       // we're on page one, don't enable 'previous' link
	$first = ' [First Page] '; // nor 'first page' link
}

if ($pageNum < $maxPage)
{
	$page = $pageNum + 1;
	$next = " <a href=\"$self?page=$page\">[Next]</a> ";
	$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
} 
else
{
	$next = ' [Next] ';      // we're on the last page, don't enable 'next' link
	$last = ' [Last Page] '; // nor 'last page' link
}


echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;


?>
Suhasini
AddThis Social Bookmark Button
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

  #2  
May 27th, 2008
I changed my code.Here is my new code:
<?php 
include ('conn.php'); 
// how many rows to show per page 
$rowsPerPage = 10; 
// by default we show first page 
$pageNum = 1;
// if $_GET['page'] defined, use it as page number 
if(isset($_GET['page'])) 
{ 
$pageNum = $_GET['page']; 
} 
// counting the offset 
$offset = ($pageNum - 1) * $rowsPerPage;
//$searchresult = array(); 
$var = $_REQUEST['keyword'] ; 
$trimmed = trim($var); 
// rows to return 


// check for an empty string and display a message. 
if ($trimmed == "") 
{ 
echo "<p>Please enter a search...</p>"; 
exit; 
} 

// check for a search parameter 
if (!isset($var)) 
{ 
echo "<p>We dont seem to have a search parameter!</p>"; 
exit; 
} 

//$table ="results"; 
$query ="select count(*) from medlist where dispname like '%".$trimmed."%'"; 
$rsid = mysql_query($query); 
if(!$rsid){die(mysql_error());}
$resArr = mysql_fetch_row($rsid); 
$numrows = $resArr[0]; 

//$query ="select * from medlist where dispname like '%".$trimmed."%' limit $strpos, $rowsPerPage"; 
$query = "select * from medlist where dispname like '%".$trimmed."%' LIMIT ". $start .", ". 
$rowsPerPage ;
$result = mysql_query($query); 
if(!$result){die(mysql_error());}
//$query = "SELECT val FROM randoms LIMIT $offset, $rowsPerPage"; 
//$result = mysql_query($query) or die('Error, query failed'); 

while($val = mysql_fetch_row($result)) 
{ 
print_r($val); 
} 

echo '<br>'; 


$maxPage = ceil($numrows/$rowsPerPage); 

$self = $_SERVER['PHP_SELF']; 
if ($pageNum > 1) 
{ 
$page = $pageNum - 1; 
$prev = " <a href=\"$self?page=$page&keyword=$var\">[Prev]</a> "; 

$first = " <a href=\"$self?page=1&keyword=$var\">[First Page]</a> "; 
} 
else 
{ 
$prev = ' [Prev] '; // we're on page one, don't enable 'previous' link 
$first = ' [First Page] '; // nor 'first page' link 
} 

if ($pageNum < $maxPage) 
{ 
$page = $pageNum + 1; 
$next = " <a href=\"$self?page=$page&keyword=$var\">[Next]</a> "; 
$last = " <a href=\"$self?page=$maxPage&keyword=$var\">[Last Page]</a> "; 
} 
else 
{ 
$next = ' [Next] '; // we're on the last page, don't enable 'next' link 
$last = ' [Last Page] '; // nor 'last page' link 
} 
echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last; 
?>
Error occured is :
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 10' at line 1.
This error is some where around this line:if(!$result){die(mysql_error());}
What is the solution?
Last edited by Suhacini : May 27th, 2008 at 1:54 am.
Suhasini
Reply With Quote  
Join Date: May 2008
Location: Hyderabad, India
Posts: 13
Reputation: hivenk is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 2
hivenk hivenk is offline Offline
Newbie Poster

Re: Problem with page navigation

  #3  
May 27th, 2008
Actually in your code you have to put $offset instead of $start in your query

check once
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

  #4  
May 27th, 2008
Actually I got this code from some where.I wanted to write my own code..Can anyone tell me how to start with that?
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

  #5  
May 27th, 2008
From the above code I am not getting any error but,the link to next page is not enabled.Even though $limit=10; & $rowsPerPage =10; both are declared as '10'.If I have 15 records all of them are displayed in the same page,but not in the 2 different pages.
Anyone to help regarding this??
Last edited by Suhacini : May 27th, 2008 at 9:00 am.
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

  #6  
Jun 3rd, 2008
Changed the code again.
<?php 
include ('conn.php');

// by default we show first page 
$page = 1; 

// if $_GET['page'] defined, use it as page number 
if(isset($_GET['page'])) 
{ 
$page = $_GET['page']; 
} 
// counting the offset 
$offset = ($page - 1) * $rowsPerPage; 


//$searchresult = array(); 
$var = $_POST['keyword'] ; 
$trimmed = trim($var); 

// rows to return 
$limit=10; 

$str = "select * from medlist where dispname like '%".$trimmed."%'"; 
echo $str.'<br>';
$res= mysql_query($str); 
if(!$res){die(mysql_error());}
$num_rows= mysql_num_rows($res); 
echo $num_rows.'<br>';

// how many rows to show per page 
$rowsPerPage =10; 
$maxPage = ceil($num_rows/$rowsPerPage);
//$page=(isset($_GET['page']))?$_GET['page']:1;
//$offset=($page-1)*$rowsPerPage;

$query = "select * from medlist where dispname like '%".$trimmed."%' limit $offset,$rowsPerPage";
$result = mysql_query($query);
//<a href=page.php?search=$string&offset=$offset>
if(!$result){die(mysql_error());}
while($data = mysql_fetch_array($result)) { 
    
    echo ' <tr> <td colspan="3" > <a href="'.$data['MedName'].'" >' . $data['dispname'].' </a> </td>'; 
    echo'</tr>'; 
}
echo "<tr><td align='left' colspan='2'>Result Pages:";
for($i=1;$i<=$maxPage;$i++)
 {
   
   echo "<a href=pagno.php?rowsPerPage=$rowsPerPage&page=$i>".$i."&nbsp;|&nbsp;";
 }   
echo "</td>
<td>Showing page <strong>".$page."</strong> of <strong>".$maxPage."</strong> pages </td>
</tr>";	

?> 
In Page1 data displayed is from A1 to A10. Now the problem is that if I go to Page 2 data displayed must be from A11 to A12 but in this case it displaying the data where Id=11 in my db.
What is wrong with this code?
Suhasini
Reply With Quote  
Join Date: Jun 2008
Location: Amstelveen, Holland
Posts: 5
Reputation: Jorann is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
Jorann Jorann is offline Offline
Newbie Poster

Re: Problem with page navigation

  #7  
Jun 3rd, 2008
well, you are doing this
// counting the offset 
$offset = ($page - 1) * $rowsPerPage;

before you do this:
// how many rows to show per page 
$rowsPerPage =10; 

i think it shoudl be other way around...
Last edited by Jorann : Jun 3rd, 2008 at 8:54 am.
Reply With Quote  
Join Date: Jun 2008
Location: Amstelveen, Holland
Posts: 5
Reputation: Jorann is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
Jorann Jorann is offline Offline
Newbie Poster

Re: Problem with page navigation

  #8  
Jun 3rd, 2008
This is what i once used in my code:


  1. $showfromthisid = ceil($pagenumber*10) -10;
  2. $maxshownmessages = 10;
  3.  
  4. $result = mysql_query("SELECT * FROM table ORDER BY id DESC LIMIT $showfromthisid,$maxshownmessages") or die(mysql_error());
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

  #9  
Jun 3rd, 2008
Originally Posted by Suhacini View Post
Changed the code again.
<?php 
include ('conn.php');

// by default we show first page 
$page = 1; 

// if $_GET['page'] defined, use it as page number 
if(isset($_GET['page'])) 
{ 
$page = $_GET['page']; 
} 
// counting the offset 
$offset = ($page - 1) * $rowsPerPage; 


//$searchresult = array(); 
$var = $_POST['keyword'] ; 
$trimmed = trim($var); 

// rows to return 
$limit=10; 

$str = "select * from medlist where dispname like '%".$trimmed."%'"; 
echo $str.'<br>';
$res= mysql_query($str); 
if(!$res){die(mysql_error());}
$num_rows= mysql_num_rows($res); 
echo $num_rows.'<br>';

// how many rows to show per page 
$rowsPerPage =10; 
$maxPage = ceil($num_rows/$rowsPerPage);
//$page=(isset($_GET['page']))?$_GET['page']:1;
//$offset=($page-1)*$rowsPerPage;

$query = "select * from medlist where dispname like '%".$trimmed."%' limit $offset,$rowsPerPage";
$result = mysql_query($query);
//<a href=page.php?search=$string&offset=$offset>
if(!$result){die(mysql_error());}
while($data = mysql_fetch_array($result)) { 
    
    echo ' <tr> <td colspan="3" > <a href="'.$data['MedName'].'" >' . $data['dispname'].' </a> </td>'; 
    echo'</tr>'; 
}
echo "<tr><td align='left' colspan='2'>Result Pages:";
for($i=1;$i<=$maxPage;$i++)
 {
   
   echo "<a href=pagno.php?rowsPerPage=$rowsPerPage&page=$i>".$i."&nbsp;|&nbsp;";
 }   
echo "</td>
<td>Showing page <strong>".$page."</strong> of <strong>".$maxPage."</strong> pages </td>
</tr>";	

?> 
In Page1 data displayed is from A1 to A10. Now the problem is that if I go to Page 2 data displayed must be from A11 to A12 but in this case it displaying the data where Id=11 in my db.
What is wrong with this code?


I don't really understand the question here. What is the difference between A11 and Id=11?

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

  #10  
Jun 3rd, 2008
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?
Suhasini
Reply With Quote  
Reply

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

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

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

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