Not an array / Invalid argument supplied for foreach() in...

I’m creating code that requires a lot of arrays that I have a seperate file creating all of the arrays.
[php]// These arrays are universal
$server = array(“Game Server” => “game”, “Voice Server” => “voice”, “Web Server” => “web”, “Virtual Hard Drive” => “hd”);

// These arrays are for the game servers
$g_game = array(“Battle Field 2 -> $0.00” => “bf2”, “Battle Field 2142 -> $0.00” => “bf2142”, “Counter-Strike 1.6 -> $0.00” => “cs1.6”,
“Counter-Strike Condition Zero -> $0.00” => “cscz”, “Counter-Strike Source -> $0.00” => “cs:s”, “Day of Defeat Source -> $0.00” => “dod:s”);
$g_type = array(“Public” => “public”, “Private” => “private”);
$g_tick = array(“33 Tick -> $0.00” => 33, “66 Tick -> $0.00” => 66, “100 Tick -> $0.00” => 100);

// These arrays are for the public game server slots
$g_slot_pub = array(“10 Slots -> $20.00” => 10, “12 Slots -> $23.75” => 12, “14 Slots -> $27.50” => 14, “16 Slots -> $31.25” => 16,
“18 Slots -> $35.00” => 18, “20 Slots -> $39.75” => 20, “22 Slots -> $43.50” => 22, “24 Slots -> $47.25” => 24,
“26 Slots -> $51.00” => 26, “28 Slots -> $54.75” => 28, “30 Slots -> $59.50” => 30, “32 Slots -> $63.25” => 32,
“34 Slots -> $69.00” => 34, “36 Slots -> $71.75” => 36, “38 Slots -> $74.50” => 38, “40 Slots -> $79.25” => 40,
“42 Slots -> $83.00” => 42, “44 Slots -> $86.75” => 44, “46 Slots -> $90.50” => 46, “48 Slots -> $94.25” => 48,
“50 Slots -> $99.00” => 50, “52 Slots -> $102.75” => 52, “54 Slots -> $106.50” => 54, “56 Slots -> $110.25” => 56,
“58 Slots -> $114.00” => 58, “60 Slots -> $118.75” => 60, “62 Slots -> $122.50” => 62, “64 Slots -> $126.25” => 64,
“66 Slots -> $130.00” => 66, “68 Slots -> $133.75” => 68, “70 Slots -> $138.50” => 70, “72 Slots -> $142.25” => 72,
“74 Slots -> $146.00” => 74, “76 Slots -> $149.75” => 76, “78 Slots -> $153.50” => 78, “80 Slots -> $158.25” => 80,
“82 Slots -> $162.00” => 82, “84 Slots -> $165.75” => 84, “86 Slots -> $169.50” => 86, “88 Slots -> $173.25” => 88,
“90 Slots -> $178.00” => 90, “92 Slots -> $181.75” => 92, “94 Slots -> $185.50” => 94, “96 Slots -> $189.25” => 96,
“98 Slots -> $193.00” => 98, “100 Slots -> $197.75” => 100);
[/php]
This is only part of it, but at this point these are the ones having problems. PHP aknowledges $server as an array, but the rest of them aren’t array as PHP states. I use the code…[php]foreach($xxxxx as $key => $value)[/php]… To call all of the arrays on other scripts I’ve created. Like I said, I can use $server, but the rest aren’t arrays.

I’ve checked all of the arrays and recoded them a few times. What am I doing wrong. When I run the scripts, I get the error

Invalid argument supplied for foreach() in C:www.website.comwwwhostingincludeidf.general.php

Admin Edit: Changed QUOTE tags to PHP (selectively)

I think because you are using what appears to be money as values, you are running into a problem. Instead of using the double quote " use the single quote to separate the information. Since the dollar sign $ is generally used as part of a variable name (a special character) you would either need to use the single quotes or escape the $ in the array.

$server does not have any element that has a dollar sign in it that is why that worked ok.

I changed all double quotes to singles like you said and I’m still getting the errors. To add, I used some code to see if the array is an array:
[php]
if(is_array($server)) echo ‘$server IS AN ARRAY’; else echo ‘$server IS NOT A VALID ARRAY!!!’;
if(is_array($g_type)) echo ‘$g_type IS AN ARRAY’; else echo ‘$g_type IS NOT A VALID ARRAY!!!’;
if(is_array($g_tick)) echo ‘$g_tick IS AN ARRAY’; else echo ‘$g_tick IS NOT A VALID ARRAY!!!’;
if(is_array($g_slot_pub)) echo ‘$g_slot_pub IS AN ARRAY’; else echo ‘$g_slot_pub IS NOT A VALID ARRAY!!!’;
[/php]

