Echo Joke Title and Joke Text

Hi there

I am following along with the PHP Novice to Ninja book, and I am adapting the project to a blog application. So instead of just submitting joketext, I have successfully added additional code and a table in the MySQL database to accept a joketitle too. I have also created a new directory to list the jokes with an index.php which includes a listjokes.html.php where (like a blog) I list all the entries. The code that pulls the date, title and text looks like this:

try
{
  $sql = 'SELECT jokedate, joketitle, joketext FROM joke';
  $result = $pdo->query($sql); //use the $pdo to connect to the database and pass in the sql query.
}
catch (PDOException $e)
{
  $error = 'Error fetching jokes: ' . $e->getMessage();
  include 'error.html.php';
  exit();
}

while ($row = $result->fetch()) //we are using the fetch method of the PDO to return the rows as an array.  The loop will return all the joke titles and will stop when condition is false
{
  $jokes[] = $row['jokedate'] . $row['joketitle'] . $row['joketext'];
}

include 'listjokes.html.php';


However I am unsure how to display the date <br> title(in h2) another <br> then the text and then a line to separate each entry?

I need to echo the date, then the title and then the joketext and this is what I’ve got so far:

<body>
     <?php foreach ($jokes as $joke): ?> <!-- a reference to the $jokes[] array in the controller -->
      <blockquote>
        <p>
          <?php echo htmlspecialchars($joke['jokedate'} . $joke['joketitle'] . $joke['joketext'], ENT_QUOTES, 'UTF-8'); ?><!--echo each joke title and text  -->
        </p>
      </blockquote>
    <?php endforeach; ?>
  </body>

I do understand that for specific formatting I will need to apply CSS classes etc to make it prettier, but I think the layout of the data is the first things :slight_smile:

Hope someone can help
Cheers
Volterony

You can concatenate literal strings with variables.


<?php echo $joke['jokedate'] ."<br />".$joke['joketitle'] ."<br />". $joke['joketext']; ?>

Also you have a typo on the jokedate variable’s closing bracket.

Thanks for replying StarLion. I’ve tried your suggestion:

 <?php foreach ($jokes as $joke): ?> <!-- a reference to the $jokes[] array in the controller -->
      <blockquote>
        <p>
          <?php
			echo $joke['jokedate'] . "<br>" . $joke['joketitle'] . "<br>" .$joke['joketext'];
		  ?><!--echo each joke title in the database -->
        </p>
      </blockquote>
    <?php endforeach; ?>

However I get and illegal string offset warning for all three array items: jokedate, joketitle and joketext.

I read that I need to check that $joke is an array to prevent this errror, so do precede the block with:

 if (is_array($joke)) ........
<?php foreach ($jokes as $joke): ?>  etc etc


Cheers
volterony

Well the problem there is your construction of your jokes array.


while ($row = $result->fetch()) 
{ 
  $jokes[] = $row['jokedate'] . $row['joketitle'] . $row['joketext']; 
} 

This creates a string out of the jokes. I -think- what you’re trying to do here instead is make an array out of the joke:


while ($row = $result->fetch()) 
{ 
  $jokes[] = array('jokedate' => $row['jokedate'], 'joketitle' => $row['joketitle'], 'joketext' => $row['joketext']); 
} 

Which will give you a multidimensional $jokes array that you can then navigate with a foreach as you are trying to do.

StarLion, yes I now see what I’ve done there. Thanks for noticing mistake.

It displays correctly now.

Many thanks
Volterony