Jump to content

pagination and foreach


ryan king

Recommended Posts

the reason is the $zips array contais the list of zip codes found near that user, these zip codes are basically what you are displaying on ur tables, the mysql query is only there to get "more detailed" information about those zip codes.

 

when you use a foreach, it s a For Each Zip Code.... Grab its single row.... since you cannot limit a single row, theres no need. you are getting the original "Rows" from the zip code function. all your doing with mysql is getting more information for each of those rows.

 

Hope this helps clear things up.

Link to comment
Share on other sites

Thats understandable. now I have made the changes. before I made the changes it would only display 16 results. now it displays all of them (except for it limits to 50 using that old count I had included that I no longer need)

 

Also, its still displaying the same results on every page.

 

<?php 

print "<link href='/skins/Cobalt/Cobalt.css' rel='stylesheet' type='text/css' media='screen'>";
require('include/jamroom-include.inc.php'); 
require_once('zipcode.class.php');      // zip code class 

$row_count = 0; 
$z = new zipcode_class; 
$zips = $z->get_zips_in_range($_POST['zip_code'], $_POST['miles'], _ZIPS_SORT_BY_DISTANCE_ASC, true); 

// add searched for zip to $zips array 
$zips[$_POST['zip_code']] = 0; 
$row_count = 0; 


$prevno = $_GET['page'] - 1;
$nextno = $_GET['page'] + 1;
$startrow = (isset($_GET['page']) && $_GET['page'] != 0)? ($_GET['page'] - 1) * $rowsperpage : 0;
$rowsperpage = 16;
// this is just to initialise the variable:
$result_array = array();

// first we gather all the results into a single array:
foreach ($zips as $key => $value){
//find all locations within range using returned zipcode values 
$sql_events = mysql_query("SELECT * FROM jamroom_band_info WHERE band_zipcode='$key' ORDER BY band_update DESC");
while ($row = mysql_fetch_array($sql_events)) 
{
	// we just add this row to the array:
	$result_array[] = $row;
}
}
// config i guess 
$entriesperline=4;
$counter=1;
print "<table>";
$i = 0;


Function array_limit($array,$start,$length){
$return_array = array();
for($i=$start;$i<($start + $length);$i++){
	$return_array[] = $array[$i];
}
return $return_array;
}

$zips = array_limit($zips,$startrow,$rowsperpage);
// loop through each result row.
foreach ($result_array As $row){

 if($counter%$entriesperline==1 && $i < 50){

                               		print "<tr><td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></div></td>";
}else if($counter%$entriesperline==0 && $i < 50){
                                             print "<td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></td></div></tr>";
}else if($i < 50){
                                	print "<td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></div></td>";
}
$counter++;
  $i++; 

} //exit loop

if($counter%$entriesperline!=0){
print "</tr>";
}
print "</table>";

echo("<a href=\"./azippyyy.php?page=$prevno\">Prev</a> | ".htmlentities($_GET['page'])." | <a href=\"./azippyyy.php?page=$nextno\">Next</a>"); 

?>

Link to comment
Share on other sites

Ok, you need to understand where to put the code i gave you. You need to understand this its very basic so im only going to give you hints im afraid, dont worry though, you will hit yourself once u figure it out.

 

You need to move a variable i mentioned earlier.

 

and you need to move the $zips = array_limit.... line, the function code can be put anywhere (i put all my functions in a seperate file but i suggest putting them at the top at least).

 

Read through and follow your code line by line. you can use echo() and print_r() (for arrays), to see what the value of each variable is at a specific point in the script - it really helps.

Link to comment
Share on other sites

ok.. well I get a value of 16 on rowperpage anywhere I put it in the script. this one gives me diffrent values but mostly does this up to 15

 

 

print_r($zips = array_limit($zips,$startrow,$rowsperpage));

 

Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] =>

[13] => [14] => [15] => ) 

Link to comment
Share on other sites

I think it would make since for rowsperpage to go here above this:

 

<?php
$rowsperpage = 16;
$prevno = $_GET['page'] - 1;
$nextno = $_GET['page'] + 1;
$startrow = (isset($_GET['page']) && $_GET['page'] != 0)? ($_GET['page'] - 1) * $rowsperpage : 0;

?>

 

since it then gets call as part of the startrow variable

Link to comment
Share on other sites

man.. I cant figure out for the life of me where it should go, Ive tried everywhere

it would make sense to go after the $zips as $key which is after the $rowsperpage.. those all have to be defined before I cant start this array correct? so I figure it should go right before the foreach loop where it currently is. I either get a blank page or I get the count to 15 deal whereever I put it

Link to comment
Share on other sites

