0

When I click on a project in my web app, that it contains values, I do not get any error, but I created another project with empty fields in MySQL, and I clicked on it, in my web app (PHP app), so I got this error:

Undefined variable: total in C:\wamp\www\architect\projDetails.php on line 80

I have this PHP code:

$id = $_REQUEST['id'];
$sql = "SELECT (SELECT SUM(total_pay) FROM workers) total,workers. * FROM workers WHERE projects_id = ".$id." ORDER BY date_of_pay DESC";
$stmt = mysqli_query($con, $sql) or die($sql."<br/><br/>".mysqli_error($con));

And here html and php code near line 80:

 <tr>

      <?php while($rows = mysqli_fetch_array($stmt)){ $total = 0; ?>
    <tr>
    <?php if($rows['total']!=0){
        $total = $rows['total'];
    }
    else {
        $total = "غير متوفر";
    }
        ?>
      <td align="center"><?php echo $rows['total_pay']?></td>
      <td align="center"><?php echo $rows['date_of_pay']?></td>
      <td align="center"><?php echo $name['project_name'] ?></td>
      <td align="center"><!--<input class="imgClass_insert" type="submit" name="submit1" value="" />-->
        <input class="imgClass_dell" type="submit" onClick="return confirm('Are you sure you want to delete?')" name="delete_workers" value=""/>


    </td>
  </tr>

    </tr>
  <?php } ?>
  <tr>    

    <td colspan="3">مجموع تكاليف العمال في مشروع <?php echo $name['project_name']?></td>
    <td align="center"><?php echo $total ?></td>
  </tr>

So when total is empty, I get the error,and when it is not empty, I don't get any errors, so what is the problem here?

am90
  • 201
  • 2
  • 11
  • I believe you need to learn the differnce between a **notice** and an **error**, you can find all you need [here](http://stackoverflow.com/questions/4624474/php-difference-between-notice-and-warning) – Naruto Nov 23 '15 at 09:12
  • At the bottom of the second code block you posted you do a `echo $total`. However if the sql query at the top does _not_ return _any_ values, then `$total` is _never_ defined, thus undefined. A simple solution would be to set a safe default for `$total` _before_ the `while()` loop. – arkascha Nov 23 '15 at 09:13
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Naruto Nov 23 '15 at 09:16

3 Answers3

0

Try something like below.

echo isset($total)? $total: 0;
Kaushik Maheta
  • 1,741
  • 1
  • 18
  • 27
peanut
  • 106
  • 6
0

Your variable $total is currently only alive within the while block. You are exiting this block at the statement </tr><?php } ?><tr>. That means your variable is no longer allocated in code row <td align="center"><?php echo $total ?></td> below. To use this total variable below the while block, add the declaration and initialization of the variable above the while block, e.g:

<tr>
    <?php $total = 0;
          while($rows = mysqli_fetch_array($stmt)){ ?>
    <tr>
        <?php if($rows['total']!=0){      <-- Check this comparison (described below)
            $total = $rows['total'];
        }
        else {
            $total = "غير متوفر";
        }
        ?>
        <td align="center"><?php echo $rows['total_pay']?></td>
        <td align="center"><?php echo $rows['date_of_pay']?></td>
        <td align="center"><?php echo $name['project_name'] ?></td>
        <td align="center"><!--<input class="imgClass_insert" type="submit" name="submit1" value="" />-->
            <input class="imgClass_dell" type="submit" onClick="return confirm('Are you sure you want to delete?')" name="delete_workers" value=""/>
        </td>
    </tr>
</tr>
<?php } ?>
<tr>    
    <td colspan="3">مجموع تكاليف العمال في مشروع <?php echo $name['project_name']?></td>
    <td align="center"><?php echo $total ?></td>
</tr>

Hope this will work for you. The annotated line in code above will compare a string with an integer value. Please be aware that $rows['total'] will contain a value of type String. Your comparison is against an integer value of 0. PHP should not throw an error or warning but it could come to unwanted results in this if statement. (Further information: http://php.net/manual/de/language.types.type-juggling.php )

Kevin
  • 26
  • 4
0

Here if we are getting any data from DB, then only it will enter into the while loop. Now inside while loop only $total is defining. If we define it outside the while loop, the default value, will solve the issue

<tr> <?php 
    $total = "0";
    while($rows = mysqli_fetch_array($stmt)){ $total = 0; 
      if($rows['total']!=0){
          $total = $rows['total'];
      }
      //.....
      //.....
    } ?>
</tr>
<tr>
    <td colspan="3">some text <?php echo $name['project_name']?></td>
    <td align="center"><?php echo $total ?></td>
</tr>

Thank you

Santhy K
  • 829
  • 1
  • 7
  • 12