Page 1 of 1

Invalid argument supplied for foreach()

Posted: Mon Aug 20, 2007 9:55 am
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 );

			}

}


?>

Posted: Mon Aug 20, 2007 10:03 am
by feyd
Are you sure you gave it an array?

Posted: Mon Aug 20, 2007 10:17 am
by ghadacr
Yeah i have the [] in the hidden form....

Posted: Mon Aug 20, 2007 10:24 am
by feyd
So the URL had one or more of "HotelRoomID[]=1234" in it?

Posted: Mon Aug 20, 2007 10:25 am
by John Cartwright
What does

Code: Select all

echo '<pre>';
var_dump(isset($_GET['HotelRoomID']));
echo '<br />';
var_dump($_GET['HotelRoomID']);
echo '</pre>';
yield?

Posted: Mon Aug 20, 2007 10:27 am
by ghadacr
The anwser is:

Code: Select all

bool(true)
string(5) "Array"

Posted: Mon Aug 20, 2007 10:28 am
by feyd
That's a string, not an array.

Posted: Mon Aug 20, 2007 10:29 am
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.

Posted: Mon Aug 20, 2007 10:55 am
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.'');

Posted: Mon Aug 20, 2007 10:58 am
by VladSun

Posted: Mon Aug 20, 2007 11:11 am
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.

Posted: Mon Aug 20, 2007 11:18 am
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)

Posted: Mon Aug 20, 2007 11:31 am
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) {
....
}

Posted: Mon Aug 20, 2007 11:43 am
by ghadacr
Thanks CoderGoblin that worked....Cheers