it works! I had the thing in the right place ALL along. I had screwed something else up.. I went back to my code before I started editing with your latest function changes and added in everything.. then moved the stuff that needed to be moved and viola!

 

here is the final outcome:

<?php 

print "<link href='/skins/Cobalt/Cobalt.css' rel='stylesheet' type='text/css' media='screen'>";
require('include/jamroom-include.inc.php'); 
require_once('zipcode.class.php');      // zip code class 

Function array_limit($array,$start,$length){
$return_array = array();
for($i=$start;$i<($start + $length);$i++){
	$return_array[] = $array[$i];
}
return $return_array;
}


$row_count = 0; 
$z = new zipcode_class; 
$zips = $z->get_zips_in_range($_POST['zip_code'], $_POST['miles'], _ZIPS_SORT_BY_DISTANCE_ASC, true); 

// add searched for zip to $zips array 
$zips[$_POST['zip_code']] = 0; 
$row_count = 0; 

$rowsperpage = 16; // 4x4 grid - u can change this variable to change how many each page, without having to change any of the other code.
$prevno = $_GET['page'] - 1;
$nextno = $_GET['page'] + 1;
$startrow = (isset($_GET['page']) && $_GET['page'] != 0)? ($_GET['page'] - 1) * $rowsperpage : 0;

// this is just to initialise the variable:
$result_array = array();

// first we gather all the results into a single array:
foreach ($zips as $key => $value){
//find all locations within range using returned zipcode values 
$sql_events = mysql_query("SELECT * FROM jamroom_band_info WHERE band_zipcode='$key' ORDER BY band_update DESC LIMIT ".$startrow.",".$rowsperpage);
while ($row = mysql_fetch_array($sql_events)) 
{
	// we just add this row to the array:
	$result_array[] = $row;
}
}
// config i guess 
$entriesperline=4;
$counter=1;
print "<table>";

// loop through each result row.
$i = 0;
$zips = array_limit($zips,$startrow,$rowsperpage);


foreach ($result_array As $row){

 if($counter%$entriesperline==1 && $i < 50){
                               		print "<tr><td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></div></td>";
}else if($counter%$entriesperline==0 && $i < 50){
               
                	print "<td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></td></div></tr>";
}else if($i < 50){
                                		print "<td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></div></td>";
}
$counter++;
  $i++; 

} //exit loop

if($counter%$entriesperline!=0){
print "</tr>";
echo("<a href=\"./azippyyy.php?page=$prevno\">Prev</a> | ".htmlentities($_GET['page'])." | <a href=\"./azippyyy.php?page=$nextno\">Next</a>"); 
}

print "</table>";

?>

Link to comment
Share on other sites

yes.. I even have it displaying in my smarty templates going page to page like it should

However. SMALL problem.. it starts at 0 so when I click next page it goes to page 1 which ofcourse displays the same results, then goes to page 2 and the results change.. we need to start it at page 1, not 0.

Link to comment
Share on other sites

I fixed it.. I changed

$startrow = (isset($_GET['page']) && $_GET['page'] != 0)? ($_GET['page'] - 1) * $rowsperpage : 0;

 

to

 

$startrow = (isset($_GET['page']) && $_GET['page'] != 1)? ($_GET['page'] + 1) * $rowsperpage : 0;

 

this way it starts at page 1 and displays page 1 on the page counter.

Link to comment
Share on other sites

Ok, im going to end this im afraid, if you dont understand the code have a good look through.

 

To save time and page space, and me repeating myself again (no offense), here's the code that shud work:

 

<?php 

print "<link href='/skins/Cobalt/Cobalt.css' rel='stylesheet' type='text/css' media='screen'>";
require('include/jamroom-include.inc.php'); 
require_once('zipcode.class.php');      // zip code class 

// FUNCTIONS
Function array_limit($array,$start,$length){
   $return_array = array();
   for($i=$start;$i<($start + $length);$i++){
      $return_array[] = $array[$i];
   }
   return $return_array;
}