When I used that block of code, it told me that none of the four were arrays! Not even $server! Here is all of my code. Some it’s not all of it because it’s all not done; but what is done so far should allow me to do a certain action.

idf.general.php
[php]
function disp_game_form()
{
// This is activated when the customer wants a game server
?>

Game: 


<option value=“order.php?process=packages” <?php if(!$_SESSION['game']) echo 'selected="selected"' ?>>Choose Game
<?php
if(is_array($server)) echo ‘$server IS AN ARRAY’; else echo ‘$server IS NOT A VALID ARRAY!!!’;
if(is_array($g_type)) echo ‘$g_type IS AN ARRAY’; else echo ‘$g_type IS NOT A VALID ARRAY!!!’;
if(is_array($g_tick)) echo ‘$g_tick IS AN ARRAY’; else echo ‘$g_tick IS NOT A VALID ARRAY!!!’;
if(is_array($g_slot_pub)) echo ‘$g_slot_pub IS AN ARRAY’; else echo ‘$g_slot_pub IS NOT A VALID ARRAY!!!’;
foreach($g_game as $key => $value)
{
echo ‘<option value="order.php?process=packages&game=’.$value.’"’;
if($_SESSION[‘game’] == $value) echo ‘selected=“selected”’;
echo ‘>’.$key.’’;
}
?>



<?php
if($_GET[‘game’] || $_SESSION[‘game’])
{
if($_GET[‘game’])
$_SESSION[‘game’] = $_GET[‘game’];
?>
Tick: 


<option value=“order.php?process=packages” <?php if(!$_SESSION['tick']) echo 'selected="selected"' ?>>Choose Tickrate
<?php
foreach($g_tick as $key => $value)
{
echo ‘<option value="order.php?process=packages&tick=’.$value.’"’;
if($_SESSION[‘tick’] == $value) echo ‘selected=“selected”’;
echo ‘>’.$key.’’;
}
?>



<?php
if($_GET[‘tick’] || $_SESSION[‘tick’])
{
if($_GET[‘tick’])
$_SESSION[‘tick’] = $_GET[‘tick’];
?>
Type: 


<option value=“order.php?process=packages” <?php if(!$_SESSION['type']) echo 'selected="selected"' ?>>Choose Server Type
<?php
foreach($g_type as $key => $value)
{
echo ‘<option value="order.php?process=packages&type=’.$value.’"’;
if($_SESSION[‘type’] == $value) echo ‘selected=“selected”’;
echo ‘>’.$key.’’;
}
?>



<?php
if($_GET[‘type’] || $_SESSION[‘type’])
{
if($_GET[‘type’])
$_SESSION[‘type’] = $_GET[‘type’];
switch(@$_SESSION[‘type’])
{
case “public”:
?>
Slot: 


<option value=“order.php?process=packages” <?php if(!$_SESSION['slot']) echo 'selected="selected"' ?>>Choose Slot Amount
<?php
foreach($g_slot_pub as $key => $value)
{
echo ‘<option value="order.php?process=packages&slot=’.$value.’"’;
if($_SESSION[‘slot’] == $value) echo ‘selected=“selected”’;
echo ‘>’.$key.’’;
}
?>



<?php
break;
case “private”:
?>
Slot: 


<option value=“order.php?process=packages” <?php if(!$_SESSION['slot']) echo 'selected="selected"' ?>>Choose Slot Amount
<?php
foreach($g_slot_priv as $key => $value)
{
echo ‘<option value="order.php?process=packages&slot=’.$value.’"’;
if($_SESSION[‘slot’] == $value) echo ‘selected=“selected”’;
echo ‘>’.$key.’’;
}
?>



<?php
break;
default:
break;
}
if($_GET[‘slot’] || $_SESSION[‘slot’])
{
if($_GET[‘slot’])
$_SESSION[‘slot’] = $_GET[‘slot’];
if($_SESSION[‘type’] == “private”)
{
if($_GET[‘s_pass’])
$_SESSION[‘s_pass’] = $_GET[‘s_pass’];
?>
Server Password: [When you are done, press enter](10 Character Max) 




<?php
}
if($_SESSION[‘type’] == “private” && $_SESSION[‘s_pass’] || $_SESSION[‘type’] != “private”)
{
if($_GET[‘r_pass’])
$_SESSION[‘r_pass’] = $_GET[‘r_pass’];
?>
Rcon Password: [When you are done, press enter](10 Character Max) 




<?php
}
if($_SESSION[‘r_pass’])
{
// The following doesn’t turn into $_SESSION unless the customer chooses yes then selects their mods
?>
Do you want to add MODs to your server? 



No
Yes



<?php
}
if($_GET[‘mods’] == “yes”)
{
?>
Select all MODs that you would like installed:


<?php
$counter = 0;
foreach($g_mod as $key => $value)
{
echo ‘’.$key.’
’;
$counter++;
}
?>



<?php
if($_POST[‘mods’]) // WARNING! ‘mods’ is an array
$_SESSION[‘mods’] = $_POST[‘mods’];
}
?>


<?php
if($_GET[‘mods’] == “no” || $_SESSION[‘mods’])
{
?>

<?php
}
?>


















<?php
}
}
}
}
}
[/php]

