CodeIgniter Forums
foreach statement is giving syntax error - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: foreach statement is giving syntax error (/showthread.php?tid=65861)



foreach statement is giving syntax error - dilipan - 08-01-2016

<?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


RE: foreach statement is giving syntax error - waptik - 08-01-2016

try this:

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


RE: foreach statement is giving syntax error - dilipan - 08-01-2016

(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.


RE: foreach statement is giving syntax error - InsiteFX - 08-02-2016

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



RE: foreach statement is giving syntax error - rtenny - 08-02-2016

(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; 
       }
?>


RE: foreach statement is giving syntax error - waptik - 08-02-2016

(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.