Jump to content

Need help paginating results


cluelessnoob

Recommended Posts

I am new to PHP and was work on paginating results, I've got it working to a point where it displays the additional pages, but it doesn't print the results on the new page and if you go back to the first page the results disappear. I'd appreciate any help. I've attached my code.

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/141522-need-help-paginating-results/
Share on other sites

I havnt got sqllite on my server but just minor changes, let me know if this helps , if not send a dummy of your sqlite db and ill put it on my wamp server.

 

<?php # Script 1.0 - view_facilities.php #2
// This script retrieves retrieves selected records from the facilities table.

$page_title = 'View the search results';
include ('includes/header2.html');

// Page header:
echo '<h1>Registered Facilities</h1>';
$zip = trim($_POST['szip']);

require_once ('includes/mysqli_connect.php'); // Connect to the db.

// Number of records to show per page:
$display = 4;

// Determine how many pages there are...
if (isset($_GET['p']) && is_numeric($_GET['p'])) { // Already been determined.
$pages = $_GET['p'];
} else { // Need to determine.
	// Count the number of records:
$q = "SELECT COUNT(FID) FROM facility";
$r = @mysqli_query ($dbc, $q);
$row = @mysqli_fetch_array ($r, MYSQLI_NUM);
$records = $row[0];
// Calculate the number of pages...
if ($records > $display) { // More than 1 page.
	$pages = ceil ($records/$display);
} else {
	$pages = 1;
}
} // End of p IF.

// Determine where in the database to start returning results...
if (isset($_GET['s']) && is_numeric($_GET['s'])) {
$start = $_GET['s']*$display;
} else {
$start = 0;
}



//Make the query:
$q = "SELECT facName,facAddress, CONCAT(facCity, ', ', facState) AS loc, facZip FROM facility WHERE (facZip = '$zip') LIMIT $start, $display";		
$r = @mysqli_query ($dbc, $q); // Run the query.

//Count the number of returned rows:
//$num = mysqli_num_rows($r);


?>
<div id="header">


</div>

	<div id="main-nav">

		<dl class="hidden">
			<dt id="about"><a href="#">About</a></dt>
			<dt id="services"><a href="#">Services</a></dt>
			<dt id="portfolio"><a href="#">Portfolio</a></dt>
			<dt id="contact"><a href="#">Contact Us</a></dt>
		</dl>
</div>

	<div id="sidebar-a">
		<div class="padding">

			Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nullam gravida enim ut risus. 
			Praesent sapien purus, ultrices a, varius ac, suscipit ut, enim. Maecenas in lectus. 
			Donec in sapien in nibh rutrum gravida. Sed ut mauris. Fusce malesuada enim vitae lacus 
			euismod vulputate. Nullam rhoncus mauris ac metus. Maecenas vulputate aliquam odio. 
			Duis scelerisque justo a pede. Nam augue lorem, semper at, porta eget, placerat eget, 
			purus. Suspendisse mattis nunc vestibulum ligula. In hac habitasse platea dictumst.

			</div>


		</div>

	<div id="content">
	<div class="padding">
<?php				


// Print how many users there are:

echo "<H2>Your search results:</H2>";
echo "<br/>";
echo "<br/>";

// Fetch and print all the records:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
	echo $row['facName']. "<br/>";
	echo $row['facAddress']. "<br/>";
	echo $row['loc']. "<br/>";
	echo $row['facZip']. "<br/>";
	echo "<br/>";

}



mysqli_free_result ($r); // Free up the resources.	
mysqli_close($dbc); // Close the database connection.


// Make the links to other pages, if necessary.
if ($pages > 1) {

// Add some spacing and start a paragraph:
echo '<br /><p>';

// Determine what page the script is on:	
$current_page = floor($start/$display) + 1; //strip decimal point if any

// If it's not the first page, make a Previous button:
if ($current_page > 1) {
	echo '<a href="view_facilities.php?s=' . ($current_page-1) . '&p=' . $pages . '">Previous</a> ';
}

// Make all the numbered pages:
for ($i = 1; $i <= $pages; $i++) {
	if ($i != $current_page) {
		echo '<a href="view_facilities.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '">' . $i . '</a> ';
	} else {
		echo $i . ' ';
	}
} // End of FOR loop.


// If it's not the last page, make a Next button:
if ($current_page != $pages) {
	echo '<a href="view_facilities.php?s=' . ($current_page+1) . '&p=' . $pages . '">Next</a>';
}


echo '</p>'; // Close the paragraph.

} // End of links section.				

	include ('includes/footer2.html');
?>

I have been through the tutorial on this site, and while I have not figured out a fix. I believe I have isolated the problem. In this script I am using a value passed be an HTML form as a WHERE clause when querying the database. However this value is not passed on to subsequent pages. I am fairly certain this is the problem but I have no idea how to fix it. Any help would be appreciated. The value is called $zip in the code.

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.