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

<noob />
<noob />
16,678 Points

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

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

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

thanks!

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 ) : ?>
    <li>
        <a href='#'>
            <img src="<?php echo $item['img']; ?>" alt="<?php echo $item['title']; ?>" />
            <p>View Details</p>
        </a>
    </li>
<?php endforeach; ?>
<noob />
<noob />
16,678 Points

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

https://gyazo.com/493269ccf62127cf7072bc5f3b11ff0d

and the original: https://gyazo.com/204332711d608e35aa26ac86f465630a

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