Developer Jay
Developer Jay

Reputation: 1363

Div tags in a foreach loop in PHP

I'm wondering what is the best practice to embed tags in a foreach loop?

I have an associative array which has one entry with the following keys:

$portfolio = [
'title' => '',
'technology' => '',
'description' => '',
'link' => '',
];

What I am trying to do is echo the div tag with the specific elements inside, but each div tag is then duplicated:

<div class="container">
<div class="row">
<div class="col-md-6 col-lg-4"><div class="card">\n<div class="car img 
top"></div>
</div>
</div><div class="col-md-6 col-lg-4"><div class="card"><div class="car 
img top"></div>
</div>
</div><div class="col-md-6 col-lg-4"><div class="card"><div class="car 
img top"></div>
</div>
</div><div class="col-md-6 col-lg-4"><div class="card"><div class="car 
img top"></div>
</div>
</div>  </div><!-- row end -->
</div><!-- container end -->

My foreach loop is: I have commented some out, as I'm in the process of debugging as when the page is loaded a white page appears, which the error is within the commented out code, which I will fix.

foreach($portfolio as $value){

echo '<div class="col-md-6 col-lg-4">' . '\n';
    echo '<div class="card">' . '\n';
        echo '<div class="car img top">';
            //echo "<img src='img/" . "$value['image']'" . ">";
        //echo "</div>";
        //echo "<div class='card-body'>";
        //  echo "<div class='card-body'>";
            //  echo "<h3 class'card-title'><a class='text-secondary' href='#'>See Project</a></h3>";
            //  echo "<h6 class='card-subtitle mb-2 text-muted'>$value['description']</h6>";

        echo "</div>". "\n";
    echo "</div>" . "\n";   
echo "</div>";
}

Any help would be appreciated, but I think my best aim is to do a multidimensional array?

Thanks, James

Upvotes: 2

Views: 4447

Answers (1)

Ibu
Ibu

Reputation: 43810

If your problem is only a formatting and legibility one, try to use this syntax instead.

<?php foreach($portfolio as $value):?>
<div class="col-md-6 col-lg-4">
    <div class="card">
        <div class="car img top">
            <img src="img/<?php echo $value['image'];?>" />
        </div>";
        <div class="card-body">
            <div class="card-body">
                <h3 class='card-title'>
                    <a class='text-secondary' href='<?php echo $value['link']?>'>
                       <?php echo $value['title']?>
                    </a>
                </h3>
                <h6 class='card-subtitle mb-2 text-muted'><?php echo $value['description']?></h6>
            </div>
        </div>  
    </div>
</div>
<?php endforeach;?>

Notice that you had mistakes on your quotes and you had forgotten the closing div tag. This format makes it easier to read and catch mistakes.

Note in my example, I assume that your $portfolio variable is a multi-dimensional array as such:

$portfolio = [
    [
      'title' => '',
      'technology' => '',
      'description' => '',
      'link' => ''
    ],
    [
      'title' => '',
      'technology' => '',
      'description' => '',
      'link' => ''
    ],
    // etc
];

Upvotes: 1

Related Questions