Jump to content

unexpected T_FOREACH


Logician

Recommended Posts

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);
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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".

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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);
  }
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.