【问题标题】:How to I display a div for each row of data in a database?如何为数据库中的每一行数据显示一个 div?
【发布时间】:2015-05-08 14:08:43
【问题描述】:

我有一个存储项目信息的数据库,包括图像源路径、描述等。目前数据是静态的,如下面的代码 sn-p 所示。基本上我需要为表中的每一行数据输出一个新的“缩略图”。我认为这是由 for 循环完成的,但是我不确定如何实现这一点。谢谢。

<div class='panel-heading' contenteditable='false'>Your items for sale</div>
<div class='panel-body'>
    <div class='row'>
        <div class='col-md-4'>
            <div class='thumbnail'>
                <img alt='300x200' src='http://lorempixel.com/600/200/people'>
                <div class='caption'>
                    <h3>
                        Rover
                    </h3>
                    <p>Cocker Spaniel who loves treats.</p>
                    <p></p>
                </div>
            </div>
        </div>
        <div class='col-md-4'>
            <div class='thumbnail'>
                <img alt='300x200' src='http://lorempixel.com/600/200/city'>
                <div class='caption'>
                    <h3>
                        Marmaduke
                    </h3>
                    <p>Is just another friendly dog.</p>
                    <p></p>
                </div>
            </div>
        </div>
        <div class='col-md-4'>
            <div class='thumbnail'>
                <img alt='300x200' src='http://lorempixel.com/600/200/sports'>
                <div class='caption'>
                    <h3>
                        Rocky
                    </h3>
                    <p>Loves catnip and naps. Not fond of children.</p>
                    <p></p>
                </div>
            </div>
        </div>
    </div>
</div>

我的尝试:

<? $sql = "SELECT srcpath FROM images";
$res = mysql_query($sql) or die(mysql_error());

$result = mysql_query($sql);

echo '<div"><ul>';

while ($fetch = mysql_fetch_array($result)) {
    $petname = $fetch['username'];
    echo '<li><P>'.$fetch['description'].'</P>';
    echo '<img src="'.$fetch['srcpath'].'" alt="" />';
    echo '</li>';
}
echo '</ul></div>';

?>

【问题讨论】:

  • 我认为人们在给出答案之前会想看看你的镜头。您是否找到任何教程或您尝试遵循的任何内容?您尝试过任何 PHP 代码吗?
  • 抱歉,你是对的。我已经在一定程度上进行了尝试,并将添加到主帖中。
  • 您是否遇到了某种错误?如我所见,您的查询中不存在“用户名”和“描述”字段,
  • stop using mysql_* functions。它们不再被维护并且是officially deprecated。改为了解 prepared statements,并考虑使用 PDO,it's not as hard as you think
  • @JayBlanchard ^ 也可以被忽略 Sam。但是,不是 moi ;-)

标签: php html web


【解决方案1】:

我仍然不鼓励使用 mysql,你应该使用 mysqli 或其他的,因为 mysql 已被弃用。

无论如何,我建议通过 php 准备整个 html 循环,然后打印出来,如下所示:

首先,这将是您的 php 部分:

<?
$sql = "SELECT username, description, srcpath FROM images"; // Note that I included the fields you're actually using.

$res = mysql_query($sql) or die(mysql_error());

$result = mysql_query($sql);

$divHtml ='<div class='panel-body'>'; // Here's where you add the main div that holds the others.

//now in the loop you've got to be careful with the css classes, as your html shows, they won't be always the same. But this should give you some insight on how it works.

while($fetch = mysql_fetch_array($result)){

    $divHtml .='<div class="row">';// add css classes and the like here. In case you don't know, the .= operators concatenate the strings that will make your html code.
    $divHtml .='    <div class="col-md-4">'; // be careful with this class, as you might need to evaluate it for every run of the loop
    $divHtml .='        <div class="thumbnail">';
    $divHtml .='            <li><P>' . $fetch['description'] .'</P>';
    $divHtml .='            <img src="'.$fetch['srcpath'].'" alt="" />';
    $divHtml .='            </li>';
    $divHtml .='        </div>';
    $divHtml .='    </div>';
    $divHtml .='</div>';
}

$divHtml .= '</div>'; // And here you close the main div.
?>

那么你的 html 将如下所示:

<div class='panel-heading' contenteditable='false'>Your items for sale</div>
          <?php echo $divHtml; ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2014-03-05
    • 1970-01-01
    • 2019-03-14
    • 2021-12-13
    • 1970-01-01
    • 2020-05-05
    相关资源
    最近更新 更多