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>';
?>