0

我是 php 新手,我在产品页面的一些代码上遇到了一个小问题,我试图在数据库中加载尽可能多的项目,但是,我一直收到一个

“未定义的索引”为i

我真的需要完成我正在做的这份工作。感谢所有的帮助!如果有人可以请描述如何i仍未定义,也很感激。

到目前为止,我已经尝试在任何if语句中定义变量并且while没有更改,还尝试根据数据库表中返回的行数来设置它。我现在已经没有想法了。

我想知道如何在代码中设置全局变量,因为$i在这种情况下似乎看不到,$i = "1"或者$i = 1,会issetempty真的会有所帮助吗?还是会有function帮助?

当我之前尝试使用数组时,我真的不知道该怎么做,而且这似乎if在它规定等式的地方也出现了同样的错误。是否可以使用$i = count($row);然后更改计算来解释它?

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

0 回答 0