1

I have a function in my Model which is passed a single value and in which I have a query which is giving me two results.

 public static function updImgtbl($id)
{
    $total_images = 0;
    $images_uploaded = 0;

    $sqlquery = "SELECT  SUM(CASE WHEN ims.`installation_id` = $id THEN 1 ELSE 0 END) AS total_images, 
    SUM(CASE WHEN ims.`image_upload_flag` = 1 AND ims.`installation_id` = $id THEN 1 ELSE 0 END) AS images_uploaded
    FROM    `installation_images_site` ims
    INNER JOIN `installations` ins ON ins.`id` = ims.`installation_id`
    WHERE   (ims.`image_upload_flag` = 1 AND ims.`installation_id` = $id)
    OR (ims.`installation_id` = $id) 
    GROUP BY ims.`installation_id`";

    $pt = Yii::$app->db->createCommand($sqlquery)->queryAll();

    foreach ($pt as $val)
    {
        $total_images= $val['total_images'];
        $images_uploaded = $val['images_uploaded'];
    }
    return ["total_images" => $total_images, "images_uploaded"=>$images_uploaded];
}

This function is called from an API

 $response = Installations::updImgtbl($install_id);
        print_r($response);
        exit();

The response is below

Array
(
  [total_images] => 4
  [images_uploaded] => 1
)

But I want to get these values in a variable in my API. How can I achieve it?

1 Answer 1

2

You can simply:

list($total_images, $images_uploaded) = array_values($response);

Then, you have total_images value in $total_images variable and images_uploaded in $images_uploaded variable.

I mean:

list($total_images, $images_uploaded) = Installations::updImgtbl($install_id);
echo $total_images; // 4
echo $images_uploaded; // 1

You can read more about list in official doc.

Another way is using extract() function. For example:

$response = Installations::updImgtbl($install_id);
extract($response);   
echo $total_images; // 4
echo $images_uploaded; // 1
Sign up to request clarification or add additional context in comments.

1 Comment

` list('total_images'=>$total_images, 'images_uploaded'=>$images_uploaded) = Installations::updImgtbl($install_id); print_r($total_images); print_r($images_uploaded); exit();` it works for me :)

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.