Logician Posted November 3, 2011 Share Posted November 3, 2011 I am getting the syntax error "unexpected T_FOREACH" and I can't see why? I have checked and rechecked but can't find anything wrong with the code. Why am I getting unexpected T_FOREACH? <?php session_start(); $link = mysql_connect('localhost', 'me', '123ok'); mysql_select_db('mydb', $link); $query = "INSERT INTO orders (id, cartcontent) VALUES (null, '". foreach ($_SESSION['cart']['content'] as $content) { $querycontent = "select * from products where id='{$content['id']}'"; $result = mysql_query($querycontent); $row = mysql_fetch_array($result); if ($content['sizes'] == 0) {goto sizel;} else {echo 'Small: "'.$content['sizes'].'"';} sizel: if ($content['sizel'] == 0) {goto endsizes;} else {echo 'Large: "'.$content['sizel'].'"';} endsizes: mysql_query ($querycontent); } ."')"; mysql_query ($query); mysql_close($link); ?> I also tried this, but getting same result. <?php session_start(); $link = mysql_connect('localhost', 'me', '123ok'); mysql_select_db('mydb', $link); $foobar = foreach ($_SESSION['cart']['content'] as $content) { $querycontent = "select * from products where id='{$content['id']}'"; $result = mysql_query($querycontent); $row = mysql_fetch_array($result); if ($content['sizes'] == 0) {goto sizel;} else {echo 'Small: "'.$content['sizes'].'"';} sizel: if ($content['sizel'] == 0) {goto endsizes;} else {echo 'Large: "'.$content['sizel'].'"';} endsizes: mysql_query ($querycontent); } $query = "INSERT INTO orders (id, cartcontent) VALUES (null, '$foobar')"; mysql_query ($query); mysql_close($link); ?> Quote Link to comment https://forums.phpfreaks.com/topic/250355-unexpected-t_foreach/ Share on other sites More sharing options...
trq Posted November 3, 2011 Share Posted November 3, 2011 This line: $query = "INSERT INTO orders (id, cartcontent) VALUES (null, '". is incomplete. As for your second piece of code. foreach does not return anything. Your code makes little sense. Quote Link to comment https://forums.phpfreaks.com/topic/250355-unexpected-t_foreach/#findComment-1284528 Share on other sites More sharing options...
Logician Posted November 3, 2011 Author Share Posted November 3, 2011 This line: $query = "INSERT INTO orders (id, cartcontent) VALUES (null, '". is incomplete. No, it is complete here: ."')"; Quote Link to comment https://forums.phpfreaks.com/topic/250355-unexpected-t_foreach/#findComment-1284531 Share on other sites More sharing options...
Logician Posted November 3, 2011 Author Share Posted November 3, 2011 Sorry i am a little confused. What do you mean the second code returns nothing? Quote Link to comment https://forums.phpfreaks.com/topic/250355-unexpected-t_foreach/#findComment-1284533 Share on other sites More sharing options...
trq Posted November 3, 2011 Share Posted November 3, 2011 You cannot put a foreach in the middle of an echo. Explain exactly what it is your trying to do. It's likely you don't even need the SELECT statements. As it stands, your code makes little sense. Quote Link to comment https://forums.phpfreaks.com/topic/250355-unexpected-t_foreach/#findComment-1284534 Share on other sites More sharing options...
trq Posted November 3, 2011 Share Posted November 3, 2011 Sorry i am a little confused. What do you mean the second code returns nothing? You where trying to assign the result of a foreach to a variable. foreach does not return any result. Quote Link to comment https://forums.phpfreaks.com/topic/250355-unexpected-t_foreach/#findComment-1284536 Share on other sites More sharing options...
Logician Posted November 3, 2011 Author Share Posted November 3, 2011 I am trying to put all $_SESSION['cart']['content'] information into 1 MySQL table. Quote Link to comment https://forums.phpfreaks.com/topic/250355-unexpected-t_foreach/#findComment-1284538 Share on other sites More sharing options...
trq Posted November 3, 2011 Share Posted November 3, 2011 Not enough information I'm afraid. Quote Link to comment https://forums.phpfreaks.com/topic/250355-unexpected-t_foreach/#findComment-1284540 Share on other sites More sharing options...
Logician Posted November 3, 2011 Author Share Posted November 3, 2011 I see, I did not know this! If foreach cannot be used inside an echo (even when braking out of the echo), I will use this code bellow. But the same result still returns in error. so, I think the answer lies closer to you saying that foreach is not returning anything. I will investigate further. <?php session_start(); $link = mysql_connect('localhost', 'testusr', 'testpw'); mysql_select_db('testdb', $link); $foobar = foreach ($_SESSION['cart']['content'] as $content) { $querycontent = "select * from products where id='{$content['id']}'"; $result = mysql_query($querycontent); $row = mysql_fetch_array($result); if ($content['sizes'] == 0) {goto sizel;} else {echo 'Small: "'.$content['sizes'].'"';} sizel: if ($content['sizel'] == 0) {goto endsizes;} else {echo 'Large: "'.$content['sizel'].'"';} endsizes: mysql_query ($querycontent); } $query = "INSERT INTO orders (id, cartcontent) VALUES (null, '$foobar')"; mysql_query ($query); mysql_close($link); ?> Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/250355-unexpected-t_foreach/#findComment-1284541 Share on other sites More sharing options...
Logician Posted November 3, 2011 Author Share Posted November 3, 2011 Ok, so I have lots of stuff in $_SESSION['cart']['content']... $_SESSION['cart']['content']['1'] $_SESSION['cart']['content']['2'] $_SESSION['cart']['content']['3'] etc etc.......... Inside these session things, there is an array. What I want to do is to dump all of the arrays inside a MySQL table called "cartcontent". To do this, I have tried this failed method: $foobar = foreach ($_SESSION['cart']['content'] as $content) { $querycontent = "select * from products where id='{$content['id']}'"; $result = mysql_query($querycontent); $row = mysql_fetch_array($result); if ($content['sizes'] == 0) {goto sizel;} else {echo 'Small: "'.$content['sizes'].'"';} sizel: if ($content['sizel'] == 0) {goto endsizes;} else {echo 'Large: "'.$content['sizel'].'"';} endsizes: mysql_query ($querycontent); } $query = "INSERT INTO orders (id, cartcontent) VALUES (null, '$foobar')"; What I have tried to do with the above, is get the dumping of data to occur by assigning it as $foobar, where foobar is the trigger at which point the information will be dumped - into the MySQL table called "cartcontent". Quote Link to comment https://forums.phpfreaks.com/topic/250355-unexpected-t_foreach/#findComment-1284545 Share on other sites More sharing options...
Logician Posted November 3, 2011 Author Share Posted November 3, 2011 Would this work?. $query = "INSERT INTO orders (id, cartcontent) VALUES (null, "'.include 'foobar.php'.'")"; ... where foobar.php is the foreach argument. Quote Link to comment https://forums.phpfreaks.com/topic/250355-unexpected-t_foreach/#findComment-1284549 Share on other sites More sharing options...
Logician Posted November 3, 2011 Author Share Posted November 3, 2011 LOL EVEN BETTER! A dirty trick! $_SESSION['order']['contentdump'] = foreach() I will try it and report. ... and I am also going to try and even more disgusting trick, dumping the information into a text file, then including that text file, then deleting it... all in a single PHP script.. hey! What ever works! Quote Link to comment https://forums.phpfreaks.com/topic/250355-unexpected-t_foreach/#findComment-1284551 Share on other sites More sharing options...
trq Posted November 3, 2011 Share Posted November 3, 2011 You need something like: foreach ($_SESSION['cart']['content'] as $content) { foreach ($content as $data) { $query = "INSERT INTO orders (id, cartcontent) VALUES (null, '$data')"; mysql_query($query); } } Quote Link to comment https://forums.phpfreaks.com/topic/250355-unexpected-t_foreach/#findComment-1284563 Share on other sites More sharing options...
Logician Posted November 3, 2011 Author Share Posted November 3, 2011 Well, all those attempts totally failed and I am so pissed off right now. I just broke my 4 day streak of good eating habbits (I don't wanna use the work diet!) over the stress of this problem. Fuckin awesome shit NOT! Thorpe, what you suggested looks really good and I am confident your suggestion will work. I have a few other things I want to try out first, so that strategy is now on que for trial. I will report back the outcome. Quote Link to comment https://forums.phpfreaks.com/topic/250355-unexpected-t_foreach/#findComment-1284584 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.