Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SMALL Pagnination Problem

Status
Not open for further replies.

chriszd

Technical User
Apr 21, 2008
42
MT
i need to create a pagination code for php. i found many php codes around the web and found them very usefull, but never found one which really suited my needs.

What i need is:

1) i select which section i need to search for from a FORM, then i sumbit the result.

2) in the viewresult page you get the value of the section chosen via the $_POST function

$section =$_POST[section];

then the section is searched for from the MYSQL database

sql = ..... WHERE section=\"$section"\


Go to next page

<a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&section=$section'><img src=Pics/control_fastforward.png BORDER=0></a>

with the part of code where i need to go to next page i need to send the value of the section

the problem i am finding is that since it is refreshing on the same page the value of section is coming to "null" since the first value of section was taken from another page with the $_POST function.

is there a way how i can overcome this problem?

hope i explained my self:

the code is below

<?php

$section=$_POST[section];

connect to database

if (isset($_GET['pageno']))
{
$pageno = $_GET['pageno'];
}

else
{
$pageno = 1;
}



$query = "SELECT count(*) FROM news WHERE section like \"%$section%\" ";
$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

echo "SECTION: $section <br> ";

$rows_per_page = 2; // number of records to show per page
$lastpage = ceil($numrows/$rows_per_page);


$pageno = (int)$pageno;
if ($pageno < 1) {
$pageno = 1;
}
elseif ($pageno > $lastpage) {
$pageno = $lastpage;
}

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;


$query2 = "SELECT * FROM news WHERE section like \"%$section%\" ORDER by id DESC $limit ";
$result2 = mysql_query($query2) or trigger_error("SQL", E_USER_ERROR);


while($nt2=mysql_fetch_array($result2))
{
$author = $nt2[author];
$section = $nt2[section];
$title = $nt2[title];
$msg = $nt2[msg];

echo $author
echo $section
echo $title
echo $msg

}

echo "<font face=Century Gothic size=2>Page $pageno of $lastpage</font> <br>";

if ($pageno == 1)
{

}
else
{

echo "<a href='{$_SERVER['PHP_SELF']}?pageno=1&section=$section'><img src=Pics/control_start.png BORDER=0></a>\n ";
$prevpage = $pageno-1;
echo "<a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage&section=$section'><img src=Pics/control_rewind.png BORDER=0></a>\n ";
}




if ($pageno == $lastpage)
{


}
else
{

$nextpage = $pageno+1;
echo "<a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&section=$section'><img src=Pics/control_fastforward.png BORDER=0></a>\n ";
echo "<a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage&section=$section'><img src=Pics/control_end.png BORDER=0></a>\n ";
}

?>
 
Hi

I think the simplest is to get the [tt]$section[/tt]'s value according to the request method :
Code:
[s]$section =$_POST[section];[/s]

$section=$_SERVER[REQUEST_METHOD]=='POST'?$_POST[section]:$_GET[section];

Feherke.
 
i've tweaked your code a bit. please remember always to post your code in [ignore]
Code:
[/ignore] tags

Code:
<?php
function getSectionNumber(){
	if(!empty($_POST['section'])){
		$section = (int) $_POST['section'];
	} elseif (!empty($_GET['section'])){
		$section = (int) $_GET['section'];
	} else {
		$section = 1;
	}
	return $section;
}
//you need to make sure that $section ALWAYS has a value
$section = getSectionNumber();

$pageno = empty($_GET['pageno']) ? 1 : (int) $_GET['pageno'] ;

//connect to database
 
$query = "SELECT count(*) FROM news WHERE section like '%$section%'";
$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

echo "SECTION: $section <br> ";

$rows_per_page = 2; // number of records to show per page
$lastpage = ceil($numrows/$rows_per_page);

if ($pageno < 1) {
   $pageno = 1;
} elseif ($pageno > $lastpage) {
   $pageno = $lastpage;
}

//it is clearer to use LIMIT and OFFSET
$limit = 'LIMIT $rows_per_page OFFSET ' .($pageno - 1) * $rows_per_page;


$query2 = "SELECT * FROM news WHERE section like \"%$section%\" ORDER by id DESC $limit ";
$result2 = mysql_query($query2) or trigger_error("SQL", E_USER_ERROR);


while($nt2=mysql_fetch_array($result2)){
	//you must enquote associative array keys
	$author = $nt2['author'];
	$section = $nt2['section'];
	$title = $nt2['title'];
	$msg = $nt2['msg'];

	//you need to end php lines with semicolons
	echo $author;
	echo "<br/>";
	echo $section;
	echo "<br/>";
	echo $title;
	echo "<br/>";
	echo $msg;
	echo "<br/>";
	echo "<hr/>"; //add a spacer
}

echo "<font face=Century Gothic size=2>Page $pageno of $lastpage</font> <br>";

//build some variables for later use
$nextPage = '<a href="'.$_SERVER['PHP_SELF'].'?pageno='. ($pageno + 1) .'&section='.$section.'"<img src=Pics/control_start.png border="0"></a>';
$prevPage = '<a href="'.$_SERVER['PHP_SELF'].'?pageno='. ($pageno - 1) .'&section='.$section.'"<img src=Pics/control_rewind.png border="0"></a>';

//output the links as necessary
if ($pageno == 1){
	//just print next page
	if ($pageno == $lastpage){
		//no links are necessary
	} else {
		echo $nextPage;
	}
} else {
	if ($pageno == $lastpage){
		//just print prev page
		echo $prevPage;
	} else {
		//print both
		echo $prevPage . '&nbsp;' . $nextPage;
	}
}
?>
 
thanks guys i managed to make it work... thanks alot again for your help :) the best
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top