// creates a url without destroying the current one. (keeping it on the same page with the same values).
function make_url($id_list){
   
   if(!is_Array($id_list)){
      echo("error, function make_url(arg), arg should be an array.");
      return;
   }
   
   // add each element
   foreach($id_list as $id_str=>$id_val){
      
      if($id_val === false){ // must use ===, since if you use ==, then 0, false, and null mean the same thing.
         
         $url = (!isset($url))? null : $url;
         
         $url = str_replace(
            array(
               "&".$id_str."=".@$_GET[$id_str],
               $id_str."=".@$_GET[$id_str]."&",
               $id_str."=".@$_GET[$id_str]
               ), 
            "", 
            $url
         );
      }else{
         // For the loop
         $url = (!isset($url))? $_SERVER['REQUEST_URI'] : $url;
         
         // if page is alread in the url somewhere get rid of it (or we will have lots of them,).
         $url = (isset($_GET[$id_str]))? str_replace(
            array(
               "&".$id_str."=".@$_GET[$id_str],
               $id_str."=".@$_GET[$id_str]."&",
               $id_str."=".@$_GET[$id_str]
               ), 
            "", 
            $url
         ) : $url; // the : colon is like the }else{ part of an if statement.

         $preq = (substr($url,-1) != "?")? "?" : "";

         // if there is already arguments u need to add the &, otherwise you need to add the ?.
         $url = ((count($_GET) == 1 && isset($_GET[$id_str])) || count($_GET) == 0)? $url.$preq.$id_str."=" : $url."&".$id_str."=".$id_val;
      }
   }
   
   return $url;
}
// END FUNCTIONS

// GRAB ZIPS
$z = new zipcode_class; 
$zips = $z->get_zips_in_range($_POST['zip_code'], $_POST['miles'], _ZIPS_SORT_BY_DISTANCE_ASC, true); 

// add searched for zip to $zips array 
$zips[$_POST['zip_code']] = 0; 

// make sure this is a multiple of $entriesperline;
$rowsperpage = 16; // 4x4 grid - u can change this variable to change how many each page, without having to change any of the other code.
$entriesperline=4;

// PAGE VARIABLES
$prev = make_url(
   array(
      "page"=>@$_GET['page'] - 1
   )
);
$next = make_url(
   array(
      "page"=>@$_GET['page'] + 1
   )
);

// Figure which row to start from.
$startrow = (isset($_GET['page']) && $_GET['page'] != 0)? ($_GET['page'] - 1) * $rowsperpage : 0;

// this is just to initialise the variable:
$result_array = array();

// Take only the result for this page...
$zips = array_limit($zips,$startrow,$rowsperpage);

// first we gather all the results into a single array:
foreach ($zips as $key => $value){
   //find all locations within range using returned zipcode values 
$sql_events = mysql_query("SELECT * FROM jamroom_band_info WHERE band_zipcode='$key' ORDER BY band_update DESC LIMIT 1");
while ($row = mysql_fetch_array($sql_events)) 
{
	// we just add this row to the array:
	$result_array[] = $row;
}
}

// Used for Modulus
$counter=1;
print "<table>";

// loop through each result row.
$i = 0; // debug?

foreach ($result_array As $row){

    if($counter%$entriesperline==1 && $i < 50){
                                     print "<tr><td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></div></td>";
   }else if($counter%$entriesperline==0 && $i < 50){
                  
                   print "<td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></td></div></tr>";
   }else if($i < 50){
                                      print "<td align='center'><div class='body-zip'><a href=\"members/{$row['band_id']}\">{$row['band_name']}</a><br><a href=\"members/{$row['band_id']}\"><img border='0' src=\"image.php?band_id={$row['band_id']}&mode=band_image&width=100&theme=Sage\">{$row['band_image']}</a></div></td>";
   }
   $counter++;
  $i++; 

} //exit loop

if($counter%$entriesperline!=0){
   print "</tr>";
   // Add my big function i told you about earlier to fix this header/footer problem.
echo("<a href=\"$prev \">Prev</a> | ".htmlentities($_GET['page'])." | <a href=\"$next\">Next</a>"); 
}

print "</table>";

?>

 

TAKE NOTE OF WHERE I PUT EVERYTHING, and figure out why.

 

Thank you, and good night :P.

Link to comment
Share on other sites

ok.. after getting some sleep and wakign up with a clear mind. I realize that the script is only paginating results if you dont not give a zipcode.. basicly its only paginating ALL results.

 

when using thhis string.. it will paginate and limit ALL results, but not zipcodes entered

 

<?php
$sql_events = mysql_query("SELECT * FROM jamroom_band_info WHERE band_zipcode='$key' ORDER BY band_update DESC LIMIT ".$startrow.",".$rowsperpage); 
?>

 

I used your code above and it gives me a blank page and it doesnt work it all if I use:

<?php
$sql_events = mysql_query("SELECT * FROM jamroom_band_info WHERE band_zipcode='$key' ORDER BY band_update DESC");
?>

 

the placement of this doesnt help either even though we know its in the right place:

<?php
// this is just to initialise the variable:
$result_array = array();

$zips = array_limit($zips,$startrow,$rowsperpage);


// first we gather all the results into a single array:
foreach ($zips as $key => $value){


 

I gotta figure out a way to LIMIT the results of zips in my userdata base, and Im still stumped on how to do this correctly.

 

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.