Jump to content

array problem


guyfromfl

Recommended Posts

I am working on a project where there is a set of the same data used over and over on multiple pages.  So i wanted to make a require_once file that populates the array whenever I need that array in a page.  I did but all that happens is a blank page (with the bgcolor i defined in the css) and my error_log doesnt have a record of the request.

I am stumped.

 

Here is the idea of what im talking about

 

the require_once (../includes/globals.php):

<?php
$reason = array (1 => "item1",
                 2 => "item2",
                 3 => "item3",
                 4 => "item4");
?>

 

one of the files that calls on it

<?php
     // Ive tried with declaring the $reason array and without it
     require_once(includes/globals.php);

/*
       content until i need to populate a 
       listbox
*/

for ($i=1; $i<=4; $i++)
{
     echo "<option>".$reason[$i]."</option>";
}

 

sounds simple enough.  everything worked great until i made this small change

 

--mark

Link to comment
https://forums.phpfreaks.com/topic/40934-array-problem/
Share on other sites

try a print_r($reason); above the <select> to be sure the variable is getting there and what the contents are. If that doesn't work, then start with the print_r in the require page...then keep moving it farther away (closer to your <select>)until you discover where the problem is.

Link to comment
https://forums.phpfreaks.com/topic/40934-array-problem/#findComment-198235
Share on other sites

i don't think this will make a difference, but it would be better in my opinion if you used a foreach loop instead of a for loop, like this:

<?php
        echo "<select name=\"drop_down_menu\">\n";

        foreach($reason as $key => $val){
                echo "<option value=\"". $key ."\">". $val ."\n";
        }

        echo "</select>\n";
?>

 

also, did anything output when you printed the contents of the array? it could be a path related issue.

Link to comment
https://forums.phpfreaks.com/topic/40934-array-problem/#findComment-198251
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.