Jump to content

pagination problem


d3vilr3d

Recommended Posts

Hi guys

 

I'm trying to break a listing into pages with 10 listings per page.  I have read through the pagination tutorial and is having difficulty implementing the codes.  Please point me toward the correct direction.

 

Here is the original listing code:

 

<?PHP

if (!defined("MC_KEY")) die("Security Error.");

include_once('includes/database.php');
require_once('config.php');


if ($_REQUEST[action]=="showads") {
    echo "<div id=\"ads_search_form\">\n";
    include('search_form.php');
    echo "</div>\n";
    echo "<div>\n";

    if (isset($_REQUEST[regionid]) && isset($_REQUEST[subcategoryid])) {
        $url_params = "regionid=".intval($_REQUEST[regionid])."&subcategoryid=".intval($_REQUEST[subcategoryid]);
    } else {
        // SCRIPT TO BAIL OUT!!!
        //echo "<script></script>";
    }

    $ads = $info->getAds($_REQUEST[regionid],$_REQUEST[subcategoryid]);

    echo "<table cellspacing=5>\n";

    $cur_date = "";
$limit      = 25;
$query_count    = "SELECT count(*) FROM ad";   
$result_count   = mysql_query($query_count);    
$totalrows  = mysql_num_rows($result_count);
if(empty($page)){    // Checks if the $page variable is empty (not set)
    $page = 1;      // If it is empty, we're on page 1
    } 	
$limitvalue = $page * $limit - ($limit);  
$query  = "SELECT * FROM ads LIMIT $limitvalue, $limit";
$result = mysql_query($query) or die("Error: " . mysql_error());

    foreach ($ads as $adRecord) {
        $ad_url = "?action=display&adid=".$adRecord[id]."&$url_params";
        if ($cur_date!=date("D M d",$adRecord[epoch_date])) {
            $cur_date = date("D M d",$adRecord[epoch_date]);
            /*echo "<tr><td colspan='3'> </td></tr>";
            echo "<tr class='ad_date_separator'><td colspan='3'>".date("D M d",$adRecord[epoch_date])."</td></tr>";*/
        }

        $price = $adRecord[price];
        if (!preg_match("/[a-z]/i",$price)&&!preg_match("/\\$/",$price)) { $price = "$".$price; }

        $row_color = ($row_color!='#ffffdd') ? '#ffffdd' : '#ffffff';

        echo "<tr style=\"background-color:$row_color;\">\n";
        echo "  <td>\n";
        echo "    <B><a href='$ad_url'>".$adRecord[title]."</a></B>\n";
        if (strlen($adRecord[city])) {
            echo "    <a href='$ad_url'>(".$adRecord[city].")</a>\n";
        }
        if (strlen($adRecord[price])) {
            echo "    <span class='adlist_price'>".substr($price,0,10)."</span>\n";
        }
        if (intval($adRecord[num_pics])) {
            echo "    <img src='images/picture.gif' border='0' style='vertical-align:middle'>\n";
        }
        echo "    <br>$adRecord[content]\n";
	echo "<br><br>";
        echo "  </td>";
        echo "</tr>";
    }
    echo "</table>\n";
    echo "</div>\n";

if($page != 1){  
 $pageprev = $page--;
 echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> ");   
 }else
        echo("PREV".$limit." "); 
	$numofpages = $totalrows / $limit;  
	for($i = 1; $i <= $numofpages; $i++){ 
		if($i == $page){
			echo($i." ");
		}else{
			echo("<a href=\"$PHP_SELF&page=$i\">$i</a> "); 
			} 
	} 
	if(($totalrows % $limit) != 0){   
        if($i == $page){
            echo($i." ");
			}else{
            echo("<a href=\"$PHP_SELF&page=$i\">$i</a> ");
			}
	}
	if(($totalrows - ($limit * $page)) > 0){ 
		 $pagenext   = $page++; 
	  echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>"); 
	      }else{
        echo("NEXT".$limit);  
	    } 
	mysql_free_result($result); 
}
  
?>

 

I tried the method from the tutorial to get counts, by using

 

$query_count = sprintf("SELECT * FROM `ad` WHERE `sub_category_id` = '{$_REQUEST[subcategoryid]}' AND `region_id` = '{$_REQUEST[regionid]}'");
$result = mysql_query($query_count)or die(mysql_error());
$count = mysql_num_rows($result);
$limit = 10;  

if(empty($page)){
    $page = 1;
    } 

    $limitvalue = $page * $limit - ($limit); 

 

but, in my original php the data is picked up by

 

    $ads = $info->getAds($_REQUEST[regionid],$_REQUEST[subcategoryid]);

 

I dont know how to make a output limit with that, please help.

Link to comment
Share on other sites

where you have this in the links:

<?php
$PHP_SELF

 

remove that and put in the full URL with the vpage at the end.

 

note: with that tutorial, I have implemented it into my site as well, but the links will be stuffed up, like no matter what page you are on, the number '1' will never be a link, while the rest of the numbers will be links to the other pages. Also note the NEXT link and the PREV link do each others job, the NEXT button goes to the previous page, and the PREV button goes to the next page. Im still trying to fix it  :-\

 

Regards ACE

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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