Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Newbie to PHP.

Trying to output the results of an array inside a foreach loop. I have got the following code but I cannot understand what to do with it next.

Mainly;

  • Where/how do I start and end the php statements?
  • Is there a better way to format this code?

I want to make it as easy to understand and legible as possible.

I'm not sure how to echo my rows within the specified html below?

My code so far;

<?php foreach ($userResults as $row) : ?>
<div class="row">
        <ul class="list-group">
            <li class="list-group-item">echo $row['user_id'] here</li>
            <li class="list-group-item">echo $row['user_name'] here</li>
            <li class="list-group-item">echo $row['user_email'] here</li>
        </ul>
    </div>
<?php endforeach; ?>

Any help or advice is appreciated.

share|improve this question
    
When you do ?> you jump out of PHP, so your echo $row[...] stuff is printing out literally, I'm guessing? You need to re-enter PHP or never leave it, if I'm understanding correctly. Technically, this is broken code, so your question might get closed. – Barry Carter Mar 9 at 18:21
up vote 2 down vote accepted

I dont think mixing html inside PHP the way Tim Penner does in his first example is very readable or maintainable, i'd prefer the way you're trying to do it.

If you're using PHP 5.4+ and do not need the code to be working in earlier php versions i'd recommend you use the short_open_tag <?= to echo out variables easily.

<?php foreach ($userResults as $row) : ?>
<div class="row">
        <ul class="list-group">
            <li class="list-group-item"><?= $row['user_id']; ?>here</li>
            <li class="list-group-item"><?= $row['user_name']; ?>here</li>
            <li class="list-group-item"><?= $row['user_email']; ?>here</li>
        </ul>
</div>
<?php endforeach; ?>

This is in my mind more readable and maintainable.

share|improve this answer
    
I like this approach too. Is the only difference here the omitted 'php' and 'echo' ? No performance issues? I wasn't aware of these short tags. Thanks! – johnny_s Mar 9 at 19:23
    
No , the performance difference is insignificant – JazzCat Mar 9 at 19:25

Where/how do I start and end the php statements?

PHP statements begin with <?php and end with ?>

Is there a better way to format this code?

Using the original syntax for control structures the code would be written more like this:

<?php
foreach ($userResults as $row){
    echo '<div class="row">';
    echo '  <ul class="list-group">';
    echo '      <li class="list-group-item">'.$row['user_id'].' here</li>';
    echo '      <li class="list-group-item">'.$row['user_name'].' here</li>';
    echo '      <li class="list-group-item">'.$row['user_email'].' here</li>';
    echo '  </ul>';
    echo '</div>';
}
?>

Using alternative syntax for control structures it could also be written like this:

<?php foreach ($userResults as $row) : ?>
<div class="row">
    <ul class="list-group">
        <li class="list-group-item"><?php echo $row['user_id'] ?> here</li>
        <li class="list-group-item"><?php echo $row['user_name'] ?> here</li>
        <li class="list-group-item"><?php echo $row['user_email'] ?> here</li>
    </ul>
</div>
<?php endforeach; ?>
share|improve this answer
1  
i updated my answer giving a working example of the alternative syntax. – Tim Penner Mar 9 at 19:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.