Jump to content


Photo

why am i getting problems with this foreach loop


  • Please log in to reply
4 replies to this topic

#1 predator

predator
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts
  • LocationEngland

Posted 14 July 2006 - 03:15 PM

hey i got this for each loop

if($_POST['remove']){
foreach($_POST['remove'] as $k=>$v){
mysql_query("DELETE FROM sections WHERE id='$v' LIMIT 1");
}
}

and i am getting the error of

Warning: Invalid argument supplied for foreach()

the $_POST['remove'] is an array i get from an html page and it has a products id assigned to it

but i just cant work out why this is giving me an error.

any help greatly apprechiated

Regards
Mark
<a href="http://www.mandppcs.co.uk">M & P PC's Website and Software Design & Development Service's</a>

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 14 July 2006 - 03:17 PM

If you are getitng that error then $_POST['remove'] is not an array. Try this code:
echo '<pre>' . print_r($_POST['remove'], true) . '</pre>';

What does that return?

Also if you have input fields that have the same name as remove make sure you have add the square brackets after the name, eg: remove[]

#3 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 14 July 2006 - 03:18 PM

Are you sure it is an array? Make sure it has [] after the name in HTML, and use is_array in PHP to double check.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#4 predator

predator
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts
  • LocationEngland

Posted 14 July 2006 - 03:25 PM

yes it is an array the name of each check box is
remove[{$content[i].id}]

where $content is obviously the id that will be passed into each remove element.

personally i dont like this way of coding however i have taken this project over at work and cant totally re write the code as the customer needs it done obviously sooner than later

regards
Mark
<a href="http://www.mandppcs.co.uk">M & P PC's Website and Software Design & Development Service's</a>

#5 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 14 July 2006 - 05:38 PM

it would still be nice to see the input's HTML to verify that it isn't a spelling or notation error.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users