Re: what to use instead of foreach

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Leon,
Thanks for the suggestion; I'm quite new to all this, so it's a bit
complicated for my peanut brain.
I have already tried with several count and for schemes. None work
because foreach ignores any counters once in the loop. Also, this
foreach is nested within another foreach; don't know if that affects
anything.
I'll try to understand the second suggestion using for. I'll see what
comes up.
There are actually several conditions that have to be met:
1. if only 1 author = echo "author<br>"
2. if 2 authors = echo "author & author1<br>"
3. if more than 2 authors = echo "author, author1, author2 & author3<br>"
That's what makes it a "toughie"

Leon du Plessis wrote:
> You may try something basic like:
>
> $b = 1;
> foreach ($my_array as $a)
> {
> echo " $a ";
>
> //Send new line to browser
> if ($b++ == 3) { echo "<br>"; $b = 1; }
> }
>
> Or there are some different ways to approach this also like:
> for ($a =& current($my_array); $a; $a = next($my_array))
> {
> //Format 1
> echo " $a ";
> $a = next($my_array);
>
> //Format 2
> /* you may add checks here to see if $a contains data */
> echo " ~ $a ~ "; $a = next($my_array);
>
> //Format 3 + NEW LINE
> /* you may add checks here to see if $a contains data */
> echo " ~~ $a ~~<br> ";
> }
>
> This way you have some added control over the iteration through the array,
> and you can play around with when & how to display what.
>
> Regards.
>
> -----Original Message-----
> From: PJ [mailto:af.gourmet@xxxxxxxxxxxx]
> Sent: 12 April 2009 08:57 PM
> To: php-general@xxxxxxxxxxxxx
> Subject:  what to use instead of foreach
>
> foreach does not allow for different formatting for output...
> What could be used as a workaround?
> example:
> echo $some_result, "<br>"; // will print all results in 1 column
> echo $some_result, ","; // will print all results comma-separated in 1 row
>
> But how do you get result1, result2 & result3 // with <br> at end ?

-- 
unheralded genius: "A clean desk is the sign of a dull mind. "
-------------------------------------------------------------
Phil Jourdan --- pj@xxxxxxxxxxxxx
http://www.ptahhotep.com
http://www.chiccantine.com/andypantry.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux