Search code examples
phparraysmysqliarray-push

Array push to a key giving null value in php


So I am taking some input from json that is the userid.

Then I am trying to find its friends and their blogs and its respective comments.

Obviously the comments will be more than one. So whenever I am doing a array push to those comments to a key in array I get null value in my api response.

I tried to use simple $name['comments'] = $res. but as there will be more than one comments so it replaces the new comment with older one.

please help me solving these problem

CoDE->

   $userdata = json_decode(file_get_contents('php://input'));
    $userid = mysqli_real_escape_string($conn, $userdata->userid);
    // =============================================================================
    // ===========================Get Friends=======================================
    // =============================================================================

    $friends = "    SELECT  `friend_one`
                FROM    `friends`
                WHERE   `friend_two` = '$userid' AND `status` = '1'
                UNION ALL
                SELECT  `friend_two`
                FROM    `friends`
                WHERE   `friend_one` = '$userid' AND `status` = '1'
            ";

    $response = array();
    $friendsq = mysqli_query($conn, $friends);
    if(!$friendsq) {
        $response['statusCode'] = 400;
        $response['message'] = "failed to connect to backend. please contact developer";
    }
    $count = mysqli_num_rows($friendsq);
    // =============================================================================
    // ===========================No Friends=======================================
    // =============================================================================
    if($count == 0) {
        $response['statusCode'] = 202;
        $response['message'] = "No Friends to show the newsfeed";
    } else {
        $response['statusCode'] = 200;
        $response['message'] = "Success";
        $response['microblogs'] = array();
        $name['comments'] = array();
        // =============================================================================
        // ===========================show blogs of Friends=============================
        // =============================================================================

        while($friendlist = mysqli_fetch_assoc($friendsq)) {
            $friends = $friendlist['friend_one'];
            $getblog = "SELECT DISTINCT t1.*,t2.username,t2.profile_pic1 FROM microblogs AS t1 INNER JOIN users AS t2 ON t1.user_id = t2.user_id WHERE (t2.user_id = '$friends') ORDER BY t1.id DESC";
            // $getblog = "SELECT * FROM `microblogs` WHERE `user_id` = '$friends' OR `user_id` = '$userid' ORDER BY `id` DESC";
            $getblogq = mysqli_query($conn, $getblog);

            while($name = mysqli_fetch_assoc($getblogq)) {
                $blgid = $name['id'];
        // =============================================================================
        // ===========================show comments=============================
        // =============================================================================
                $getcomment = "SELECT t1.*, t2.username, t2.profile_pic1 FROM `comments` AS t1 INNER JOIN `users` AS t2 ON t1.user_id = t2.user_id INNER JOIN `microblogs` AS t3 ON t3.id = t1.microblog_id WHERE t3.id ='$blgid'";

                $getcommentq = mysqli_query($db, $getcomment);
                if($getcommentq) {
                    $count = mysqli_num_rows($getcommentq);
                    if($count >= 1){
                        while($res = mysqli_fetch_assoc($getcommentq)) {
                            // echo "found";

                            array_push($name['comments'], $res); //this gives null
                        }

                    } else {
                        // echo "not found";
                        array_push($name['comments'], "");
                    }

                }
                http_response_code(200);
                array_push($response['microblogs'], $name);
            }
        }
    }

response->
{
    "statusCode": 200,
    "message": "Success",
    "microblogs": [
        {
            "id": "23",
            "user_id": "40",
            "blogname": "djd",
            "location": "bhubaneswar",
            "image": "https://www.gvitechnology.com/gypsi/api/uploads/blogimg/722e99a7b1a21a0074ac6015912c25ed.jpeg",
            "data": "jjncdn",
            "Likes": "0",
            "username": "satya",
            "profile_pic1": "https://www.gvitechnology.com/gypsi/api/uploads/2d39859f7165ebc57bf02e613f435395.jpeg",
            "comments": null
        },
        {
            "id": "7",
            "user_id": "12",
            "blogname": "test",
            "location": "cuttack",
            "image": "https://www.gvitechnology.com/gypsi/api/uploads/blogimg1.jpg",
            "data": "This is a nice place.i like it here.sgdyusdhsj",
            "Likes": "4",
            "username": "subha",
            "profile_pic1": "5e102c72eb6d2.",
            "comments": null
        }
]

}


Solution

  • Instead of array_push($name['comments'], $res);, use $name['comments'][] = $res;. This is documented right at the top of the manual for array_push.