Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Ty Schenk
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Ty Schenk
iOS Development Techdegree Graduate 16,269 Points

PHP foreach issues. Need help Please

Hello,

i am needing this for each loop to work but i can't figure it out.

Cant do code markdown because it was giving weird symbols

foreach($ _POST["site"] as $ key=>$value) { $site=$ value; $tag=$ _POST["tag"][$key]; $object=$ _POST["object"][$key]; $graph=$ _POST["graph"][$key]; $sql = "INSERT INTO $client . Sites (Name, Tag, Graph, Object) VALUES ('$ ite', '$tag', '$object', '$graph')"; mysql_query($sql); }

1 Answer

Kevin Korte
Kevin Korte
28,149 Points

Is $_POST['site'] an array?

Lets start at the beginning. Can you var_dump($_POST['site']) before the loop so we can see what it actually contains for info to work with.

Ty Schenk
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Ty Schenk
iOS Development Techdegree Graduate 16,269 Points

Yes it is an array. it is html inputs but because they are dynamic html inputs i had to put them into a array. here is the dump from $_POST['site'] : array(5) { [0]=> string(6) "Site 1" [1]=> string(6) "Site 1" [2]=> string(6) "Site 2" [3]=> string(7) "Site 2 " [4]=> string(6) "Site 3" }

Kevin Korte
Kevin Korte
28,149 Points

What's the error, I'm trying to think of the best way to test this? Couple things I noticed is you have an extra space when you state $site=$ value. Also I noticed in your sql statement, you state '$ ite' when I think you meant '$site'

Ty Schenk
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Ty Schenk
iOS Development Techdegree Graduate 16,269 Points

it won't let me reply to your most recent comment for some reason. The $site=$ value is not really there in the code its only there on treehouse for some reason. so we don't have to keep replying and we can both see it - i have the full script on codeshare : http://codeshare.io/jGzQ9

Kevin Korte
Kevin Korte
28,149 Points

Cool, that helps! Forum has been acting weird lately.

Let me play with it and see if I can help.

Ty Schenk
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Ty Schenk
iOS Development Techdegree Graduate 16,269 Points

Hey i looked a the codeshare and saw a few changes so i tested them to see if they worked and it didn't work out. it ok though because have a idea. What if i combine the 4 arrays that i have into one ?

array(3) { [0]=> string(6) "Site 1" [1]=> string(6) "Site 1" [2]=> string(6) "Site 2" }

array(3) { [0]=> string(5) "Tag 1" [1]=> string(5) "Tag 2" [2]=> string(5) "Tag 1" }

array(3) { [0]=> string(1) "V" [1]=> string(1) "V" [2]=> string(1) "V" }

array(3) { [0]=> string(3) "Yes" [1]=> string(3) "Yes" [2]=> string(3) "Yes" }

That is what my array var_dump looks like so here is my idea, can i combine all the 0's , 1's , 2's and so on? This would give me the the output of :

array(4) { [0]=> string(6) "Site 1" [1]=> string(5) "Tag 1" [2]=> string(1) "V" [3]=> string(3) "Yes" }

I could then take that output and put it into my script somehow like so :

foreach(*** No idea what to do here***) { 

$sql = "INSERT INTO `$client` . `Sites` (Name, Tag, Graph, Object) VALUES ('$row')"; 

// The $row would be the new array that i displayed earlier.

mysql_query($sql); 

}

No idea if this would actually work or not but eh i hope so :). Thanks let me know your thoughts and how i could get this done.