CodeBlake
CodeBlake

Reputation: 184

Unexpected if(T_IF) parser error

Unexpected if(T_IF) on line 15, I know it is probably something I'm not closing, and maybe I have just been staring at the screen too long but I can't find it.

 <?php

$response=array();


if (!empty($_GET["page"])){
    $Page = $_GET['page'];
    $limit= (int)page*10;

      $db = mysqli_connect('127.0.0.1', 'root', '', 'CampusList') or die(mysqli_error());
// get all products from products table
$result = mysqli_query($db, "SELECT * FROM post order by post_date desc limit $limit") or die(mysqli_error());
 

if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["post"] = array();
 
    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $post = array();
        $post["post_id"] = $row["post_id"];
        $post["post_name"] = $row["post_name"];
        $post["post_creator"] = $row["post_creator"];
        $post["post_type"] = $row["post_type"];
        $post["post_school_id"] = $row["post_school_id"];
        $post["post_content"] = $row["post_content"];
        $post["price"] = $row["price"];
        $post["post_date"] = $row["post_date"];
        $post["post_class_id"] = $row["post_class_id"];
        $post["post_flagged"] = $row["post_flagged"];
 
        // push single product into final response array
        array_push($response["post"], $post);
    }
    // success
    $response["success"] = 1;
 
    // echoing JSON response
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No Posts found";
 
    // echo no users JSON
    echo json_encode($response);
}
}else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Error Please try again";

    // echoing JSON response
    echo json_encode($response);
}
?>

Ths is just a php activated script to return 10 entries from a database, nothing fancy, yes for some reason I am not catching what is causing this error, thank you for the help!

Upvotes: 0

Views: 113

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74219

You're missing the $ in front of page and that should be an upper-case P since you're using it with $Page in $Page = $_GET['page'];

PHP variables are case-sensitive.

You're also mixing MySQL APIs, they don't mix together.

mysql_num_rows should be mysqli_num_rows and mysql_fetch_array should be mysqli_fetch_array

<?php

$response=array();


if (!empty($_GET["page"])){
    $Page = $_GET['page'];
    $limit= (int)$Page*10;

      $db = mysqli_connect('127.0.0.1', 'root', '', 'CampusList') or die(mysqli_error());
// get all products from products table
$result = mysqli_query($db, "SELECT * FROM post order by post_date desc limit $limit") or die(mysqli_error());


if (mysqli_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["post"] = array();

    while ($row = mysqli_fetch_array($result)) {
        // temp user array
        $post = array();
        $post["post_id"] = $row["post_id"];
        $post["post_name"] = $row["post_name"];
        $post["post_creator"] = $row["post_creator"];
        $post["post_type"] = $row["post_type"];
        $post["post_school_id"] = $row["post_school_id"];
        $post["post_content"] = $row["post_content"];
        $post["price"] = $row["price"];
        $post["post_date"] = $row["post_date"];
        $post["post_class_id"] = $row["post_class_id"];
        $post["post_flagged"] = $row["post_flagged"];

        // push single product into final response array
        array_push($response["post"], $post);
    }
    // success
    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No Posts found";

    // echo no users JSON
    echo json_encode($response);
}
}else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Error Please try again";

    // echoing JSON response
    echo json_encode($response);
}
?>

Upvotes: 3

Giacomo1968
Giacomo1968

Reputation: 26074

Not 100% this is the solution, but I reformatted & looked through your code. Here is what I have. First, you have (int)page*10; which contextually makes no sense. So I changed that to (int) $Page*10; but that could probably be just intval($Page*10);. Also you are using mysql_num_rows when it should probably be mysqli_num_rows. So adjusted that as well.

$response=array();


if (!empty($_GET["page"])) {
    $Page = $_GET['page'];
    // $limit= intval($Page*10); // Maybe this would work as well.
    $limit= (int) $Page*10;

    $db = mysqli_connect('127.0.0.1', 'root', '', 'CampusList') or die(mysqli_error());
    // get all products from products table
    $result = mysqli_query($db, "SELECT * FROM post order by post_date desc limit $limit") or die(mysqli_error());


    if (mysqli_num_rows($result) > 0) {
        // looping through all results
        // products node
        $response["post"] = array();

        while ($row = mysql_fetch_array($result)) {
            // temp user array
            $post = array();
            $post["post_id"] = $row["post_id"];
            $post["post_name"] = $row["post_name"];
            $post["post_creator"] = $row["post_creator"];
            $post["post_type"] = $row["post_type"];
            $post["post_school_id"] = $row["post_school_id"];
            $post["post_content"] = $row["post_content"];
            $post["price"] = $row["price"];
            $post["post_date"] = $row["post_date"];
            $post["post_class_id"] = $row["post_class_id"];
            $post["post_flagged"] = $row["post_flagged"];

            // push single product into final response array
            array_push($response["post"], $post);
        }
        // success
        $response["success"] = 1;

        // echoing JSON response
        echo json_encode($response);
    }
    else {
        // no products found
        $response["success"] = 0;
        $response["message"] = "No Posts found";

        // echo no users JSON
        echo json_encode($response);
    }
}
else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Error Please try again";

    // echoing JSON response
    echo json_encode($response);
}

Upvotes: 1

Related Questions