Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm having trouble getting the array to work right, let me show you the code here

function scanArray(){
$al = sizeof($_userLoaders);
echo $al. "<br />";
for ($tr = 0; $tr <= $al; $tr++) {
echo  "value: " .$tr. "<br />";
echo $_userLoaders[$tr];


}



}

//Fetch user's loaders.

$_userLoaders;

function get_user_loaders(){ 

         $con = connectToMySQL();//connects to my_sql

        mysql_select_db("my_database", $con);//connect database

        $t = mysql_query("SELECT * FROM someTable
        WHERE value_a=".$_SESSION['value_a']." AND value_b=someValue");
        $x= 0;
        //lets loop through the results and create an array to compare later.
            while ($result = mysql_fetch_array($t)){
             $_userLoaders[$x] = $result['value_c'];
                $x++;
            }
        //lets get all the options for
        print_r($_userLoaders);//this part prints what it should
scanArray();
}

okay, I minimized the code above to show you what's going on. Pretty much function get_user_loaders() works great. It fetches data from a table in a database, and returns what it should. Second, it makes an array out of it. Again, this part works great. When the print_r() method is called it prints what it should, here's an example of what it prints:

Array ( [0] => tableValue )

yes, at this point it only has one value, please note that this value can vary from no values to 100 values which is why I am using an array. In this case, i'm testing it with one value.

Now, once I call scanArray() It doesn't echo the values.

the scanArray() function echoes the following:

0
value: 

so what I don't understand is why does it print it out, but it doesn't display the function? Thanks in advance.

share|improve this question
    
$_userLoaders is not defined in scanArray and you have an error there - for ($tr = 0; $tr <= $al - 1; $tr++) { –  botzko Mar 10 '12 at 22:47
    
Ahh! Good catch! Thank you, I fixed that. –  Felipe Tadeo Mar 10 '12 at 22:53

2 Answers 2

up vote 2 down vote accepted

Your problem is that $_userLoaders variable is declared outside the function and function scanArray knows nothing about it. You need to either pass that variable in as a parameter:

function scanArray($_userLoaders) {
    ...
}

with the call at the end being

scanArray($_userLoaders);

or alternatively declare the variable as global inside the function:

function scanArray($_userLoaders) {
    global $_userLoaders;
    ...
}
share|improve this answer
    
Ahh, I see. I was used to declaring it outside of the function, like in javaScript, and having it automatically be global. I didn't know that you had to declare it as a global variable. Well thank you to both of you gentlemen. I think I'll pass it through the parameters since there are a lot of other php pages that will be working together. Thanks again! –  Felipe Tadeo Mar 10 '12 at 22:50
    
@FelipeTadeo If you're happy with the results, then please accept one of the answers. –  Aleks G Mar 11 '12 at 13:10
    
Sorry about that, I thought I had selected it! –  Felipe Tadeo Mar 12 '12 at 19:30

That would be because $_userLoaders is not equal to anything inside your scanArray() function. While it's not good practice, you can add the line:

global $_userLoaders;

to your scanArray() function and every other function that uses that global variable then, and it should work.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.