打印购物网站数据库表中的信息



我尽了最大努力,但还是遇到了这个问题。抱歉我英语不好。

因此,情况如下:我想做一个小购物网站。我的数据库表如下:

id   pic        titel               desc                    price
1    gta5.png   Grand Theft Auto 5  A open world game...    49.99
2    cod.png    Call Of Duty: MW    A Ego-Shooter game...   59.99
3    play4.png  Playstation 4       Next-Gen Console...     249.99
4    contr.png  Ps4 Controller      Next-Gen Equipment...   69.99

在数据库类文件中,我有一些函数,可以从表中获取列信息:

public function getImg(){
while($row = mysqli_fetch_row($this->query)){
return $row[1];
}
}
public function getTitel(){
while($row = mysqli_fetch_row($this->query)){
return $row[2];
}
}
...

使用getImg((,我将获得图片列上的所有信息,如:gta5.png、cod.png等。

我的问题从这里开始。我想在index.php文件上用for循环打印出每一列。但我看不到任何信息。这是index.php文件:

<ul>
<li>
<div class="container">
<div class="content">
<?php 
for($i = 0; $i < $database->getTableLength(); $i++){
?>
<img src="image/<?php echo $database->getImg(); ?>"></img>
<h4><?php echo  $database->getTitel(); ?></h4>
<h5><?php echo  $database->getDesc(); ?></h5>
<h2><?php echo  $database->getPrice(); ?>€</h2>
<input type="button" name="submit" value="Buy">
<?php 
}
?>
</div>
</div>
</li>
</ul>

但正如我所说,什么都没有。getTableLength((函数是表的长度(在本例中为4(。我还试着像这样一个接一个地打印出来:

<ul>
<li>
<div class="container">
<div class="content">
<?php 
for($i = 0; $i < $database->getTableLength(); $i++){
echo $database->getImg() . "<br>";
}
?>
</div>
</div>
</li>
</ul>

但是又失败了。。我甚至可以看到br标签,但看不到我想打印出来的信息。当我把回显线放在for循环之前时,我可以看到列的第一个索引(gta5.png(,但我希望所有列都有。

希望看到一些解决方案。谢谢你的帮助!

编辑:当我写5而不是长度函数时,它就起作用了:

for($i = 0; $i < 5; $i++){
echo $database->getImg();
}

但我仍然想使用这个函数。getTableLength((函数没有像我预期的那样工作。插入函数时没有什么可看的。getTable函数如下所示:

public function getTableLength(){
$sql = "SELECT COUNT(*) AS num FROM `$this->table`";
$this->query = mysqli_query($this->connect, $sql);
if($this->query){
$row = mysqli_fetch_assoc($this->query);        
return $row['num'];
}
}

当我在index.php文件上调用它时,比如:

$database->getTableLength();

我什么也看不见,一切都不见了。它就像童车。

已修复。这太复杂了。简单思考,做得更好。。

以下是解决方案:

<?php 
$table = $database->getTableName();
$sql = "SELECT * FROM `$table`";
$connect = $database->getConn();
$result = mysqli_query($connect, $sql);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){

?>
<form method="post" action="index.php?action=add&id=<?php echo $row["id"] ?>">
<ul>
<li>
<div class="container">
<div class="content">
<img src="image/<?php echo $row["bild"]; ?>"></img>
<h4><?php echo $row["titel"]; ?></h4>
<h5><?php echo $row["beschreibung"]; ?></h5>
<h2><?php echo $row["preis"]; ?>€</h2>
<input type="hidden" name="hidden_name" value="<?php echo $row["titel"]; ?>">
<input type="hidden" name="hidden_price" value="<?php echo $row["preis"]; ?>">
<input type="submit" name="add_to_cart" value="Kaufen">
</div>
</div>
</li>
</ul>
<?php   

最新更新