我有一个数据库,用于存储项目的信息,包括图像源路径,描述等。目前,数据是静态的,如下面的代码片段所示。基本上我需要为表中的每一行数据输出一个新的'缩略图'。我认为这是通过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>';
?>
答案 0 :(得分:0)
我仍然不鼓励使用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; ?>