dongyu1979 2017-03-17 10:41
浏览 92
已采纳

如何使用php根据mysql表中的数据自动创建div?

I want to create an ecommerce website, using PHP and mySQL DB. I have this code below and I want (with the use of php if possible), after connecting to sql DB(I did this) to repeat this DIV depending on the entries in the DB table and echo the data of each entry in table. I found this but I don't know if it's the right example: Automatically creating div's using data from a sql table

Here's the code:

<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>

</div>
  • 写回答

1条回答 默认 最新

  • duanming7961 2017-03-17 11:08
    关注

    The example you provided above will work just fine, all you need to do is put the div/div's you want to generate multiple times inside a loop. then they will be generated as long as the loop condition is met.

    But though the example u provided works but you need to note that it is using mysql_* functions which are depreciated and no longer supported by newer php versions .

    Therefore I would advice that you learn about prepared statements instead, and use PDO.

    consider the following :

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

    OR

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

    Where $pdo is your database connection string.

    This will fetch data and generates the div for each product.

    If you not using prepared statements for your application, I would advice you to use them more especially pdo prepared statements, below are the places you can learn pdo prepared statements

    https://phpdelusions.net/pdo#foreach

    http://jayblanchard.net/demystifying_php_pdo.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?