Jump to content

Pagination help


etnastyles

Recommended Posts

hello people,

 

im having lots of trouble trying to get a good pagination script working and implemented.

 

I have a pagination script see below that i found on the net.. cool right.. i want to display 10 records of entry and then have a search bar for users t search .. only when a user searches i can pull out the results in the pagination but when they click on a link to go to the next page i loose all my search results and the page refreshes back to nothing...  has anyone come accross this issue . its quite annoying - im struggling with it now  :confused:

 

 

if(isset($_POST['textbtn'])) { $search = $_POST['textbtn']; } else if (isset($_GET['textbtn'])) { $search = $_GET['textbtn']; }

 

 

function do_it_all($rowsperpage, $conn, $search)

{

$sql = "SELECT COUNT(*) FROM all3_media_new";

$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

$r = mysql_fetch_row($result);

$numrows = $r[0];

// number of rows to show per page

// find out total pages

$totalpages = ceil($numrows / $rowsperpage);

// find out how many rows are in the table

// get the current page or set a default

if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage']))

{

// cast var as int

$currentpage = (int) $_GET['currentpage'];

}

else

{

// default page num

$currentpage = 1;

} // end if

 

// if current page is greater than total pages...

if ($currentpage > $totalpages)

{

// set current page to last page

$currentpage = $totalpages;

} // end if

// if current page is less than first page...

if ($currentpage < 1)

{

// set current page to first page

$currentpage = 1;

} // end if

// the offset of the list, based on current page

$offset = ($currentpage - 1) * $rowsperpage;

// get the info from the db

//$sql = "SELECT Shelf_Location, Title FROM all3_media_new LIMIT $offset, $rowsperpage";

$sql = "SELECT * FROM all3_media_new WHERE Title like '%".$search."%' ORDER BY ALL3_ID LIMIT ".$offset.", ".$rowsperpage."";

$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

// while there are rows to be fetched...

while ($list = mysql_fetch_assoc($result))

{

// echo data

echo $list['Shelf_Location'] . " : " . $list['Title'] . "<br />";

} // end while

 

/******  build the pagination links ******/

// range of num links to show

$range = 3;

// if not on page 1, don't show back links

if ($currentpage > 1)

{

// show << link to go back to page 1

echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1&textbtn=$search'><<</a> ";

// get previous page num

$prevpage = $currentpage - 1;

// show < link to go back to 1 page

echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&textbtn=$search'><</a> ";

} // end if

 

// loop to show links to range of pages around current page

for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++)

{

// if it's a valid page number...

if (($x > 0) && ($x <= $totalpages))

{

// if we're on current page...

if ($x == $currentpage)

{

// 'highlight' it but don't make a link

echo " [<b>$x</b>] ";

// if not current page...

}

else

{

// make it a link

echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x&textbtn=$search'>$x</a> ";

} // end else

} // end if

} // end for

 

// if not on last page, show forward and last page links       

if ($currentpage != $totalpages)

{

// get next page

$nextpage = $currentpage + 1;

// echo forward link for next page

echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage&textbtn=$search'>></a> ";

// echo forward link for lastpage

echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages&textbtn=$search'>>></a> ";

} // end if

/****** end build pagination links ******/

}

 

 

 

<form method="post" action="pageination2.php" />

<input type="text" name="textbtn" value="<?php if(isset($search)) { echo $search; } ?>" />

<input type="submit" name="btn" />

 

</form>

Link to comment
https://forums.phpfreaks.com/topic/167105-pagination-help/
Share on other sites

think i may have solved this with sessions usign a new script but would love any input css not included

 

<?php

 

session_start();

 

$connection = mysql_connect("localhost", "root", "") or die(mysql_error());

mysql_select_db("all3medianew", $connection) or die(mysql_error());

 

echo "we have a db<br/><br/><br/><br/>";

 

 

$pageNumber = (isset($_GET['page']) && is_numeric($_GET['page'])) ? $_GET['page'] : 1;

$perPage = 50;

$padding = 5;

