Php 如何通过相邻单元中的数量来回传图像?
我有以下数据库Php 如何通过相邻单元中的数量来回传图像?,php,Php,我有以下数据库 +---------+----------+-----------+--------------+--------------------------------+-----------+ | id | award | image | type | description | qty | +---------+----------+-----------+--------------
+---------+----------+-----------+--------------+--------------------------------+-----------+
| id | award | image | type | description | qty |
+---------+----------+-----------+--------------+--------------------------------+-----------+
| 1 | award 1 | award.jpg | award type 1 | 2010, 2011, 2013, 2014, | 4 |
+---------+----------+-----------+--------------+--------------------------------+-----------+
我的php代码
<?php
$query = "SELECT * FROM database WHERE type="award type 1"";
$query_award = mysql_query($query) or die(mysql_error());
$result = '';
$count = 0;
while($row = mysql_fetch_array($query_award)){
$count++;
$id = $row['id'];
$award = $row['award'];
$image = $row['image'];
$type = $row['type'];
$description = $row['description'];
$qty = $row['qty'];
$result .= "
<div class='type'>" . $type . "</div>
<div class='awards'>" . $image . "</div>
<div class='details'>" .$award . " (" . $qty . "):" . $description . "</div>
";
}
?>
您可以为
循环使用。您还需要在1
处启动计数器,而不是0
,因为您使用的是基于1的
索引
$result .= "
<div class='type'>". $type ."</div>
<div class='awards'>";
for($i = 1; $i < $row['qty']; $i++):
$result .= $image;
endfor;
$result .="
</div>
<div class='details'>" .$award . " (" . $qty . "):" . $description . "</div>
";
$result.=”
“$type。”
";
对于($i=1;$i<$row['qty'];$i++)而言:
$result.=$image;
endfor;
$result.=”
“$奖金。“(“$qty”):”$描述"
";
为什么不直接使用
我不知道怎么做,我做了一些搜索,但什么也找不到。考虑到一些让选民失望的评论,这看起来很简单?
<?php
$img = "image.jpg";
$qty = 4;
echo str_repeat(sprintf('<div class="awards">%1$s</div>', $img), $qty);
?>