problem with foreach() - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: problem with foreach() (/showthread.php?tid=17673) |
problem with foreach() - El Forum - 04-12-2009 [eluser]Unknown[/eluser] hi! i am new to codeigniter.in my first application i am facing problem using foreach function in my viw page. my code r below. Code: <html> and it's showing error like Parse error: parse error in C:\xampp\htdocs\hello\system\application\views\helloworld_view.php on line 10 problem with foreach() - El Forum - 04-12-2009 [eluser]jedd[/eluser] Quote: It is almost definitely not being helped by the semicolon at the end of your foreach Personally I like to wrap all this stuff up in echos rather than dip in between PHP and HTML like this - I find it easier to read (but I recognise I'm in the majority). I think there's a format that involves more colons ( : ) which better facilitates the mixing of PHP control loops and real HTML like this, too. problem with foreach() - El Forum - 04-12-2009 [eluser]Mike Ryan[/eluser] Like jedd says, there is a format for mixing control loops and HTML. You should be using a colon ( instead of a semi-colon (. Example: Code: <?php foreach($result as $key=> $row):?> However, I tend to use the same method as jedd - stay in the PHP context and echo the HTML: Code: <html> EDIT: Clearly not fully awake yet :-) problem with foreach() - El Forum - 04-12-2009 [eluser]Choo[/eluser] And why? Speed? One echo instead of several? OK, it's true, but as for me it's really uncomfortable to write long HTML fragments such way and I prefer mixing. problem with foreach() - El Forum - 04-12-2009 [eluser]jedd[/eluser] [quote author="Choo" date="1239557874"]And why? Speed? One echo instead of several? OK, it's true, but as for me it's really uncomfortable to write long HTML fragments such way and I prefer mixing.[/quote] There's an article [url="http://www.e-gineer.com/v1/articles/php-hackers-paradise.htm"]at hacker's paradise[/url] that concludes there may be a benefit from inlining your HTML, but then goes on to say that it's a trivial performance benefit. You'd have to read up on the PHP method - I forget the syntax, and because it's denoted by a : it's a bit tricky to google for. But really, to answer your question - I prefer to have blocks of echos in my foreach statments in preference to raw HTML for two reasons. First, as I mentioned, I find it more readable. I actually meant to say that I'm in the minority on that one, but nonetheless... Second, and quite relevant here, I find I make less mistakes - and those I do are easier to spot - than when I do what you were doing when you got caught out with the above PHP syntax problem. Your call. problem with foreach() - El Forum - 04-12-2009 [eluser]Choo[/eluser] Thanks. And I can say that I prefer inline method for the same two reasons... |