Pagination Lists All Pages Problem

Discussion in 'PHP' started by Mirial, Jun 2, 2008.

  1. #1
    The following code works correctly in the sense that it displays only 30 results per page as I need it to, but it also lists numbered pages in the thousands beneath the results when the database has thousands of entires.

    Can anyone point me in the right direction for fixing the code to where the page results are shortened?

    I don't care if they show a <<Previous 1 2 3 4 5 Next>> or if they simply number 1 to 10 on the first page, with pages after being picked up as they browse through the pages, like

    Page 1

    Got To Page: 1 2 3 4 5 6 7 8 9 10

    Page 2

    Go To Page: 2 3 4 5 6 7 8 9 10 11

    etc.

    I have searched google as well as the forums here for days now, and every solution I attempt either gives me errors, or causes no results to display at all.

    If anyone can asist me I would greatly appreciate it.

    
    
    //Get # of ads 
        $result = query("SELECT COUNT(ad_id) FROM {$class_prefix}ads WHERE ad_status=1 AND ad_category=$cat $localQuery $typeQuery",$class_link); 
        list($numAds) = mysql_fetch_row($result); 
    
        //Get the ads 
        $limit = (($pg*30)-30).",30"; 
        $result = query("SELECT ad_id,ad_category,ad_status,ad_options,ad_title,ad_condition,ad_price,ad_description,ad_city,ad_state,ad_postal,ad_country,ad_image1,ad_date_begin,ad_date_end,ad_type,ad_type_optional1,ad_type_optional2,ad_type_optional3,ad_type_optional4,ad_type_optional5,ad_type_optional6,ad_type_optional7,ad_type_optional8,ad_type_optional9,ad_type_optional10,ad_city_code,ad_state_code,ad_country_code FROM {$class_prefix}ads WHERE ad_status=1 AND ad_category=$cat $localQuery $typeQuery $sort_text LIMIT $limit",$class_link); 
        while(list($ad_id,$ad_category,$ad_status,$ad_options,$ad_title,$ad_condition,$ad_price,$ad_description,$ad_city,$ad_state,$ad_postal,$ad_country,$ad_image1,$ad_date_added,$ad_date_ends,$ad_type,$ad_opt_1,$ad_opt_2,$ad_opt_3,$ad_opt_4,$ad_opt_5,$ad_opt_6,$ad_opt_7,$ad_opt_8,$ad_opt_9,$ad_opt_10,$ad_city_code,$ad_state_code,$ad_country_code) = mysql_fetch_row($result)) { 
    
            //Check local database 
            if($ad_city_code != 0) { 
                if($cityCache['city'][$ad_city_code] == "") { 
                    $result2 = query("SELECT cityName FROM bd_world_cities WHERE cityID=$ad_city_code",$uus_link); 
                    list($ad_city) = mysql_fetch_row($result2); 
                    $cityCache['city'][$ad_city_code] = $ad_city; 
                    } 
                    else { $ad_city = $cityCache['city'][$ad_city_code]; } 
                } 
            if($ad_state_code != 0) { 
                if($cityCache['region'][$ad_state_code] == "") { 
                    $result2 = query("SELECT rName FROM bd_world_regions WHERE rID=$ad_state_code",$uus_link); 
                    list($ad_state) = mysql_fetch_row($result2); 
                    $cityCache['region'][$ad_state_code] = $ad_state; 
                    } 
                    else { $ad_state = $cityCache['region'][$ad_state_code]; } 
                } 
            if($ad_country_code != 0) { 
                if($cityCache['country'][$ad_country_code] == "") { 
                    $result2 = query("SELECT cName FROM bd_world_countries WHERE cID=$ad_country_code",$uus_link); 
                    list($ad_country) = mysql_fetch_row($result2); 
                    $cityCache['country'][$ad_country_code] = $ad_country; 
                    } 
                    else { $ad_country = $cityCache['country'][$ad_country_code]; } 
                } 
    
            //Get the display data 
            $ads[$ads_counter] = getDisplay($ad_id,$ad_category,$ad_status,$ad_options,$ad_title,$ad_condition,$ad_price,$ad_description,$ad_city,$ad_state,$ad_postal,$ad_country,$ad_image1,$ad_date_added,$ad_date_ends,$ad_type,$ad_opt_1,$ad_opt_2,$ad_opt_3,$ad_opt_4,$ad_opt_5,$ad_opt_6,$ad_opt_7,$ad_opt_8,$ad_opt_9,$ad_opt_10); 
            $ads_counter++; 
            } 
    
        if($ads_counter != 0) { 
            $classifiedsList .=<<<ENDPRINT 
            $sortTable 
            <div id="menu" align="center"> 
            <table width="100%" border="0" cellspacing="0" cellpadding="3"> 
            <form method="post" action="{$insUrl}index.php"> 
            <input type="hidden" name="cat" value="$cat"> 
            <input type="hidden" name="setFilterType" value="1"> 
             <tr> 
              <td>{$Languages['index']['results']}{$local}</td> 
              <td align="right"><select name="filterType" onChange="this.form.submit();"><option value="0">{$Languages['recent']['showall']}</option>$adTypeOptions</select></td> 
             </tr> 
            </form> 
            </table> 
            </div> 
            <div id="listings" align="center"> 
            <table width="100%" border="0" cellspacing="1" cellpadding="3"> 
    ENDPRINT; 
            if(substr($SystemOptions['page_width'],-1) == "%") { $colWidth = floor($SystemOptions['page_width'] / $SystemOptions['list_columns']) . "%"; } 
                else { $colWidth = floor($SystemOptions['page_width'] / $SystemOptions['list_columns']); } 
    
            for($i=0; $i<=29; $i++) { 
                if($ads[$i] == "") { continue; } 
                $classifiedsList .= "<tr>"; 
                for($ii=0; $ii<=$SystemOptions['list_columns']-1; $ii++) { 
                    $j = $i + $ii; 
                    $classifiedsList .= "<td width=\"$colWidth\" valign=\"top\">$ads[$j]</td>"; 
                    } 
                $classifiedsList .= "</tr>"; 
                $i = $i + $SystemOptions['list_columns'] - 1; 
                } 
            $classifiedsList .= "</table></div>"; 
    
            if($numAds > 30) { 
                $classifiedsList .=<<<ENDPRINT 
                <div id="info" align="center"> 
                <table width="100%" border="0" cellspacing="1" cellpadding="5"> 
                 <tr> 
                  <td align="right">{$Languages['index']['gotopage']} 
    ENDPRINT; 
                $pages = ceil($numAds/30); 
                for($i=1; $i<=$pages; $i++) { 
                    if($pg == $i) {    $classifiedsList .= "<span style=\"font-weight:bold; text-decoration:underline;\">{$i}</span> "; } 
                        else { 
                            switch($SystemOptions['spider']) { 
                                case 0: $classifiedsList .= "<a href=\"{$insUrl}index.php?cat=$cat&pg=$i&sort=$sort&sort_dir=$sort_dir\">{$i}</a> "; break; 
                                case 1: 
                                case 2: 
                                    $classifiedsList .= "<a href=\"{$insUrl}index.php/cat/$cat/pg/$i/sort/$sort/sort_dir/$sort_dir\">{$i}</a> "; 
                                    break; 
                                } 
                            } 
                    } 
                $classifiedsList .= "</td></tr></table></div>"; 
                } 
            } 
    
        if($ads_counter == 0 && $cat != 0) { 
            $classifiedsList .=<<<ENDPRINT 
            <div id="menu" align="center"> 
            <table width="100%" border="0" cellspacing="1" cellpadding="3"> 
            <form method="post" action="{$insUrl}index.php"> 
            <input type="hidden" name="cat" value="$cat"> 
            <input type="hidden" name="setFilterType" value="1">         
             <tr> 
              <td>{$Languages['index']['results']}{$local}</td> 
              <td align="right"><select name="filterType" onChange="this.form.submit();"><option value="0">{$Languages['recent']['showall']}</option>$adTypeOptions</select></td> 
             </tr> 
            </form> 
            </table> 
            </div> 
            <div id="listings" align="center"> 
            <table width="100%" border="0" cellspacing="1" cellpadding="3"> 
             <tr> 
              <td align="center">{$Languages['index']['noads']}</td> 
             </tr>         
            </table> 
            </div> 
    ENDPRINT; 
            } 
    
        return $classifiedsList; 
        } 
    
    PHP:
    This is the section of the code that displays the page numbers, but as I said, it displays all number 1 to 1000+ and that is not only very ugly, but also very confusing to site visitors.

    
    ENDPRINT; 
                $pages = ceil($numAds/30); 
                for($i=1; $i<=$pages; $i++) { 
                    if($pg == $i) {    $classifiedsList .= "<span style=\"font-weight:bold; text-decoration:underline;\">{$i}</span> "; } 
                        else { 
                            switch($SystemOptions['spider']) { 
                                case 0: $classifiedsList .= "<a href=\"{$insUrl}index.php?cat=$cat&pg=$i&sort=$sort&sort_dir=$sort_dir\">{$i}</a> "; break; 
                                case 1: 
                                case 2: 
                                    $classifiedsList .= "<a href=\"{$insUrl}index.php/cat/$cat/pg/$i/sort/$sort/sort_dir/$sort_dir\">{$i}</a> "; 
                                    break; 
    
    PHP:
     
    Mirial, Jun 2, 2008 IP
  2. Altari

    Altari Peon

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What have you tried? My first inclination would be to add an if statement to your for loop

    if ($i <= 5 || $i >= ($pages - 5)) {
     execute;
    }
    PHP:
    Would, theoretically, only return pages 1-5 and the last 5 pages...
     
    Altari, Jun 2, 2008 IP
  3. Mirial

    Mirial Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for the help Altari. I have it working fine now, and a rep goes to you, thank you VERY much!
     
    Mirial, Jun 2, 2008 IP