Jump to content

variable not yet defined


Brendan

Recommended Posts

function main(){

      $sql=mysql_query("$sql_query");  

      if(!empty($sql)){

          while($row=mysql_fetch_array($sql)){

              foreach($array as $key => $value){

                print $array[$key][value];

              }

          }

      }

}

 

$array[1][value]=$row[value];

$array[2][value]='test';

 

I want array[1] to return the content of the MySql field "value" while array[2] just return the word "test" however the array is set outside of the mysql loop (and it has to be since the function main() is included from a different file)

 

The code I have shown above doesn't work since $row[value] isn't yet defined. How would I go about doing this without including the array anywhere in the main() function?

 

Link to comment
https://forums.phpfreaks.com/topic/191925-variable-not-yet-defined/
Share on other sites

I have tried that, such as with the following script:

 

function main($array){

      $sql=mysql_query("$sql_query");                 

      if(!empty($sql)){

          while($row=mysql_fetch_array($sql)){

              foreach($array as $key => $value){

                print $array[$key][value];

              }

          }

      }

}

 

$array[1][value]=$row[value];

$array[2][value]='test';

 

However it is still the same result, the array was still created before it is passed to the function so the $row[value] was still interpreted as empty before it reached the function.

somwhere you would define your array say as $myarray

 

then call main($myarray);

 

if you wanted an array to be returned then you would return an array, or you could defune your function to work on the original array by passing by reference

 

in which case your function definition changes to

 

function main(&$array)

{

}

 

 

The problem with the reference is I would still have to create/define the array within the main() function. I want the main function to be more flexible, which means I need to define the array outside of the main() function because the array will vary depending on what I am calling the function for.

 

The whole point of the function is so I can have a function that lists information from a mysql database and puts it into an html table format. Sometimes I will want to, for example, explode a date and location and put them together in one table row element (<td>). So in the example script on the post above, for example, I would want to insert a custom function into the foreach loop, from outside of the main() function, that will use the sql while loop's variables even though i'm creating a custom function or array outside of the main() function. So I want to reference variables that technically aren't yet created when I define the variable outside of the main() function, but are there when I call them inside of the foreach loop.

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.