dphphvs496524 2018-05-06 16:21
浏览 20
已采纳

php img src到php和数据库行?

I've searched hi and low for an answer to my queston, I'm attempting to call a php file with a script to check file size and output an image based of a database table of a certain type. Would anyone be able to help? I'm getting a undefined index for stocknr when I run it even with $row[stocknr] as $stocknr = $_GET['stocknr'].

// assume connection
$query = "SELECT year, brand, model, class, stocknr, price, status, pic FROM 
stock WHERE class = 'ldv' ORDER BY brand ASC";
// Perform Query
$result = mysqli_query($con,$query);
if (!$result) 
{
die("Connection Error: " . mysqli_connect_error());
}
// test array
$product = array();
while ($row = mysqli_fetch_array($result))
{
for($j = 1; $j <= 30; $j++) {

    $product[$j]["model"] = "MODEL-$j";
    $product[$j]["pic"] = "PIC-$j";
    $product[$j]["price"] = "PRICE-$j";
}
// counter for cells
$i = 1;
// begin a table
echo '<table border="1">' . "
";
foreach($product as $row)
// while($row = mysql_fetch_array($result)) 
{ 
    $title = $row['model'] ;
    $uppic = $row['pic'];
    $price = $row['price'];
    // for 1st, 6th, 11th etc record insert a tag for a new row
    if (($i == 1) or (($i - 1) % 4) == 0) {

        echo '<tr>' . "
";
    }
    // insert a cell
    echo ("<td width=150 align=center>");
    echo ("<a href=../imagesize.php?scode=$uppic><img 
src=images/'$uppic'.jpg border=0 alt=Item $title</a>");
// $row[stocknr]? 
    echo ("<br><a href=\"details.php?stocknr=$row[stocknr]\">
          <span class=fs13>$title</span></a>");
    echo ("</td>");
    echo ("<tr height=150>");
    echo ("<a href=\"details.php?stocknr=$row[stocknr]\">
         <span class=sapri>$price</span></a>");
    // for 5th, 10th, 15th etc record insert a tag for end of the row
    if ((($i) % 5) == 0) {

        echo '</tr>';
    }

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

 while(($i - 1) % 5 != 0) {

    // insert a blank cell
    echo '<td>&nbsp;</td>';

    // increment $i
    $i++;

    // for 5th, 10th, 15th etc record insert a tag for end of the row
    if (($i - 1) % 5 == 0) {

        echo '</tr>';
    }
}
// end the table
echo '</table>';
?>
  • 写回答

1条回答 默认 最新

  • doucan957495 2018-05-06 17:05
    关注

    you are overwriting the database output with your foreach($product as $row) line, which means that $row['stocknr'] ceases to exist.

    As far as I can tell, you don't need that foreach loop and it can be removed, but I am not sure what you are trying to achieve with the $product array.

    As an aside, the for($j = 1; $j <= 30; $j++) { loop can move outside the while loop as it's just rewriting the same values for every row in the database.

    Edit: does this do what you want?

    $query = "SELECT year, brand, model, class, stocknr, price, status, pic 
    FROM stock WHERE class = 'ldv' ORDER BY brand ASC";
    
    $i = 0;
    $result = mysqli_query($con,$query);
    
    if (!$result) {
        die("Connection Error: " . mysqli_connect_error());
    }
    
    
    while ($row = mysqli_fetch_array($result)) {
    
        $title = $row['model'] ;
        $uppic = $row['pic'];
        $price = $row['price'];
        $stock = $row['stocknr'];
    
        if (($i == 1) or (($i - 1) % 4) == 0) {
            echo '<tr>' . "
    ";
        }
    
        echo "<td width=150 align=center>";
        echo "<a href=../imagesize.php?scode=$uppic><img src='images/$uppic.jpg' border='0' alt='Item $title'></a>";
    
        echo "<br><a href='details.php?stocknr=$stock'>
               <span class=fs13>$title</span></a>";
        echo "<br><a href='details.php?stocknr=$stock'>
         <span class=sapri>$price</span></a>";
        echo "</td>";
    
        // for 5th, 10th, 15th etc record insert a tag for end of the row
        if ((($i) % 5) == 0) {
            echo '</tr>';
        }
    
        $i++;
     }
     // filling row with empty cells 
    
    while(($i - 1) % 5 != 0) {
         echo '<td> </td>';
    
         $i++;
    
        // for 5th, 10th, 15th etc record insert a tag for end of the row
        if (($i - 1) % 5 == 0) {
            echo '</tr>';
        }
    }
    
    echo '</table>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?