• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
foreach statement is giving syntax error

#1
<?php foreach ($records as $rows);?>
      <?php echo $rows->firstname; ?>
<?php endforeach; ?>
                    When I put the above code in view i'm getting syntax error in the last line.  But syntax looks to be fine.  Anyone can help me why i'm  getting Severity: Parsing Error

Message: syntax error, unexpected 'endforeach' (T_ENDFOREACH)

Filename: views/site_view.php
Reply

#2
try this:

<?php foreach ($records as $rows):?>
<?php echo $rows->firstname; ?>
<?php endforeach; ?>
Be Simple Angel
Reply

#3
(08-01-2016, 09:58 PM)waptik Wrote: try this:

<?php foreach ($records as $rows):?>
     <?php echo $rows->firstname; ?>
<?php endforeach; ?>

waptik  It did work well. I learnrd, that i did a syntax error by putting ; instead of : at the end of the foreach statement. Appreciate it and God Bless.
Reply

#4
PHP Code:
<?php
    foreach 
($records as $rows):
        echo 
$rows->firstname;
    endforeach;
?>
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#5
(08-01-2016, 08:55 PM)dilipan Wrote: <?php foreach ($records as $rows);?>
      <?php echo $rows->firstname; ?>
<?php endforeach; ?>
                    When I put the above code in view i'm getting syntax error in the last line.  But syntax looks to be fine.  Anyone can help me why i'm  getting Severity: Parsing Error

Message: syntax error, unexpected 'endforeach' (T_ENDFOREACH)

Filename: views/site_view.php

You need the : at the end of the foreach () line you have the ; there.
This creates the error as the endforeach does not have a opening line

I personally dont use endif, endforech, etc. I prefer the use of { } that way the editor can easily spotting missing { }
<?php
       foreach ($records as $rows) {
         echo $rows->firstname; 
       }
?>
On the package it said needs Windows 7 or better. So I installed Linux.
Reply

#6
(08-01-2016, 11:44 PM)dilipan Wrote:
(08-01-2016, 09:58 PM)waptik Wrote: try this:

<?php foreach ($records as $rows):?>
      <?php echo $rows->firstname; ?>
<?php endforeach; ?>

waptik  It did work well. I learnrd, that i did a syntax error by putting ; instead of : at the end of the foreach statement. Appreciate it and God Bless.


You are welcome bro. But next time, kindly use this
Code:
foreach(condition){
// what you want to achieve
}
 statement which is pretty clear unlike the one you used which can sometimes be confusing.
Be Simple Angel
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.