$startIndex = ($pageNumber * $perPage) - $perPage;

echo "start index  = ".$startIndex."<br/>";

 

 

 

 

if(isset($_POST['textbtn']))

{

$search = $_POST['textbtn'];

$_SESSION['search'] = $_POST['textbtn'];

}

 

if(isset($_SESSION['search']))

{

$search = $_SESSION['search'];

}

 

$sql = "SELECT * FROM all3_media_new WHERE Shelf_Location like '%".$search."%' ORDER BY ALL3_ID LIMIT ".$startIndex.", ".$perPage."";

$sql1 = "SELECT * FROM all3_media_new WHERE Shelf_Location like '%".$search."%' ORDER BY ALL3_ID";

echo "sql = ". $sql."<br/>";

$rs = mysql_query($sql1, $connection) or die(mysql_error());

$rs2 = mysql_query($sql, $connection) or die(mysql_error());

$rowCount = mysql_num_rows($rs);

echo "row count = ".$rowCount."<br/>";

$numberOfPages = ceil($rowCount / $perPage);

echo "number of pages = ".$numberOfPages;

 

 

/*

$sql = "SELECT * FROM all3_media_new ORDER BY ALL3_ID LIMIT ".$startIndex.", ".$perPage."";

echo $sql."<br/>";

$totalCount = "SELECT COUNT(*) AS 'Total' FROM all3_media_new";

echo $totalCount;

$rsCount = mysql_query($totalCount, $connection) or die(mysql_error());

$rowCount = mysql_fetch_array($rsCount);

 

print_r($rowCount);

$numberOfPages = ceil($rowCount['Total'] / $perPage);

$rs = mysql_query($sql, $connection) or die(mysql_error());

//$counter = mysql_num_rows($rs);

 

*/

 

 

 

echo "<span>

<ul id=\"pagination-flickr\">";

//$numberOfPages = ceil($counter / $perPage);

//echo ceil($counter / $perPage);

echo "<li><a href =\"pageination.php?page=1\"/>FIRST</a></li> ";

if(($pageNumber - $padding > 1))

{

//echo "....";

$lowerLimit = $pageNumber - $padding;

for ($i = $lowerLimit; $i < $pageNumber; $i++)

{

echo " <li><a href=\"pageination.php?page=".$i."\"/>".$i."</a></li> ";

}

}

else

{

for ($i = 2; $i < $pageNumber; $i++)

{

echo " <li><a href=\"pageination.php?page=".$i."\"/>".$i."</a></li> ";

}

}

 

if( ($pageNumber != 0) && ($pageNumber !=1) && ($pageNumber != $numberOfPages) )

{

//echo "<li>".$pageNumber."</li>";

//echo $pageNumber;

}

 

if(($pageNumber + $padding < $numberOfPages))

{

//echo "....";

$upperLimit = $pageNumber + $padding;

for ($i = $pageNumber+1; $i <= $upperLimit; $i++)

{

echo " <li><a href=\"pageination.php?page=".$i."\"/>".$i."</a></li> ";

}

}

else

{

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

{

echo "<li><a href=\"pageination.php?page=".$i."\"/>".$i."</a></li> ";

}

}

echo " <li><a href =\"pageination.php?page=".$numberOfPages."\"/>LAST</a></li>";

 

//searchdisplay($counter, $keyword, $keyword2, $tape_format_select, $keyword0, $sql, $rowCount);

 

echo "</ul></span>";

 

while($row = mysql_fetch_array($rs2))

{

echo "<tr>

 

<td class=\"count_details\">".$row['Shelf_Location']."</td>

 

</tr>";

}

 

mysql_close($connection);

 

?>

 

<form method="post" action="pageination.php" />

<input type="text" name="textbtn" value="<?php if(isset($search)) { echo $search; } ?>" />

<input type="submit" name="btn" />

 

</form>

Link to comment
https://forums.phpfreaks.com/topic/167105-pagination-help/#findComment-881078
Share on other sites

Archived

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

×
×
  • 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.