xProteuSx Posted December 22, 2011 Share Posted December 22, 2011 Lets say that I have several arrays, as follows: $array1 = ('A', 'B', 'C', 'D', 'E', 'F', 'G'); $array 2 = (3, 4, 5, 6, 7, ; $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. Quote Link to comment https://forums.phpfreaks.com/topic/253664-pagination-of-several-arrays/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 22, 2011 Share Posted December 22, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/253664-pagination-of-several-arrays/#findComment-1300434 Share on other sites More sharing options...
xProteuSx Posted December 22, 2011 Author Share Posted December 22, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/253664-pagination-of-several-arrays/#findComment-1300613 Share on other sites More sharing options...
kicken Posted December 22, 2011 Share Posted December 22, 2011 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)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/253664-pagination-of-several-arrays/#findComment-1300636 Share on other sites More sharing options...
AGuyWithAthing Posted December 22, 2011 Share Posted December 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253664-pagination-of-several-arrays/#findComment-1300639 Share on other sites More sharing options...
xProteuSx Posted December 24, 2011 Author Share Posted December 24, 2011 Aguywithathing, I've studied your code, and tested it. The output it produces is this: LETTERS A B C D Not what I was going for ... Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/253664-pagination-of-several-arrays/#findComment-1301124 Share on other sites More sharing options...
AGuyWithAthing Posted December 24, 2011 Share Posted December 24, 2011 What does it not do apart from create tables and pagination links? If you want to test the pagination append "?page=2" to the end of the url Quote Link to comment https://forums.phpfreaks.com/topic/253664-pagination-of-several-arrays/#findComment-1301155 Share on other sites More sharing options...
PFMaBiSmAd Posted December 24, 2011 Share Posted December 24, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253664-pagination-of-several-arrays/#findComment-1301177 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.