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
}
?>
for id=1, array 0=>this, 1=>that
So, foreach($pieces[1] as $test)
, it means "that"
The above works, but, when I change $pieces[1]
to $pieces[0]
, I still get the result which corresponds with $pieces[1]
. And, when I change it to $pieces[4]
which doesn't exist, I again get the result which corresponds with $pieces[1]
.
Where am I doing wrong? How can I make this array work?
According to tbl1.id=1 piece[0]=>this, piece[1]=>that and I want to make it work for my tables. Please help.
$pieces = explode(",", $rows['detail']);
before you've done the database query that assigns to$rows
. Should that be$pieces = explode(",", $detail);
? – Barmar Aug 20 '12 at 4:20