[英]php script to display data from SQL database
我在phpmyadmin上有一個數據庫,我想將一些數據(不是所有數據)從表顯示為HTML / PHP上的表。 我無法弄清楚我的代碼出了什么問題。 每個表格行必須包含3個單元格。 這就是為什么我在php腳本中使用$ nrArt變量。 你能幫我嗎?
<table class="art-content">
<div class="row">
<? php
try {$cnx= new PDO("mysql:host=localhost;charset=utf8;dbname=restaurant","root", "");
$interogare=$cnx -> prepare("SELECT id_preparat, nume, pret, foto from preparate WHERE id_categorie= 1");
$interogare -> execute();
$nrArt = 0;
foreach ($interogare-> fetchAll() as $linie){
$id=$linie["id_preparat"];
$nume=$linie["nume"];
$pret=$linie["pret"];
$img=$linie["foto"];
if($nrArt==0){
echo '<tr class="col-1-3">';
}
echo '<div class="wrap-col">';
echo '<td class="item-container">';
echo'<a class="example-image-link" href="images/'.$img.'" data-lightbox="example-set" data-title="'.$nume.'">';
echo '<div class="item-caption">';
echo '<div class="item-caption-inner">';
echo'<div class="item-caption-inner1">';
echo'<h3>Pret</h3>';
echo '<span>'.$pret.'RON</span>';
echo'</div>';
echo '</div>';
echo '</div>';
echo '<img class="example-image" src="images/'.$img.'" alt=""/>';
echo'</a>';
echo '</td>';
$nrArt++;
if($nrArt>2){
echo '</tr>';
}
}
}
catch (PDOException $e){
die("Conectare imposibila: ". $e -> getMessage()); }
?>
</div>
</table>
您忘了重設變量,所以
不:
if($nrArt>2){
echo '</tr>';
}
}
但:
if($nrArt>2){
echo '</tr>';
$nrArt = 0;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.