使用不同的数据库表ID重复div宽度

时间:2016-02-10 20:41:29

标签: php jquery html mysql

我正在尝试使用与数据库表中的行数一样多次重复div相同的div。我想要的一个非工作的例子如下。

<?php for ($i=0; $i < 3; $i++) { 
            ?><!-- switch the three with the number of rows in the table-->

我想循环容器并使用值i切换数据库查询。

        <div id = "bodyContainer">

            <form class="review">

                <div class="reviewContainer">

                    <div class = "images">
                        <?php 
                            $link = mysqli_connect("localhost", "root", "root","DJP");
                                    if (mysqli_connect_error()) {
                                        die("Could not connect to database");
                                    }
                            $query = "SELECT * FROM reviews WHERE id = '<?php echo $i ?><!--switch the value of i here-->'";
                            $result=mysqli_query($link, $query);
                            $row = mysqli_fetch_array($result);
                            echo  '<img height = 90% width = 100% src="data:image/jpeg;base64,'.$row[3].' " />';

                        ?>
                    </div>

                    <!--<div class="title">
                        <?php
                            //echo $row[1];
                        ?>
                    </div>-->

                    <div class = "comment">
                        <?php
                            echo $row[2];
                        ?>
                    </div>

                    <div class = "rating" style = "float: right;">
                        <?php
                            echo $row[4];
                        ?>
                        <br/>
                        <br/>   
                        stars
                    </div>

                </div><!--end review-->
            </form>

        </div><!--end body container-->

        <?php;}?><!-- end the for loop so all the divs can be re-done with a new entry for the query. I would move the $link out of loop also.-->

1 个答案:

答案 0 :(得分:0)

只需获取所有行一次,然后在打印数据时浏览它们:

$link = mysqli_connect("localhost", "root", "root","DJP");
        if (mysqli_connect_error()) {
            die("Could not connect to database");
        }
$query = "SELECT * FROM reviews";
$result=mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)){ ?>

<div id = "bodyContainer">

    <form class="review">

        <div class="reviewContainer">

            <div class = "images">
                <?php 

                    echo  '<img height = 90% width = 100% src="data:image/jpeg;base64,'.$row[3].' " />';

                ?>
            </div>

            <!--<div class="title">
                <?php
                    //echo $row[1];
                ?>
            </div>-->

            <div class = "comment">
                <?php
                    echo $row[2];
                ?>
            </div>

            <div class = "rating" style = "float: right;">
                <?php
                    echo $row[4];
                ?>
                <br/>
                <br/>   
                stars
            </div>

        </div><!--end review-->
    </form>

</div><!--end body container-->

<?php } ?>