Forum Posting Basics

Strip out PHP (and other backend code) when asking front end questions!

If you need help manipulating or styling the HTML generated by a PHP script, for example, include the HTML itself, not the PHP that generates it.

E.g. If you want help attaching behaviour to any blockquote elements with a class of “current”, don’t post this:

<?php
  $pdo = new PDO('mysql:host=localhost;dbname=test', 'user', 'password');
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $pdo->exec('SET NAMES "utf8"');
  $sql = 'SELECT joketext, jokedate FROM joke';
  $result = $pdo->query($sql);
  
  foreach ($result as $row){
    $jokes[] = array(
      'text' => $row['joketext'],
      'date' => $row['jokedate']
    );
  }
?>

<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>My Jokes</title>
  </head>
  
  <body>
    <p>Here are all the jokes in the database:</p>
    <?php foreach ($jokes as $joke): ?>
      <blockquote <?php if(preg_match("/2013/", $joke['date'])){ echo('class="current"'); }?>>
        <p><?php echo htmlspecialchars($joke['text'], ENT_QUOTES, 'UTF-8'); ?></p>
      </blockquote>
    <?php endforeach; ?>
    </div>
  </body>
</html>

Post this:

<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>My Jokes</title>
  </head>

  <body>
    <p>Here are all the jokes in the database:</p>
    <blockquote >
      <p>Why did the chicken cross the road? To get to the other side!</p>
    </blockquote>
    <blockquote >
      <p>What has four wheels and flies? A garbage truck.</p>
    </blockquote>
    <blockquote class="current">
      <p>Why did the man at the orange juice factory lose his job? He couldn't concentrate!</p>
    </blockquote>
  </body>
</html>

You can get the HTML generated by a PHP script by opening the page in your web browser, right clicking on the page and selecting “View source” (or similar). That will show you the HTML code that is generated on the server and sent to your browser. Copy this source code and post that here.

1 Like