PHP Build a Basic PHP Website (2018) Listing and Sorting Inventory Items Displaying All Items

There is a cleaner way to generate the html in PHP?

just wondering if there is a cleaner way to conectinate this:

foreach($catalog as $item) {
                echo "<li><a href='#'><img src='" 
                    . $item["img"] . "' alt='" 
                    . $item["title"] . "' />" 
                    . "<p>View Details</p>"
                    . "</a></li>";


2 Answers

Vince Brown
Vince Brown
16,249 Points

Hey Noob, you can just close the foreach drop your html in the middle, then reopen and echo out values as needed

<?php foreach ( $catalog as $item ) : ?>
        <a href='#'>
            <img src="<?php echo $item['img']; ?>" alt="<?php echo $item['title']; ?>" />
            <p>View Details</p>
<?php endforeach; ?>

when i try to do the following the books are not displaying like they need to be, another 3 books are added below them:

and the original:

there are 3 books in each row and in the previous code each row contain 4