1

I have this query in my view.php

<?php
    $packageq= Yii::$app->db->createCommand
         ("SELECT p.package_name,p.`total_charges` 
          as 'package_charges'
          FROM `package` p, estimate e,patient_detail pd 
          where pd.package=p.id 
          and e.ipd_patient_id=pd.ipd_patient_id 
          and e.id=$model->id");
    $packages= $packageq->queryAll();
    //var_dump($packages);exit;
    echo '<table class="table table-striped table-bordered table-hover">'; 
    echo "<tr><th>Package Name</th><th>Amount</th></tr>"; 
    if (![$packages]==''){
    foreach($packages as $package){
     echo "<tr><td>";  
     echo $package['package_name'];
     echo "</td><td>";  
     $d= $package['package_charges'];
     echo $d;

      echo "</td></tr>"; 
    }
    }else{
        $d=0;
    }
    var_dump($d);exit;
    echo "<tr><td>";
    echo 'Total D';
    echo "</td><td>"; 

    echo "<tr><td>";
    echo 'Total A+B+C+D';
    echo "</td><td>"; 
    echo ($a+$b+$c+$d);
    echo "</table>";  
    ?>

When I am doing a var_dump for $packages, I am getting

array (size=0)
  empty

When I am doing a var_dump for $d I am getting the error variable d is not defined any specific reason for this or I am doing anything wrong here. Thanks for the suggestion.

0

1 Answer 1

1

In regard to $packages, $packageq->queryAll() must not be returning anything. Regarding $d, you are dumping it outside of the scope within which $d is defined. $d must be defined outside of the if/else if you want to be able to access $d in the scope where you have the var_dump. If you declare $d outside of the if/else with $d; or define $d as some empty value (e.g. $d = 0;), you'll be able to set it within the if/else and still have access to it in the parent scope. If you defined $d = 0 before the condition you could drop the else { $d = 0; } all together.

Also, if $packages is an array and you want to test if it populated, you might consider

if(!empty($packages)) {
    ...
}

Instead of

if(![$packages] == '') {
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @superultranova. I actually forget using if(!empty) . Thank you so much. and your suggestion of putting $d before the condition once also works fine.

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.