【问题标题】:How to auto create divs depending on the data in mysql table using php?如何使用php根据mysql表中的数据自动创建div?
【发布时间】:2017-08-08 21:10:32
【问题描述】:

我想创建一个电子商务网站,使用 PHP 和 mySQL DB。我在下面有这段代码,我希望(如果可能的话,使用 php),在连接到 sql DB(我这样做)之后,根据 DB 表中的条目重复这个 DIV 并回显表中每个条目的数据。我找到了这个,但我不知道它是否是正确的例子:Automatically creating div's using data from a sql table

代码如下:

<div class="col-md-4 agileinfo_new_products_grid agileinfo_new_products_grid_mobiles">
<div class="agile_ecommerce_tab_left mobiles_grid">
    <div class="hs-wrapper hs-wrapper2">
        <img src="images/<image>.jpg" alt=" " class="img-responsive" /> 
        <div class="hs_bottom hs_bottom_sub1">
            <ul>
                <li>
                    <a href="#" data-toggle="modal" data-target="#myModal3"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></a>
                </li>
            </ul>
        </div>
    </div>
    <h5><a href="#"><Itemname></a></h5> 
    <div class="simpleCart_shelfItem">
        <p><i class="item_price"><Price></i></p>
        <p><i class="item_price"><Description></i></p>
        <form action="#" method="post">
            <input type="hidden" name="cmd" value="_cart" />
            <input type="hidden" name="add" value="1" /> 
            <input type="hidden" name="item" value="Itemname" /> 
            <input type="hidden" name="amount" value="Price"/>   
            <button type="submit" class="cart">Add to cart</button>
        </form>
    </div>  
</div>

【问题讨论】:

  • 是的,这是正确的方法。从链接
  • 你找到了一个很好的例子,为什么不实现它呢?
  • 有点像 asp 对吧??
  • 该链接是一个很好的起点,只需遍历您的数据库结果并在每次循环时用您在数据库中调用它们的任何内容替换您的字段,就像它们在您的链接中一样提供了。
  • 因为我找到了几百个其他示例,但没有一个有效,所以我想问一下!

标签: php html mysql database e-commerce


【解决方案1】:

您在上面提供的示例可以正常工作,您只需将要多次生成的 div/div 放入循环中即可。那么只要满足循环条件就会生成。

虽然你提供的例子有效,但你需要注意它 正在使用已折旧且不再使用的 mysql_* 函数 较新的 php 版本支持。

因此,我建议您改为学习准备好的语句,并使用PDO

考虑以下几点:

<?php

    $sql = $pdo->query("SELECT * FROM products")->fetchall(PDO::FETCH_ASSOC);
    foreach($sql as $row):?>
<div class="col-md-4 agileinfo_new_products_grid agileinfo_new_products_grid_mobiles">
<div class="agile_ecommerce_tab_left mobiles_grid">
    <div class="hs-wrapper hs-wrapper2">
        <img src="images/<?php echo $row['productImg'];?>" alt=" " class="img-responsive" /> 
        <div class="hs_bottom hs_bottom_sub1">
            <ul>
                <li>
                    <a href="#" data-toggle="modal" data-target="#myModal3"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></a>
                </li>
            </ul>
        </div>
    </div>
    <h5><a href="#"><?php echo $row['item_name'];?></a></h5> 
    <div class="simpleCart_shelfItem">
        <p><i class="item_price"> Price : <?php echo $row['item_price'];?></i></p>
        <p><i class="item_price">Description <?php echo $row['item_description'];?></i></p>
        <form action="#" method="post">
            <input type="hidden" name="cmd" value="_cart" />
            <input type="hidden" name="add" value="1" /> 
            <input type="hidden" name="item" value="<?php echo $row['item_name'];?>" /> 
            <input type="hidden" name="amount" value="<?php echo $row['item_price'];?>"/>   
            <button type="submit" class="cart">Add to cart</button>
        </form>
    </div>  
</div>

<?php

    endforeach;
?>

<?php

    $sql = $pdo->query("SELECT * FROM products");
    while ($row = $sql->fetchall(PDO::FETCH_ASSOC)):?>
<div class="col-md-4 agileinfo_new_products_grid agileinfo_new_products_grid_mobiles">
<div class="agile_ecommerce_tab_left mobiles_grid">
    <div class="hs-wrapper hs-wrapper2">
        <img src="images/<?php echo $row['productImg'];?>" alt=" " class="img-responsive" /> 
        <div class="hs_bottom hs_bottom_sub1">
            <ul>
                <li>
                    <a href="#" data-toggle="modal" data-target="#myModal3"><span class="glyphicon glyphicon-eye-open" aria-hidden="true"></span></a>
                </li>
            </ul>
        </div>
    </div>
    <h5><a href="#"><?php echo $row['item_name'];?></a></h5> 
    <div class="simpleCart_shelfItem">
        <p><i class="item_price"> Price : <?php echo $row['item_price'];?></i></p>
        <p><i class="item_price">Description <?php echo $row['item_description'];?></i></p>
        <form action="#" method="post">
            <input type="hidden" name="cmd" value="_cart" />
            <input type="hidden" name="add" value="1" /> 
            <input type="hidden" name="item" value="<?php echo $row['item_name'];?>" /> 
            <input type="hidden" name="amount" value="<?php echo $row['item_price'];?>"/>   
            <button type="submit" class="cart">Add to cart</button>
        </form>
    </div>  
</div>

<?php

    endwhile;
?>

$pdo 是您的数据库连接字符串。

这将获取数据并为每个产品生成 div。

如果你的应用程序没有使用准备好的语句,我建议你更多地使用它们,尤其是 pdo 准备好的语句,下面是你可以学习 pdo 准备好的语句的地方

https://phpdelusions.net/pdo#foreach

http://jayblanchard.net/demystifying_php_pdo.html

【讨论】:

  • 我应该学习一下 PDO。进行更改后我得到的是“调用未定义的方法 mysqli_result::fetchall()”。
  • 你需要pdo连接,你不再使用mysqli。在我上面给出的链接上,有一种连接方式......在这里用你的代码评论,这样我就可以看到它或添加你的问题
  • 你要我发送连接数据库的 php 字符串吗??
  • 是的,我想要那个
  • set_charset("utf8"); // 检查连接 if ($conn->connect_error) { die("连接失败:" . $conn->connect_error); } ?>
猜你喜欢
  • 2012-08-20
  • 2017-05-28
  • 2020-09-28
  • 2023-03-20
  • 2011-10-21
  • 2020-09-28
  • 1970-01-01
  • 1970-01-01
  • 2017-12-14
相关资源
最近更新 更多