0

I have the following function.php:

function getPublishedPosts() {
    // use global $conn object in function
    global $conn;
    $sql = "SELECT * FROM posts WHERE published=true";
    $result = mysqli_query($conn, $sql);
    // fetch all posts as an associative array called $posts
    $posts = mysqli_fetch_all($result, MYSQLI_ASSOC);

    $final_posts = array();
    foreach ($posts as $post) {
        $post['topic'] = getPostTopic($post['id']); 
        array_push($final_posts, $post);
    }
    return $final_posts;
}

and I am calling it to the other page with this:

<!-- THIS LOOP -->
  <?php $posts = **getPublishedPosts();** ?>
  <div class="content">
  <h2 class="content-title">Recent Articles</h2>
  <hr>
  <!-- more content still to come here ... -->

        <!-- Add this ... -->
      <?php $posts = getPublishedPosts(); ?>
      <?php foreach ($posts as $post): ?>

      <div class="post" style="margin-left: 0px;">
        <img src="<?php echo BASE_URL . '/static/images/' . $post['image']; ?>" class="post_image" alt="">
            <!-- Added this if statement... -->
        <?php if (isset($post['topic']['name'])): ?>
          <a 
            href="<?php echo BASE_URL . 'filtered_posts.php?topic=' . $post['topic']['id'] ?>"
            class="btn category">
            <?php echo $post['topic']['name'] ?>
          </a>
        <?php endif ?>

        <a href="single_post.php?post-slug=<?php echo $post['slug']; ?>">
          <div class="post_info">
            <h3><?php echo $post['title'] ?></h3>
            <div class="info">
              <span><?php echo date("F j, Y ", strtotime($post["created_at"])); ?></span>
              <span class="read_more">Read more...</span>
            </div>
          </div>
        </a>
      </div>
    <?php endforeach ?>
      </div>

However, when it is on the server, the page goes blank.

Dharman
  • 21,838
  • 18
  • 57
  • 107
GeeYawn
  • 1
  • 1
  • Turn on the error reporting or check the server error logs. There might be some errors. – Ash1271 Jul 16 '20 at 09:35
  • Don't post ALLCAPS, that's considered shouting/rude, I changed that for you. Also [How do I get PHP errors to display?](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display?rq=1) – brombeer Jul 16 '20 at 09:35
  • `` I think this line is causing the error. – Ash1271 Jul 16 '20 at 09:37
  • @kerbh0lz thank you for that, I am pretty new on this platform :D – GeeYawn Jul 16 '20 at 09:38
  • Why are you calling the function twice – RiggsFolly Jul 16 '20 at 09:38
  • @InvalidBot minus all those stars (**) when i try to remove that it will output the page but it won't display the query of the function, welp – GeeYawn Jul 16 '20 at 09:40
  • Does your "other" page include your `function.php` somewhere? – brombeer Jul 16 '20 at 09:40
  • 1
    To get errors out of PHP even in a LIVE environment add these 4 lines to the top of any `MYSQLI_` based script you want to debug `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);`. This will force any `MYSQLI_` errors to generate an Exception that you can see on the browser as well as normal PHP errors. – RiggsFolly Jul 16 '20 at 09:41
  • @InvalidBot yes i have this on top – GeeYawn Jul 16 '20 at 09:41
  • @RiggsFolly thanks i will try this one – GeeYawn Jul 16 '20 at 09:42
  • Does this answer your question? [How do I get PHP errors to display?](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – Dharman Jul 16 '20 at 10:10
  • @Dharman unfortunately it doesn't – GeeYawn Jul 16 '20 at 10:15

1 Answers1

0

I finally solved it, it was just a server extension and version problem regarding the incompatibility of my function.

GeeYawn
  • 1
  • 1