Invalid argument supplied for foreach()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Invalid argument supplied for foreach()

Post by ghadacr »

I got this error that is preventing the for loop from working:

Warning: Invalid argument supplied for foreach() in C:\on line 11
line 11 is

Code: Select all

foreach ($_GET['HotelRoomID'] as $selected_hotel) {

Code: Select all

<?PHP

$HotelRoomID = $_GET['HotelRoomID'];
$AvailableFrom = $_GET['AvailableFrom'];
$dat = str_replace("/","-",$AvailableFrom);
$newdate = date ("d M Y", strtotime ($dat));
$confirmed = 1;

       foreach ($_GET['HotelRoomID'] as $selected_hotel) { 
            


	   $query = mssql_init ("sp_ConfirmRoom");

	   mssql_bind($query, "@HotelRoomID", $selected_hotel, SQLINT2);

	   mssql_bind($query, "@AvailableFrom", $newdate , SQLVARCHAR);

	   mssql_bind($query, "@confirmed", $confirmed, SQLVARCHAR);

	   	if (($result = mssql_execute($query)) === false)

	   	{
    		die('Could not execute the query ' . $query );

			}

}


?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Are you sure you gave it an array?
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

Yeah i have the [] in the hidden form....
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

So the URL had one or more of "HotelRoomID[]=1234" in it?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

What does

Code: Select all

echo '<pre>';
var_dump(isset($_GET['HotelRoomID']));
echo '<br />';
var_dump($_GET['HotelRoomID']);
echo '</pre>';
yield?
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

The anwser is:

Code: Select all

bool(true)
string(5) "Array"
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That's a string, not an array.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

You may want to get what you are getting with $_GET by simply outputting it and may also want to try the test is_array within your code

Code: Select all

if ((!empty($_GET['HotelRoomID'])) && (is_array($_GET['HotelRoomID']))) {
  foreach ($_GET['HotelRoomID'] as $selected_hotel) {
     $query = mssql_init ("sp_ConfirmRoom");
     mssql_bind($query, "@HotelRoomID", $selected_hotel, SQLINT2);
     mssql_bind($query, "@AvailableFrom", $newdate , SQLVARCHAR);
     mssql_bind($query, "@confirmed", $confirmed, SQLVARCHAR);
     if (($result = mssql_execute($query)) === false) {
          die('Could not execute the query ' . $query );
     }
   }
} else {
  echo "BLEEP  - Not array or empty";
}
I don't know what you are passing but bear in mind $_GET does have a character length limitation.
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

Thanks for the code everyone.... i think i found what the problem but i'm not sure how to resolve it when the array is passed it goes into a script, where it will check which button is pressed and then redirects it to a certain page that corresponds with the button..

I dont think the header is passing the array. here is the URL i'm using:

Code: Select all

header('Location: http:///con_selection.php?HotelRoomID='.$_GET[HotelRoomID].'&AvailableFrom='.$AvailableFrom.'');
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

What array... Is $AvailableFrom the array ?

If it is you need to process it more. serialize may be of use but adds bloat to the get and you may find it goes over the $_GET size limitation. You would also need to use urlencode

The question I have to ask (from the limited information available) is why you need to pass the array and what is it. Could you get the array in the pages you currently process it or possibly use another common solution which is to store the array in a session variable so you don't have to pass it around.
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

$AvailableFrom is not the array its $HotelRoomID. $HotelRoomID are basically ID numbers within a database..$AvailableFrom only needs to be passed once, and is within the $_GET limits...(I checked)
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

If you can always assume that the $HotelRoomID is an array of numbers why not use implode to produce a string.

Code: Select all

$ids=urlencode(implode(',',$_GET['HotelRoomID']));
header('Location: http:///con_selection.php?HotelRoomID='.$ids.'&AvailableFrom='.$AvailableFrom.'');
exit;
Once you have it as a get (comma separated numbers) you could then use explode to reconvert it into an array.

Code: Select all

$hotel_ids=explode(',',$_GET['HotelRoomID']);
foreach ($hotel_ids as $value) {
....
}
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

Thanks CoderGoblin that worked....Cheers
Post Reply