0

I'm new to php and i'm having a small problem with some code for a products page where i'm attempting to load as many items as their are on a database however, I keep receiving an

"Undefined index" for i

I really need to finish off this job i'm doing. Appreciate all the help! If anyone could please describe how i remains undefined, also appreciated.

So far I've tried to define the variable inside any if statements and while with no change, also tried to set it up based off the number of rows that return in the table of the database. I've run out of idea's at the moment.

I would like to know how one sets a global variable in the code as it seems $i in this case does not seem to see $i = "1" or $i = 1, will isset or empty really going to help? or will a function help?

I'm really at a loss of what to do as I tried to use an array before and that also seemed to bring up the same error within an if where it stipulates the equation. Is it possible to use $i = count($row); then change the calculation to account for it?

<?php
    $servername = "server";
    $dbusername = "username";
    $dbpassword = "password";
    $dbname = "database";

    $con=mysqli_init();
    if (!$con)
    {
        die("mysqli_init failed");
    }
    if (!mysqli_real_connect($con,$servername,$dbusername,$dbpassword,$dbname))
    {
        die("Connect Error: " . mysqli_connect_error());
    }

    $query = "SELECT year, brand, model, class, stocknr, price, status, pic 
    FROM stock WHERE class = 'car' ORDER BY brand ASC";


    $result = mysqli_query($con,$query);

    $i = "0";

    if (!$result) 
    {
        die("Connection Error: " . mysqli_connect_error());
    }

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

        $title = (".$row[brand] ".".$row[model]") ;
        $uppic = $row['pic'];
        $price = $row['price'];
        $stock = $row['stocknr'];
        if (($i == 1) or (($i - 1) % 4) == 0) 
        {
            echo '<tr>' . "\n";
        }
        echo "<td>";
        echo "<tr width=250 height=30 align=center>";
        echo "<br><a href='details.php?stocknr=$stock'>;
              <span class=fs13>$title</span></a>";
        echo "</tr>";
        echo "<tr width=250 align=center>";
        echo "<a href='details.php?stocknr=$stock'>;
        <img src='upload/imagesize.php?w=220&h=250&img=$uppic.jpg' border='0' alt=' 
        $title'></a>";
        echo "</tr>";
        echo "<tr width=250 height=30 align=center>";
        echo "<br><a href='details.php?stocknr=$stock'>;
         <span class=sapri>R$price</span></a>";
        echo "</tr>";
        echo "</td>";

        // for 5th, 10th, 15th etc record insert a tag for end of the row
        if ((($i) % 4) == 0) 
        {
            echo '</tr>';
        }

        $i++;
    }
    // filling row with empty cells 

    while(($i - 1) % 4 != 0) 
    {
        echo '<td> </td>';

        $i++;

        // for 5th, 10th, 15th etc record insert a tag for end of the row
        if (($i - 1) % 4 == 0) 
        {
            echo '</tr>';
        }
    }

    echo '</table>';
?>
sean
  • 5
  • 5
  • $i=0 instead of $i = "0" – Kamlesh Solanki May 10 '18 at 08:22
  • 1
    Undefined index != undefined variable. The variable `$i` is defined, but the index is not necessarily defined. Can you show the full notice, which line is the problem? – Qirel May 10 '18 at 08:25
  • 2
    @KamleshSolanki That doesn't matter at all.. `0 == "0"` is true, and the variable is defined either way. – Qirel May 10 '18 at 08:25
  • In future it is also useful for us to see the FULL error message and also for you to identify the line in your code mentioned in the error message. Saves us having to search all the code looking for the error – RiggsFolly May 10 '18 at 08:28
  • Apologies that would be the the lines where `if ((($i) % 4) == 0)` and while (($i - 1) % 4 !=0) that i received `Notice: Undefined Index i in ...on line... – sean May 10 '18 at 09:09
  • @Qirel lines reported for undefined index are `if ((($i)%4)==0)` and `while(($i - 1)%4!=0)` should i add `$i=1` below `while ($row=mysqli_fetch_array($result))`? – sean May 10 '18 at 16:06

0 Answers0