Computing.Net > Forums > Web Development > PHP code problem, foreach loop

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

PHP code problem, foreach loop

Reply to Message Icon

Name: robber2
Date: October 26, 2007 at 18:54:12 Pacific
OS: w xp home
CPU/Ram: 2 gig celeron, 256 MB
Product: dell dim 2350
Comment:

Laler, Help!
I can't figure this one out at all. Look at the warning message on this page. Seems the values are not passed to the array. Code pasted below, let me know if you need more.

http://perfectionconstruction.com/L...

the loop:

is_array($_POST["Liked"]);
foreach($_POST["Liked"] as $k)
{
$message .= '<span style="text-decoration:underline; font-weight:bold">Liked</span>' . ': ' .

stripslashes($_POST['$k']);
}

And it pertains to this html:

Please indicate what you thought of this web site:<br />
<input type="checkbox" name="Liked[ ]" value="Navigation" />It's easy to navigate the links to the pages on

the site.<br />
<input type="checkbox" name="Liked[ ]" value="Validates" />It's well formed and displays nicely in my

browser.<br />
<input type="checkbox" name="Liked[ ]" value="Informs" />It's informative and/or educational.<br />
<input type="checkbox" name="Liked[ ]" value="Grahics" />It has nice pictures of your work.<br /><br />



Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: October 26, 2007 at 23:53:59 Pacific
Reply:

I suspect you are getting that error because when you first navigate to that page there is no POST data. So the foreach fails beacause $_POST["liked"] is not an array.

This line is_array($_POST["Liked"]); does absolutely nothing as it is shown. is_array() returns true/false based upon whether the value is an array, but you have nothing to act on it, so it returns true or false and then the script continues.

I think you also have a problem with this $_POST['$k'] since it appears you should be just using $k

Try this:

if (is_array($_POST['Liked'])) {
foreach($_POST['Liked'] as $k)
{
$message .= '<span style="text-decoration:underline; font-weight:bold">Liked</span>: ' .
stripslashes($k);
}

}

Michael J


0

Response Number 2
Name: robber2
Date: October 27, 2007 at 09:43:29 Pacific
Reply:

Thank you, sir, that works out groovy. I'm back on the road again!
As I understand it, now the "if" part, when the page loads, is finding that it's not an array, so it doesn't execute the foreach. Then when the array is populated, it does run the foreach(?) and that's why there's no more warning message when the page loads?


0

Response Number 3
Name: Laler
Date: October 27, 2007 at 11:41:08 Pacific
Reply:

True!

And if we look at our previous conversation, isset() can also be used.

This:

if (isset ($_POST['Liked'])) {

...means, if the array $_POST['Liked'] is set / defined.

--

foreach(?)

foreach($_POST['Liked'] as $k) means:

"Loop through every element in the array $_POST['Liked'], and put the value into variable $k."


---
Fubar


0

Response Number 4
Name: robber2
Date: October 27, 2007 at 17:00:19 Pacific
Reply:

Again, much gratitude for the clear, concise instruction. Please know that I appreciate the help!


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: PHP code problem, foreach loop

php code problem www.computing.net/answers/webdevel/php-code-problem/4091.html

simple php codes www.computing.net/answers/webdevel/simple-php-codes/449.html

AIM and PHP code for web design www.computing.net/answers/webdevel/aim-and-php-code-for-web-design/1001.html