0

I am trying to learn php. I wrote code to insert post to database and get posts to display on the home page. I wrote pagination code as well and tried to run it. but as a result php is returning an empty page with no code and no error. Tried every thing. Went to through most of the answers here. I still can figure out the error.

<?php



        $con = mysqli_connect("localhost","root","password","social") or die ("Connection was not established");

        //function for getting topics

            function getTopics() {

            global $con;
            $get_topics = "select * from topics";
            $run_topics = mysqli_query($con,$get_topics);

            while($row=mysqli_fetch_array($run_topics)) {

                $topic_id = $row['topic_id'];
                $topic_title = $row['topic_title'];

                echo "<option value='$topic_id'>$topic_title</option>";

            }
        }

        //function for insert posts to database

        function insertPost() {


            if(isset($_POST['sub'])) {

                global $con;

                $title = $_POST['title'];
                $content = $_POST['content'];
                $topic = $_POST['topic'];

                $user = $_SESSION['user_email'];
                    $get_user = "SELECT * FROM users WHERE user_email = '$user'";
                    $run_user = mysqli_query($con,$get_user);
                    $row=mysqli_fetch_array($run_user);

                    $user_id = $row['user_id'];

                $insert = "INSERT INTO posts (user_id,topic_id,post_title,post_content,post_date) VALUES ('$user_id','$topic','$title','$content',NOW())";
                $run = mysqli_query($con,$insert);

                    if($run) {
                        echo "<h3>Posted to timeline. Looks great.</h3>";

                        $update = "Update users set posts='yes' where user_id='$user_id'";
                        $run_update = mysqli_query($con,$update);
                    }
            }
        }


        //function to get posts

        function get_posts() {

            global $con;

            $per_page=5;

            if (isset($_GET['page'])) {

                $page = $_GET['page'];
            }
            else {
                $page=1;
            }

            $start_from = ($page-1)*$per_page;

            $get_posts = "SELECT * FROM posts ORDER by 1 DESC LIMIT $start_from, $per_page";
            $run_posts = mysqli_query($con,$get_posts);

            while($row_posts=mysqli_fetch_array($run_posts)) {

                $post_id = $row_posts['post_id'];
                $user_id = $row_posts['user_id'];
                $post_title = $row_posts['post_title'];
                $content = $row_posts['post_content'];
                $post_date = $row_posts['post_date'];

                //gettin the user who posted the thread

                $user = "SELECT * FROM users WHERE user_id='$user_id' AND posts='yes'";

                $run_user = mysqli_query($con,$user);
                $row_user = mysqli_fetch_array($run_user);
                $user_name = $row_user['user_name'];
                $user_image = $row_user['user_image'];


                //displaying all at once.
                echo "<div id='posts'>
                        <p><img src='user_images/$user_image' width='50' height='50'</p>
                        <h3><a href='user_profile.php?luser_id=$user_id'>$user_name</h3>
                        <h3>$post_title</h3>
                        <p>$post_date</p>
                        <a href='single.php?post_id=$post_id' style='float:right'><button>See replies</button></a>
                        </div>";

            }
            include("pagination.php");
        }
    ?>

pagination code goes like this.

<?php

$query = "SELECT * FROM posts";
$result = mysqli_query($con,$query);

//count the total records

$total_posts = mysqli_num_rows($result);

//using ceil function to divide the total records per page

$total_pages = ceil($total_posts / $per_page);

//going to first page

echo "
    <center>
        <div id='pagination'>
            <a href='welcome.php?page=1'>First Page</a>

        ";

for ($i=1; $i=$total_pages; $i++) {

    echo "<a href='welcome.php?page=$i'>$i</a>";
}

//going to last page

echo "<a href='welcome.php?page=$total_pages'>Last Page</a></center>";

?>
Himakar
  • 153
  • 10
  • 1
    http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – apokryfos Feb 18 '17 at 07:57
  • add code to display error and share what error you get? – Niklesh Raut Feb 18 '17 at 08:03
  • i copy pasted ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); To the main page where i am calling get_posts() function. Now i am able to get the posts but the page is taking much longer time to load and the pagination only shows 3 3 3 3 3 3 3 3 3 3 3 3 3 3 instead of page numbers – Himakar Feb 18 '17 at 08:11
  • I restarted XAMPP its working now. thank you all. Thank you @apokryfos – Himakar Feb 18 '17 at 08:15

0 Answers0