经过小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:
cellpadding="{TABLESPACE}" width="100%">
colspan=2>校园道具店
align=center>
title="$item[name]">
height=30>
道具:$item[name]
height=30>
价钱:$item[price]
height=30>
说明:$item[description]
height=30>
购买
那就在需要循环的部分加上loop
QUOTE:
cellpadding="{TABLESPACE}" width="100%">
colspan=2>校园道具店
align=center>
title="$item[name]">
height=30>
道具:$item[name]
height=30>
价钱:$item[price]
height=30>
说明:$item[description]
height=30>
购买
这样就行了
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自带的?
---------------------------------------------------
解释成foreach($a as $b => $c}
---------------------------------------------------