order.php
[php]

<?php // Name: order.php // Desc: This file is designed to allow customers to order service. This file is a holder file which calls include files for forms, functions, and situations based on what is selected from the switch statement. // These are the global functions for this website include("../include/idf.general.php"); // Start a session session_start(); // THE switch statement switch($_GET['process']) { case 'account': include_once('include/idf.contactform.php'); break; case 'packages': include_once('include/idf.packageform.php'); break; case 'payment': include_once('include/idf.paymentform.php'); break; default: include_once('include/idf.agreement.php'); break; } ?>

[/php]

idf.packageform.php
[php]

<?php /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Name: idf.packageform.php // // Desc: This file displays options of packages that customers can choose from and based on what the customer chooses, the // // form will then display secondary options that correspond with the options chosen. This form uses the $_GET array // // and the $_SESSION array to allow the customer to fully choose his/her plan. // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Start a session session_start(); // These are the include files include("idf.general.php"); include("idf.forminfo.php"); // Display the begining HTML for this portion of the form do_html_header("Hosting Order Form -> Packages"); do_html_style(); ?>
Please read carefully before you choose your package.
This page allows you to choose your packages desired. As you choose options, more options will apear that you can choose from. If you want to get another game, voice, or web server, click Add Another Server and do the process over again. At any time, you can calculate the order by pressing the Calculate Total button. When you are done and satisfied with what you want to order, press the Continue button.


<?php // WARNING!!! The following uses JumpMenus to alternate the options available to the customer based on what is chosen. DO NOT change anything below unless you want // to change the entire form. if(is_array($_SESSION['server'])) { // Insert block of code that displays previous order if customer clicked Add Another Server } ?> <?php if($_GET['server'] || $_SESSION['server']) { if($_GET['server']) $_SESSION['server'] = $_GET['server']; switch(@$_SESSION['server']) { case "game": disp_game_form(); break; case "voice": disp_voice_form(); break; case "web": disp_web_form(); break; default: break; } } ?> [/php]

I don’t know why it’s not working. Your way, in theory, should have worked but it didn’t.

Server Type:  >Choose Server Plan <?php foreach($server as $key => $value) { echo ''.$key.''; } ?>

I would really appreciate if someone could help me. To add, when I initially call idf.packageform.php, all of the variables like $server, $g_type, $g_tick, as well as $g_slot_pub are all arrays. It’s just when I call the script again, none of the variables are arrays anymore. I’ve never heard of anything like this happening before.

On a quick glance through I couldn’t find the line where you included those arrays, how is that handled?

In addition, (again, based on a quick look through) it seems like you mix the php and HTML quite a bit. While this can be usefull, it can also be confusing at times. In many (maybe all?) of your forms you have this…

As near as I can tell you are not in PHP on those lines, you’re in straight HTML, so the $PHP_SELF variable won’t be loading. Again, I could be wrong, but those are the starting points I came up with.

The file gets called at the line include(“idf.forminfo.php”); I believed I fixed it by creating local arrays and filling them with the arrays from the file idf.forminfo.php so instead of the arrays being called one at a time as the user goes down the form, they are all called at once and the local variable gets called. It appears to work.

Sponsor our Newsletter | Privacy Policy | Terms of Service