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:
$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();
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'];
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:
<?php foreach ($jokes as $joke): ?> <!-- a reference to the $jokes array in the controller -->
<?php echo htmlspecialchars($joke['jokedate'} . $joke['joketitle'] . $joke['joketext'], ENT_QUOTES, 'UTF-8'); ?><!--echo each joke title and text -->
<?php endforeach; ?>
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
Hope someone can help