Question

The cms won't load, it will only show a blanc page under the name of index.php. It worked fine before I added the foreach loop. I looked up on the internet and I couldn't find anything wrong with the foreach loop. It suddenly didn't work anymore. Help? :-) Below here are the files I am currently using.

INDEX.PHP

  include_once('includes/connection.php');
  include_once('includes/article.php');

  $article = New Article;
  $articles = $article->fetch_all();

  print_r($articles);
?>

  <html>
    <head>
      <title>Visuality dashboard</title>
      <link rel="stylesheet" type="text/css" href="assets/stylesheet.css">
    </head>

    <body>
      <div class="container">
        <a href="index.php" id="logo">CMS</a>
       <ol>
          <?php foreach ($articles as $article) { ?>
            <li>
              <a href="article.php?id=<?php echo $article ['article_id']; ?>">
                <?php echo $article['article_title']; ?>
              </a>
              - <small>
                posted <?php echo date('l jS, $article['article_timestamp']');?>
              </small>
            </li>

          <?php } ?>
      </ol>
    </div>
    </body>

    <footer>
    </footer>
  </html>

ARTICLE.PHP

    <?php

  class Article {
    public function fetch_all() {
      global $pdo;

      $query = $pdo->prepare("SELECT * FROM articles");
      $query->execute();

      return $query->fetchAll();
  }
}

?>

CONNECT.PHP

<?php

  try {
  $pdo = new PDO('mysql:host=localhost;dbname=cms', 'root', 'root');
} catch (PDOException $e) {
  exit('Database error.');
}

?>
Was it helpful?

Solution

Change following:

<a href="article.php?id=<?php echo $article ['article_id']; ?>">

to

<a href="article.php?id=<?php echo $article['article_id']; ?>">

and

posted <?php echo date('l jS, $article['article_timestamp']');?>

to

posted <?php echo date('l jS', $article['article_timestamp']);?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top