Welcome Guest, Not a member yet? Register   Sign In
problem with foreach()
#1

[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>
<head>
<title><?=$title?></title>
</head>
<body>
<?php foreach($result as $key=> $row);?>
<h3>&lt;?=$row->title ?&gt;</h3>
<p>&lt;?=$row->text ?&gt;</p>
<br/>
&lt;?php endforeach;?&gt;
&lt;/body&gt;
&lt;/html&gt;

and it's showing error like

Parse error: parse error in C:\xampp\htdocs\hello\system\application\views\helloworld_view.php on line 10
#2

[eluser]jedd[/eluser]
Quote:
Code:
&lt;?php foreach($result as $key=> $row);?&gt;

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.
#3

[eluser]Mike Ryan[/eluser]
Like jedd says, there is a format for mixing control loops and HTML. You should be using a colon (Smile instead of a semi-colon (Wink. Example:
Code:
&lt;?php foreach($result as $key=> $row):?&gt;
.

However, I tend to use the same method as jedd - stay in the PHP context and echo the HTML:

Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?=$title?&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php foreach($result as $key=> $row)
{

echo '<h3>' . $row->title . '</h3>
      <p>' . $row->text . '</p>
      <br/>' ;
}
?&gt;
&lt;/body&gt;
&lt;/html&gt;

EDIT: Clearly not fully awake yet :-)
#4

[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.
#5

[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.
#6

[eluser]Choo[/eluser]
Thanks. And I can say that I prefer inline method for the same two reasons...




Theme © iAndrew 2016 - Forum software by © MyBB