Jump to content

Recommended Posts

Lets say that I have several arrays, as follows:

 

$array1 = ('A', 'B', 'C', 'D', 'E', 'F', 'G');

$array 2 = (3, 4, 5, 6, 7, 8);

$array 3 = ('!', '@', '#', '$', '%', '^');

 

These three arrays are stored as session variables, and the page showthem.php displays the arrays.  However, the page can only display a maximum of 4 items at a time.  Therefore, the pages should display as follows:

 

Page 1 =

-> Table 1 (LETTERS) A, B, C, D

 

Page 2 =

-> Table 1 (LETTERS) E, F, G

-> Table 2 (NUMBERS) 3

 

Page 3 =

-> Table 1 (NUMBERS) 4, 5, 6, 7

 

Page 4 =

-> Table 1 (NUMBERS) 8

-> Table 2 (CHARACTERS) !, @, #

 

Page 5 =

-> Table 1 (CHARACTERS) $, %, ^

 

I can't figure out how to use the count() function and the array indexes to create proper pagination on showthem.php

 

Obviously I would like, at the top to have page numbers that link to the appropriate content from the arrays.

 

As in:

 

<a href="showthem.php?page=1"> 1 </a>

<a href="showthem.php?page=2"> 2 </a>

<a href="showthem.php?page=3"> 3 </a>

<a href="showthem.php?page=4"> 4 </a>

<a href="showthem.php?page=5"> 5 </a>

 

This is a major puzzle for me ... thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/253664-pagination-of-several-arrays/
Share on other sites

You paginate an array the same way that you would paginate rows from a database, except the values in the LIMIT clause would produce the starting and ending array index values.

 

Assuming that you started with the phpfreaks pagination tutorial (http://www.phpfreaks.com/tutorial/basic-pagination), you would do the following -

 

<?php
$arr = array_merge($array1,$array2,$array3); // your combined array

// find out how many rows are in the array
$numrows = count($arr);

 

<?php
// the code that gets the specific rows of data and loops over them -
$end = $offset + $rowsperpage; // index wise, this is actually one past the end, to avoid extra -1's in the code. the loop stops at one less than this value.
if($end > $numrows){$end = $numrows;} // limit value to prevent undefined index errors (a db doesn't care if the LIMIT is past the end of the data, but an array does)

// while there are rows to be fetched...
for($i = $offset; $i < $end; $i++) {
   // echo data
   echo "$i : {$arr[$i]}<br />";
} // end loop

Thank you for your reply.

 

I'm with you from start to finish.  I've actually created pagination for MySQL results in the past, which was relatively simple compared to this scenario.

 

I've done what you show in your first step:

 

$arr = array_merge($array1,$array2,$array3); // your combined array
// find out how many rows are in the array
$numrows = count($arr);

 

The problem with the second part is that I still have to keep the arrays separate.  I can't just create pagination from one merged array, which is substantially easier.  As you can see from my original example, each page creates a table or tables based on the arrays that are drawn from on that page.  So for example, if one page draws data from $array2 and $array3 then two tables have to be created.  The first table would display data from $array2, and the other table would display data from $array3.  The next page would resume from $array3, assuming that there were elements in $array3 that were not shown in the previous page.

 

Do you know what I mean?

You'll have to go through the arrays and keep a total count of items.  When your total count falls inside the visible range you output those items.  You'll need a way to know when to show the table as well.

 

Not the most elegant solution, but the following is an example:

<?php

$sourceArray = array(
array('A', 'B', 'C', 'D', 'E', 'F', 'G'),
array(3, 4, 5, 6, 7, ,
array('!', '@', '#', '$', '%', '^')
);


$start=5;
$len=10;

$sourceIdx=0;
$pos=0;
do {
$arr = $sourceArray[$sourceIdx];
$arrLen = count($arr);
if ($pos+$arrLen > $start){
	echo "\r\nIn array {$sourceIdx}\r\n";
	for ($i=0; $len > 0 && $i<$arrLen; $i++,$pos++){
		if ($pos >= $start){
			echo $arr[$i];
			$len--;
		}
	}
}
else {
	$pos += $arrLen;
}

$sourceIdx++;
} while ($len > 0 && $sourceIdx < count($sourceArray));

?>

Is this the sort of thing you were after?

 

<?php

$array1 = ( 'A', 'B', 'C', 'D', 'E', 'F', 'G' );
$array2 = ( 3, 4, 5, 6, 7, 8 );
$array3 = ( '!', '@', '#', '$', '%', '^' );

$lettersOffset = sizeof( $array1 );
$numbersOffset = ( sizeof( $array2 ) + $lettersOffset );
$charactersOffset = ( sizeof( $array3 ) + $numbersOffset );

$lettersHeader = 'LETTERS';
$numbersHeader = 'NUMBERS';
$charactersHeader = 'CHARACTERS';

$array = array_merge( $array1, $array2, $array3 );

// If you have large data sets you should deallocate your arrays when merging data as it is unneeded
unset( $array1 );
unset( $array2 );
unset( $array3 );

$perPage = ( isset( $_GET[ 'perPage' ] ) && ( $pp=$_GET[ 'perPage' ] ) > 0 ) ? $pp : 4;
$page = ( isset( $_GET[ 'page' ] ) && ($p=$_GET[ 'page' ] ) > 0 ) ? $p : 1;
$start = ( $perPage * ( $page - 1 ) );

$currentHeader = null;

for( $i = $start; $i < ( $start+$perPage); $i++ )
{
if( is_null( $currentHeader ) || 
	in_array( ( $i + 1 ), array( $lettersOffset, $numbersOffset, $charactersOffset ) ) )
{
	printf( "<h3>%s</h3>",
		( ( $i < $lettersOffset ) ? $lettersHeader : 
		( ( $i < $numbersOffset ) ? $numbersHeader : $charactersOffset ) ) );
	// This is where you would start/end tables
	$currentHeader = 1;
}
printf( "<p>%s</p>",
	$array[ $i ] );
}

 

I've not tested it just threw it togeather. The logic should be sound.

After participating in a few of your recent array based threads and having just reviewed the rest of them, I'm thinking that if you actually show the source of this data and what result you are trying to achieve, that someone can probably give you a general solution that will work. You seem to be throwing a lot of code at a pieces of problem, when a simpler overall solution is probably available, but that would require knowing enough about what you are actually trying to achieve in order to help. We regularly find ways of consolidating multiple-hundred lines of complicated code into just a few lines, once the overall problem has been reveled.

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.