Brendan Posted February 12, 2010 Share Posted February 12, 2010 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 More sharing options...
jl5501 Posted February 13, 2010 Share Posted February 13, 2010 you would need to pass the array, to the function, as a parameter Link to comment https://forums.phpfreaks.com/topic/191925-variable-not-yet-defined/#findComment-1011579 Share on other sites More sharing options...
Brendan Posted February 13, 2010 Author Share Posted February 13, 2010 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. Link to comment https://forums.phpfreaks.com/topic/191925-variable-not-yet-defined/#findComment-1011584 Share on other sites More sharing options...
jl5501 Posted February 13, 2010 Share Posted February 13, 2010 you have provided an array parameter, as a formal parameter in your function definition, but you are not showing your function call indicating that you are passing an actual parameter Link to comment https://forums.phpfreaks.com/topic/191925-variable-not-yet-defined/#findComment-1011589 Share on other sites More sharing options...
trq Posted February 13, 2010 Share Posted February 13, 2010 Also, your function would either need to return the array back out into the global namespace (preferred), or you would need to pass the array by reference. Link to comment https://forums.phpfreaks.com/topic/191925-variable-not-yet-defined/#findComment-1011591 Share on other sites More sharing options...
jl5501 Posted February 13, 2010 Share Posted February 13, 2010 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) { } Link to comment https://forums.phpfreaks.com/topic/191925-variable-not-yet-defined/#findComment-1011592 Share on other sites More sharing options...
Brendan Posted February 13, 2010 Author Share Posted February 13, 2010 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. Link to comment https://forums.phpfreaks.com/topic/191925-variable-not-yet-defined/#findComment-1011600 Share on other sites More sharing options...
trq Posted February 13, 2010 Share Posted February 13, 2010 You've lost me. Either way, the options given above are all that you have. Link to comment https://forums.phpfreaks.com/topic/191925-variable-not-yet-defined/#findComment-1011608 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.