Format HTML when it's echoed through PHP

This might be a weird question, but is there a way to format my HTML code when it’s echoed through PHP? I’m tired of my dynamic HTML looking different from the static. For instance, my website source looks like this:

<div class="static">
  <p>Static // Static // Static</p>
</div>

      <div class="dynamic">
         <p>Dynamic // Dynamic // Dynamic</p>
      </div>

Is there a PHP library that can help with this?

Do you have to echo HTML through PHP?

Yes. I’m echoing divs.

You are partially on the right track.

You would set variables and have those variables print in pages. OR, even better, use a templating engine to handle this for you.

I may not be able to help, but how are you echoing your divs with PHP currently?

More information, and detail on what you have tried, your expected output to current output, and exact goal may help others with helping you with a solution.

Yes, I am echoing through PHP. I mean, this is a PHP forum.

I’m sorry. Noob question here, what’s a templating engine, and which one do you reccommend

Hello @WillWam,
Are you trying to echo both divs using PHP exactly the way you mentioned in your HTML code?

No. Here’s a snippet (all the PHP on my page so far).

$stmt = "SELECT * FROM `posts` WHERE `hp`='true'";
      $res = $con->query($stmt);
      
      if ($res->num_rows > 0) {
        // output data of each row
        while($row = $res->fetch_assoc()) {
          $redactedHBody = substr($row["bodydata"], 0, 200);
            echo '
          <div class="hp-padding">
            <div class="hot-post">
              <img src="'.$row["image"].'" class="rp-img">
              <div class="hp-ipadding">
                <h1>'.$row["title"].'</h1>
                <h3>Posted '.$row["date"].' '.$row["time"].'</h3>
                <p>'.$redactedHBody.'...</p>
                <h3><i class="fas fa-tags"></i> '.str_replace(',', ', ', $row["tags"]).'</h3>
                <button class="hread r-animate" id="hb"><i class="fas fa-fire-alt"></i> Read More <i class="fas fa-fire-alt"></i></button>
              </div>
            </div>
          </div>
            ';
        }
      } else {
          echo "<h1>No posts</h1>";
      }
      ?>
      <div class="p-padding">
        <?php
        $stmt = "SELECT * FROM `posts` WHERE `hp`=''";
        $res = $con->query($stmt);

        if ($res->num_rows > 0) {
          // output data of each row
          while($row = $res->fetch_assoc()) {
              $redactedBody = substr($row["bodydata"], 0, 200);
              echo '
              <div class="p-padding">
                <div class="post">
                  <img src="'.$row["image"].'" class="rp-img">
                  <div class="p-ipadding">
                    <h1>'.$row["title"].'</h1>
                    <h3>Posted '.$row["date"].' '.$row["time"].'</h3>
                    <p>>'.$redactedBody.'...</p>
                    <button class="read">Read More</button>
                    <h3><i class="fas fa-tags"></i> '.str_replace(',', ', ', $row["tags"]).'</h3>
                  </div>
                </div>
              </div>
              ';
          }
        } else {
            
        }

https://www.smarty.net/crash_course

Understandable, as a beginner you don’t know the difference. But, it is generally a better idea to separate everything out for a cleaner more maintainable project. Think in terms of HTML, while you can have everything in a single file, it is preferred to have links to the css and javascript in the html file for separation.

Sponsor our Newsletter | Privacy Policy | Terms of Service