经过小C和丧W的指点,偶终于明白一点了……
这个似乎用于历遍一个数组之类的东西
在php里要先弄出一个数组
我想做个道具店,道具的信息用数据库存在cdb_shop里面,有id、name等等字段
我想在html里把所有的道具信息列自动出来,就要用这个loop
先在php里把数组建起来,很简单的两句
[Copy to clipboard]CODE:
$query=$db->query("select * from cdb_shop");
while($item = $db->fetch_array($query)){$itemlist[] = $item;}
之后所有的数据就存在$itemlist这个数组里
然后我在html里想把道具列出来,比如用这个表格
[Copy to clipboard]CODE:
<table border="0" cellspacing="{BORDERWIDTH}" cellpadding="{TABLESPACE}" width="100%">
<tr class="header" ><td align="center" colspan=2>校园道具店</td></tr>
<tr>
<td rowspan=4 bgcolor="{ALTBG1}" width="30%" height=90 align=center>
<img src="images/shop/$item[id].gif" border="0" title="$item[name]">
</td>
<td bgcolor="{ALTBG1}" align=center width="70%" height=30>
道具:$item[name]
</td>
<tr>
<td bgcolor="{ALTBG1}" align=center width="70%" height=30>
价钱:$item[price]
</td>
</tr>
<tr>
<td bgcolor="{ALTBG1}" align=center width="70%" height=30>
说明:$item[description]
</td>
<tr>
<td bgcolor="{ALTBG1}" align=center width="70%" height=30>
<a>购买</a>
</td>
</tr>
</tr>
</table>
那就在需要循环的部分加上loop
QUOTE:
<table border="0" cellspacing="{BORDERWIDTH}" cellpadding="{TABLESPACE}" width="100%">
<tr class="header" ><td align="center" colspan=2>校园道具店</td></tr>
<!--{loop $itemlist $item}-->
<tr>
<td rowspan=4 bgcolor="{ALTBG1}" width="30%" height=90 align=center>
<img src="images/shop/$item[id].gif" border="0" title="$item[name]">
</td>
<td bgcolor="{ALTBG1}" align=center width="70%" height=30>
道具:$item[name]
</td>
<tr>
<td bgcolor="{ALTBG1}" align=center width="70%" height=30>
价钱:$item[price]
</td>
</tr>
<tr>
<td bgcolor="{ALTBG1}" align=center width="70%" height=30>
说明:$item[description]
</td>
<tr>
<td bgcolor="{ALTBG1}" align=center width="70%" height=30>
<a>购买</a>
</td>
</tr>
<!--{/loop}-->
</tr>
</table>
这样就行了
loop $itemlist $item
被解释成foreach($itemlist as $item){...},效果是把数组$itemlist里的数据一个个的传给$item并执行{}中的语句。假如$itemlist={1,2,3},那第一次循环的时候$item=1第二次循环的时候$item=2。同理在我的例子中,第一次循环的时候$item是第一组数据,第二次循环的时候$item是第二组数据...就这样。更详细的foreach看http://php.winet.cn/control-structures.foreach.php
我的理解就这么多,现在还在迷惑loop $a $b $c三个变量的时候是怎么回事
还有就是这个该死的loop,我翻便php手册也找不到它,莫非不是标准东东是DZ自带的?
---------------------------------------------------
<!--{loop $a $b $c}-->解释成foreach($a as $b => $c}
---------------------------------------------------
分享:
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
评论