0

This script errors and I can't find where, I know it's in the $homeRandom section yet I can't seem to work out what it is.

Is there a good way of printing large amounts of HTML in PHP?

$sqla = "SELECT * FROM Products";
 $resulta = $conn->query($sqla);

 if ($resulta->num_rows > 0) {

     $homePage = '';

     while($rowa = $resulta->fetch_assoc()) {
         $nav = '<li><a href="'.$rowa['UrlPage'].'">'.$rowa['Name'].'</a></li>';
         $homeRandom = '
         <div class="col-sm-4 col-lg-4 col-md-4">
       <div class="thumbnail">
           <img src="./content/images/homepage/'.$rowa['ImageName'].'.jpg" alt="">
           <div class="caption">
               <h4 class="pull-right">'.$rowa['Price'].'</h4>
               <h4><a href="'.$rowa['UrlPage'].'">'.$rowa['Name'].'</a>
               </h4>
               <p>'.$rowa['Description'].'</p>
           </div>
       </div>
   </div>'

         $homePage = $homePage . $homeRandom; 
     }

2 Answers2

0

Is there a good way of printing large amounts of HTML in PHP?

Yes there is, you can make use of opening and closing php tags and improve readability by using the alternative syntax for control structures:

<?php if (condition) : ?>

<!-- HTML goes here -->

<?php endif; ?>

Or simply with a normal if statement:

<?php if (condition) { ?>

<!-- HTML goes here -->

<?php } ?>

Or the heredoc method:

echo <<<EOT
<p>My name is "$name". I am printing some $foo->foo.</p>
<p>Now, I am printing some {$foo->bar[1]}.</p>
<p>This should print a capital 'A': \x41</p>
EOT;
cnorthfield
  • 3,384
  • 15
  • 22
0

You have a missing semi colon, and seemingly a missing brace, but that could just be the way you've copied and pasted.

$sqla = "SELECT * FROM Products";
    $resulta = $conn->query($sqla);

    if ($resulta->num_rows > 0) {

        $homePage = '';

        while($rowa = $resulta->fetch_assoc()) {
            $nav = '<li><a href="'.$rowa['UrlPage'].'">'.$rowa['Name'].'</a></li>';
            $homeRandom = '
            <div class="col-sm-4 col-lg-4 col-md-4">
                <div class="thumbnail">
                    <img src="./content/images/homepage/'.$rowa['ImageName'].'.jpg" alt="">
                    <div class="caption">
                        <h4 class="pull-right">'.$rowa['Price'].'</h4>
                        <h4><a href="'.$rowa['UrlPage'].'">'.$rowa['Name'].'</a>
                        </h4>
                        <p>'.$rowa['Description'].'</p>
                    </div>
                </div>
            </div>'; //<-- missing in yours

            $homePage = $homePage . $homeRandom; 
        }
} //<-- also missing in yours, but could just be a copy 
  //paste missed the last }

Additionally, you should start reading the error log, as it'll tell you good info which you can use to identify where the issue is. ie "unexpected '$homePage' (T_VARIABLE)" Something unexpected usually means it was expecting something before it.

You should also use a good IDE, Netbeans, PHPStorm etc, as they return all sorts of info about your code, such as underlined for errors etc

James
  • 4,644
  • 5
  • 37
  • 48