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

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

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