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
ghadacr
Forum Contributor
Posts: 135 Joined: Fri May 11, 2007 10:44 am
Post
by ghadacr » Mon Aug 20, 2007 9:55 am
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 );
}
}
?>
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Aug 20, 2007 10:03 am
Are you sure you gave it an array?
ghadacr
Forum Contributor
Posts: 135 Joined: Fri May 11, 2007 10:44 am
Post
by ghadacr » Mon Aug 20, 2007 10:17 am
Yeah i have the [] in the hidden form....
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Aug 20, 2007 10:24 am
So the URL had one or more of "HotelRoomID[]=1234" in it?
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Mon Aug 20, 2007 10:25 am
What does
Code: Select all
echo '<pre>';
var_dump(isset($_GET['HotelRoomID']));
echo '<br />';
var_dump($_GET['HotelRoomID']);
echo '</pre>';
yield?
ghadacr
Forum Contributor
Posts: 135 Joined: Fri May 11, 2007 10:44 am
Post
by ghadacr » Mon Aug 20, 2007 10:27 am
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Aug 20, 2007 10:28 am
That's a string, not an array.
CoderGoblin
DevNet Resident
Posts: 1425 Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany
Post
by CoderGoblin » Mon Aug 20, 2007 10:29 am
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.
ghadacr
Forum Contributor
Posts: 135 Joined: Fri May 11, 2007 10:44 am
Post
by ghadacr » Mon Aug 20, 2007 10:55 am
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.'');
VladSun
DevNet Master
Posts: 4313 Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria
Post
by VladSun » Mon Aug 20, 2007 10:58 am
There are 10 types of people in this world, those who understand binary and those who don't
CoderGoblin
DevNet Resident
Posts: 1425 Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany
Post
by CoderGoblin » Mon Aug 20, 2007 11:11 am
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.
ghadacr
Forum Contributor
Posts: 135 Joined: Fri May 11, 2007 10:44 am
Post
by ghadacr » Mon Aug 20, 2007 11:18 am
$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)
CoderGoblin
DevNet Resident
Posts: 1425 Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany
Post
by CoderGoblin » Mon Aug 20, 2007 11:31 am
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) {
....
}
ghadacr
Forum Contributor
Posts: 135 Joined: Fri May 11, 2007 10:44 am
Post
by ghadacr » Mon Aug 20, 2007 11:43 am
Thanks CoderGoblin that worked....Cheers