You are here: Re: PHP Foreach HELP! « PHP Programming Language « IT news, forums, messages
Re: PHP Foreach HELP!

Posted by Steve on 10/21/20 11:34

> I have this bit of code, that isn't valid, and I need some help getting
> it to work... Currently, I get an error because I am calling the next()
> function within a foreach loop. I am opening a flat file with data in
> the format of:

> 01|data1|data2

> <?
>
> $gallery = 03;
>
> $data = file('gallery.txt');
>
> echo "<table border=0 width=500 align=center>";
> foreach ($data as $line)
> {
> list ($g, $q, $a) = explode('|' , trim($line) );
> if ( $g != $gallery ) next($line);
> else
> {
> echo "<td width=50% valign=top><img src='/gallery/$q' width=220><br>";
> echo "$a<br>";
> echo "<a href=\"/gallery/$q\">Enlarge</a><br><br></td>";
> $ee++;
> //2 - row
> if($ee%2==0) echo "<tr>";
> }
> echo "</table>";
>
> }
>
>
> ?>

Don't mix foreach() and next(). Change the test around to avoid it, and
use break to quit the test after a successful match (make sure that
your data is sorted by gallery).

foreach ($data as $line)
{

// ...

if( $g == $gallery )
{
// ...do the table output here...

// then get out of the loop:
break;
}

}

// this should be outside the foreach() loop:
echo '</table>';

---
Steve

 

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

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