Forums
This topic is locked
using foreach statement
Posted 17 Mar 2004 23:43:06
1
has voted
17 Mar 2004 23:43:06 robert Fuller posted:
HiI am having problems creating my own shopping cart. I am having trouble using the foreach statment when reteiving data from a session. The code i have is:
$query = 'SELECT * FROM Stock, Genre_Link WHERE Stock.Genre = Genre_Link.Genre AND Stock.CatNo IN ('foreach ($_SESSION['cart'] as $key => $value) ;
{
$query .=$key . ',';
}
$query = substr($query, 0, -1) . ') ORDER BY Stock.CatNo ASC';
$result = mysql_query ($query);
$nrows = mysql_num_rows($result);
If anyone could point in the direction of where i am going wrong, it would be much appreciated.
The error iam getting is:
Warning: Invalid argument supplied for foreach().
Cheers
Edited by - Rob_F on 17 Mar 2004 23:49:05
Replies
Replied 20 Mar 2004 20:45:20
20 Mar 2004 20:45:20 Phil Shevlin replied:
I didn't even know there was a foreach function, but you seem to have syntax errors:
<pre id=code><font face=courier size=2 id=code>
$query = "SELECT * FROM Stock, Genre_Link WHERE Stock.Genre = Genre_Link.Genre AND Stock.CatNo IN (".foreach ($_SESSION['cart'] as $key => $value)."" ;
</font id=code></pre id=code>
That probably isn't going to work, but you open quotes without closing them, and you are not concatenating the results of the foreach function
Edited by - wdglide on 20 Mar 2004 20:46:59
<pre id=code><font face=courier size=2 id=code>
$query = "SELECT * FROM Stock, Genre_Link WHERE Stock.Genre = Genre_Link.Genre AND Stock.CatNo IN (".foreach ($_SESSION['cart'] as $key => $value)."" ;
</font id=code></pre id=code>
That probably isn't going to work, but you open quotes without closing them, and you are not concatenating the results of the foreach function
Edited by - wdglide on 20 Mar 2004 20:46:59