-2

I'm getting a really annoying error Undefined variable: count. Here is my code:

<?php
    $user = getUserData('Username');
    $sql = "SELECT `Username`, `Uplata`, `Dobivka`, `Date` FROM `kladilnica` WHERE `Username`='$user'";
    $result = $conn->query($sql);
    if($result === false) {
        echo "<b>Could not connect to database.</b>";
        trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
    } else {
        if($result->num_rows == 0) {
            echo "<b>Something went wrong try again later.</b>";
        } else if($result->num_rows >= 1) {
            echo "<table id='t01'><tr><th>ID</th><th>Username</th><th>Uplata</th><th>Dobivka</th><th>Date</th></tr>";
            while($row = $result->fetch_assoc()) {
                $count = $count + 1;
                echo "<tr><td>".$count."</td> <td>".$row["Username"]."</td> <td>".$row["Uplata"]."</td> <td>".$row["Dobivka"]."</td> <td>".$row["Date"]."</td></tr>";
            }
                echo "<tr><th colspan='5'>Vkupna Uplata:</th></tr>";
                echo "<tr><th colspan='5'>Vkupna Dobivka:</th></tr>";
                echo "</table>";
            }
        }
    $conn->close();
    ?>
Elliot Gorokhovsky
  • 3,610
  • 2
  • 31
  • 56
Stefan
  • 9
  • 4
  • 5
    You can not increment a value that isn’t even set before. Initialize the variable with a start value before the while loop. – CBroe Jan 24 '15 at 23:59

3 Answers3

3

You need to initialize $count to some value before you try to increment it with $count = $count + 1. You can change the while loop to a for loop and initialize $count in the for statement:

for ($count = 0; $row = $result->fetch_assoc(); $count++){
    // do something
}
celeritas
  • 2,191
  • 1
  • 17
  • 28
  • Thank you for your answer i have another question and i know i'm not allowed to do this but its on the same code ... how do i take all numbers from `Uplata` row and gather to get the total of them ? – Stefan Jan 25 '15 at 00:16
  • If it's an array of numbers, why not use `array_sum`? E.g., `$sum = array_sum($row["Uplata"])`. – celeritas Jan 25 '15 at 00:24
  • it gives me error: `Warning: array_sum() expects parameter 1 to be array, null given` – Stefan Jan 25 '15 at 00:27
  • So it's not an array, hence there's nothing to sum. I suppose you want to add whatever the value of $row["Uplata"] is to a running total; you should initialize some value before the loop, e.g., `$uplata_total = 0`, and add the value of $row["Uplata"] to the running total in the loop, `$uplata_total += $row["Uplata"]` – celeritas Jan 25 '15 at 00:30
  • Its giving me always result `0` here is the code `$uplata_total += $row["Uplata"]; echo "Vkupna Uplata: $uplata_total";` – Stefan Jan 25 '15 at 00:37
  • Looks like $row["Uplata"] is null or 0 for all your rows. – celeritas Jan 25 '15 at 00:39
  • oops my mistake sorry it works fine Thank you :) – Stefan Jan 25 '15 at 00:40
1

You are reading the variable $count in the expression $count + 1. On the first iteration of the while loop, the variable isn't assigned yet, and that causes your error. You cannot read/use a variable that is not defined yet.

The solution is simple: Initialize $count to 0 before the loop.

$count = 0;
while($row = $result->fetch_assoc()) {
   $count = $count + 1;
   echo "<tr><td>".$count."</td> ......";
}
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
-3

I would suggest to change

$count = $count + 1;

to

$count+=1;

That should do the trick.

Steve Lloyd
  • 773
  • 2
  • 12
  • 33