0

In a project, I have to return user_id, user_age from the database and the return format should be like

  • user object which contains user_id and user_age
  • average age of users
  • count of users

the return format should be in JSON format. I have created user array and encoded to JSON by using the method

json_encode(user);

my code is like this :

 while ($row = mysql_fetch_array($result)) {

        $user["id"]                 = $row["id"];
        $user["name"]               = ucfirst($row["user_name"]);
        $user["date"]               = $row["date_of_treatment"];
        $user["age"]                = $row["age_of_user"];

        // push single user into final response array
        array_push($response, $user);

        $count = $count+1;
        $sum_of_age = $sum_of_age+$row["age_of_user"];

    }

echo json_encode($response);

I have calculated the average age ($sum_of_age/$count) and count of returned users ($count), but I don't know how to return average age and count of users with the same json response.any help will be appreciated.

5
  • 1
    Show your code, including the source of your data. We can't possibly help if we don't know how you are preparing the variable user (which, by the way, should be $user) Commented Mar 22, 2014 at 14:07
  • @SameeraChathuranga code is edited, hope this is ok now Commented Mar 22, 2014 at 14:09
  • 1
    @ShajeerAhmd: Try this: pastie.org/8959015 Commented Mar 22, 2014 at 14:15
  • don't use mysql_. It's deprecated and it doesn't support prepared statements, whose purpose is to avoid SQL injection attacks. Use mysqli_ or PDO. Speaking of which, show your SQL code ;-) Commented Mar 22, 2014 at 14:15
  • @JanDvorak I will consider it. Commented Mar 22, 2014 at 14:16

3 Answers 3

3

You can do like this:

$count=0;
$sum_of_age=0;
$response=array();
$response['users']=array();


while ($row = mysql_fetch_array($result)) {

    $user["id"]                 = $row["id"];
    $user["name"]               = ucfirst($row["user_name"]);
    $user["date"]               = $row["date_of_treatment"];
    $user["age"]                = $row["age_of_user"];

    // push single user into final response array
    array_push($response['users'], $user);

    $count = $count+1;
    $sum_of_age = $sum_of_age+$row["age_of_user"];

}

$response['count']=$count;
$response['avg']=$sum_of_age/$count;
echo json_encode($response);
Sign up to request clarification or add additional context in comments.

2 Comments

how do i get this in jquery, i mean how to extract the average and count in jquery ?
@Patrik please try to answer this question stackoverflow.com/questions/22578968/…
2

You can try this:

$users = array();
$sum_of_age = 0;
$count = 0;
$users = array();
while ($row = mysql_fetch_array($result)) 
{
    $user["id"] = $row["id"];
    $user["name"] = ucfirst($row["user_name"]);
    $user["date"] = $row["date_of_treatment"];
    $user["age"] = $row["age_of_user"];

    // push single user into final response array
    $users[] = $user;

    $count++;
    $sum_of_age += (int) $row["age_of_user"];
}

$response = array(
    'users' => $users,
    'averageAge' => $sum_of_age/$count,
    'count' => $count

);

echo json_encode($response);

This should result in the following json response:

{
    "users":[
        { "id" : "1", "name" : "John Doe" , "date" : "2014-03-22 15:20" , "age" : 42 },
        {...},
        ...
    ],
    "averageAge": 42,
    "count": 1337
}

Comments

0
<?php

$response = array();

while ($row = mysql_fetch_array($result)) {

        $user["id"]                 = $row["id"];
        $user["name"]               = ucfirst($row["user_name"]);
        $user["date"]               = $row["date_of_treatment"];
        $user["age"]                = $row["age_of_user"];

        // push single user into final response array
        array_push($response, $user);

        $count = $count+1;
        $sum_of_age = $sum_of_age+$row["age_of_user"];

    }

$response["average_age"]  = $sum_of_age / $count;
$response["count"] = $count;

echo json_encode($response);

}

This is the answer, thanks @Amal Murali (commented a link), the link you have provided is working.. :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.