Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Paginating problem?

Featured Replies

Hi people. I'm currently trying to build a kind of search method for a site i'm working on but currently I need a bit of help with my paginating as I can't seem to get it to work properly. At the moment when a user searches for a car the first three records are returned but when you click the next button or a numbered button the links take you to a page with no records.

 

I've highlighted the area I believe the issue is surrounding. If anyone can help it would be much appreciated.

 

Thanks!

 

for some reason the code isn't highlighted :o

 

switch ($_GET['classified_ad']) { 
default:

echo "<div id=\"background1\">
<div id=\"background3\">";
echo "Search";
echo "</div>";
echo "<div id=\"background2\">";

echo "<form action=\"buy2.php?classified_ad=list&make=$getMake&model=$getModel\"  method=\"post\">\n";
echo "<strong>Make</strong> \n<br />\n";
echo "<select name=\"make\"/>

		</select>\n<br />\n";
echo "<strong>Model</strong> \n<br />\n";
echo "<select name=\"model\"/>

		</select>\n<br />\n";
echo "<input type=\"submit\" name=\"submit\" value=\"Search\"/>\n";
echo "<input type=\"hidden\" name=\"search\" value=\"TRUE\"/>\n";
echo "</form>\n";

echo "</div> </div>\n";

break;
case 'list':

require_once ('mysql_connect2.php');

[color="#FF0000"][b]

if (!empty($_POST['make'])) {
	$getMake = escape_data($_POST['make']);
	}else{
	$getMake = FALSE;
	echo '';
}
if (!empty($_POST['model'])) {
	$getModel = escape_data($_POST['model']);
	}else{
	$getModel = FALSE;
	echo '';
}

if ($getMake && $getModel){



$display = 3;

if (isset($_GET['np'])) {

$num_pages = $_GET['np'];

}else{

$query = "SELECT COUNT(*) FROM classifieds WHERE make='$getMake' AND model='$getModel' ORDER BY ad_id ASC";
$result = mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
$num_records = $row[0];

if ($num_records > $display) {
$num_pages = ceil ($num_records/$display);
}else{
$num_pages = 1;
}
}
if (isset($_GET['s'])) {
$start = $_GET['s'];
}else{
$start = 0;
}
if (isset($_GET['make']) && isset($_GET['model'])){

$query = "SELECT * FROM classifieds WHERE make='$getMake' AND model='$getModel' ORDER BY ad_id ASC LIMIT $start, $display"; 
$result = mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_ASSOC);
echo "'$result'";
echo '<div id="background4">
	  <div id="background6">';
echo "{$row['make']} {$row['model']}";
echo '</div>';[/b]
[/color]
if ($row) {

echo '<div id="background5">';
echo '<table>';
$description = nl2br($row['description']);
echo "<tr><td><a href=\"buyer.php?classified_ad=car&id={$row['ad_id']}\" target=\"_self\">{$row['title']}</a></td></tr>";
echo "<tr><td>{$row['mileage']} miles</td></tr>";
echo "<tr><td><p>$description</p></td></tr>";


while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
$description = nl2br($row['description']);
echo "<tr><td><a href=\"buyer.php?classified_ad=car&id={$row['ad_id']}\" target=\"_self\">{$row['title']}</a></td></tr>";
echo "<tr><td>{$row['mileage']} miles</td></tr>";
echo "<tr><td><p>$description</p></td></tr>";


} 
mysql_free_result ($result);
echo "</table>";
mysql_close();
[color="#FF0000"]
[b]if ($num_pages > 1) {
echo '<br /><p>';
$current_page = ($start/$display) + 1;
if($current_page != 1) {
echo '<a href="buy2.php?classified_ad=list&make=' . $getMake . '&model=' . $getModel . '&s=' . ($start - $display) . '&np=' . $num_pages . '">Previous</a> ';
}
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo '<a href="buy2.php?classified_ad=list&make=' . $getMake . '&model=' . $getModel . '&s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '">' . $i . '</a> ';
}else{
echo $i . ' ';
}
}
if ($current_page != $num_pages) {
echo '<a href="buy2.php?classified_ad=list&make=' . $getMake . '&model=' . $getModel . '&s=' . ($start + $display) . '&np=' . $num_pages . '">Next</a> ';
}
echo '</p>';
}[/b][/color]


} else {
echo "Nothing found";
}
}






echo "</div> </div>\n";


}



break;
case 'car':

if (isset($_GET['id']) && is_numeric($_GET['id'])) {
$class_id = $_GET['id'];
require_once ('mysql_connect2.php');
$query = "SELECT * FROM classifieds WHERE ad_id='$class_id' LIMIT 1"; 
$result = mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_ASSOC);

if ($row) {
$description = nl2br($row['description']);
echo "ADVERT";
echo "{$row['ad_id']}";
echo "$description";
}else{ }
mysql_close();
}else {
echo "nothing found";
}
break;
}

  • Author

After a bit of playing around I have discovered that the problem lies somewhere around here in the code below.

For some reason I believe when the data is posted from a form it isn't being posted onto the other pages, 2, 3, 4 etc only page 1. Can anyone think of a solution? Thanks, Mark.

 

case 'list':

require_once ('mysql_connect2.php');

[color="#FF0000"][b]

if (!empty($_POST['make'])) {
	$getMake = escape_data($_POST['make']);
	}else{
	$getMake = FALSE;
	echo '';
}
if (!empty($_POST['model'])) {
	$getModel = escape_data($_POST['model']);
	}else{
	$getModel = FALSE;
	echo '';
}

if ($getMake && $getModel){



$display = 3;

if (isset($_GET['np'])) {

$num_pages = $_GET['np'];

}else{

$query = "SELECT COUNT(*) FROM classifieds WHERE make='$getMake' AND model='$getModel' ORDER BY ad_id ASC";
$result = mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
$num_records = $row[0];

if ($num_records > $display) {
$num_pages = ceil ($num_records/$display);
}else{
$num_pages = 1;
}
}
if (isset($_GET['s'])) {
$start = $_GET['s'];
}else{
$start = 0;
}
if (isset($_GET['make']) && isset($_GET['model'])){

$query = "SELECT * FROM classifieds WHERE make='$getMake' AND model='$getModel' ORDER BY ad_id ASC LIMIT $start, $display";
$result = mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_ASSOC);
echo "'$result'";
echo '<div id="background4">
	  <div id="background6">';

 

for some reason I believe the data posted from a form isn't being being posted onto the other pages, just page one

Hi Mark by the way,

Also, i wouldn't count the number of results like that, use mysql_num_rows($result) instead.....The way you have done it gives you the result number and not the number of results. This would explain why its not grabbing the second lot of results.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.