【发布时间】:2012-08-20 03:21:49
【问题描述】:
tbl1
id | detail
1 | this, that
2 | these, those
tbl2
id | visit | cost
1 | this | 20
2 | that | 30
3 | these | 40
4 | those | 50
PHP:
<?php
$id=$_GET['id'];
$detail=$_GET['detail'];
$pieces = explode(",", $rows['detail']);
// $pieces[0]; piece1
// $pieces[1]; piece2
// $pieces[2]; piece2
// $pieces[3]; piece3
// $pieces[4]; piece4
foreach($pieces[1] as $test){
$query="SELECT * FROM tbl1, tbl2 WHERE
$test = tbl2.cost AND
tbl1.id='$id'";
}
$result=mysql_query($query) or die("error: " . mysql_error());
while($rows=mysql_fetch_array($result)){
?>
<table>
<tr>
<td><input value="<? echo $rows['code'];?>" readonly="readonly"/></td>
<td><input value="<? echo $rows['cost']; ?>" readonly="readonly"/></td>
</tr>
</table>
<?php
}
?>
对于 id=1,数组 0=>this,1=>that
所以,foreach($pieces[1] as $test),意思是“那个”
上述方法有效,但是,当我将$pieces[1] 更改为$pieces[0] 时,我仍然得到与$pieces[1] 对应的结果。而且,当我将其更改为不存在的$pieces[4] 时,我再次得到与$pieces[1] 对应的结果。
我哪里做错了?我怎样才能使这个数组工作?
根据 tbl1.id=1 piece[0]=>this,piece[1]=>that 我想让它适用于我的桌子。请帮忙。
【问题讨论】:
-
在完成分配给
$rows的数据库查询之前,您拥有$pieces = explode(",", $rows['detail']);。应该是$pieces = explode(",", $detail);?