You are here: Re: Stuck with foreach & while « All PHP « IT news, forums, messages
Re: Stuck with foreach & while

Posted by JDS on 11/25/05 19:57

On Thu, 24 Nov 2005 20:43:38 +0100, Juliette wrote:

> $i = 0;
> foreach ($myarray as $value) {
> $index++;
> print($index . ". ");
> print($value);
> $i++;
> if ($i > 100) {
> break;
> }
> }
>
> Hope this helps,
> Jrf

But that only prints the first 100. Also, there is some bit of redundancy
-- no need to use $i AND $index when both are doing the same thing.

To print the lot in 100-per-"page" chunks is a bit more complex. You have
to:

* Break the array into 100 piece chunks
* Remember from page to page what the last chunk was
* pass that information from page to page

Also, I recommend using a "for" and not a "foreach" loop. It doesn't
matter except for code cleanlines, though.

So assuming an array holds more than 100 items...
(warning, this code is untested)
<?

$page_size = 100;
$page_start=$_GET['page_start']?$_GET['page_start']:0;

$myarray_length = count($myarray);

$subarray = array_slice($myarray, $page_start, $page_size);
$subarray_size = count($subarray);

for ($i = 0; $i < $subarray_size; $i++){
print($i + $page_start . ". ");
print($subarray[$i]) . "<br>\n";
}

print "<a href='?page_start=" . ($page_start - 100) . "'>Last Page</a>";
print "<a href='?page_start=" . ($page_start + 100) . "'>Next Page</a>";
?>

I may have a typo or two in there, and I may have outright screwed
something up, but hopefully you get the idea. later...

--
JDS | jeffrey@go.away.com
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация