The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
#1
|
|||
|
|||
Problem with passing query results to a function
Please check my process and help me understand where I'm going wrong.
I'm taking the results from a query and passing them to a function where I'm deconstructing the info in a while loop and running conditionals. Is this the right way to do this? I have comments in the code for what I think the code is doing (or at least what I want it to do) so I can also put this in my cheat sheet for future use. The code is not currently providing the output I desire. The problem I'm seeing is that I don't think the while loop is scanning through the array like I assume it is. Am I going crazy??? Thanks! Code:
function check_fields($my_values) //this is my custom function with my array as a pass parameter. The name does not need to be the same but I'm leaving it the same for sake of simplicity { while ($my_value=$db->fetch_array($my_values) //this while loop goes through the array entry by entry. If there were 10 instances of field5 = $some_var from earlier, this code would be executed 10 times - once for each entry { if($my_value['field1'] == $field1_var) //This goes to the array and runs a conditional against "field1". The name of the fields here corresponds with the name of the fields in the origional table { //Do something for this if true } if($my_value['field2'] == $field2_var) //Same as before but for a different table { //Do something for this if true } if($my_value['field3'] == $field3_var) { //Do something for this if true } if($my_value['field4'] == $field4_var) { //Do something for this if true } if($my_conditional == true) { return false; //if $my_conditional = true then retun a value of "false" to the calling function } else { return true; //if $my_conditional = false then retun a value of "true" to the calling function } } } $my_values=$db->query("SELECT 'field1','field2','field3','field4' FROM " . TABLE_PREFIX . "demo_table WHERE field5=".$some_var); //$my_values is now an array that is 4 fields wide and has as many rows as the number of occurances of field5 = $some_var if (check_fields($my_values)) //This sends the $my_values array to a custom function and expects a true or false as a return value { //Do something } |
#2
|
|||
|
|||
You forgot
PHP Code:
|
#3
|
|||
|
|||
Why are you putting your while inside the function? It makes more sense to loop through and apply the function to each array returned by the mysql.
|
#4
|
|||
|
|||
I tried to boil the code down to support asking a general question. The reason I'm doing the while inside the function, is becuase thats where the data is being used, and calling the function and just passing those parameters doesn't work with my actual application which is calling the function with parameters which were omitted in this example/question. Suffice it to say, it makes the most sense for my application to pass the array as a parameter and deconstruct it in a while loop inside the function if that will work in php. I'm trying to minimize database queries and function calls, and while this does sound kinda kludgy, if the sytax allows, it works for my application.
|
#5
|
|||
|
|||
What type of variable is $my_values? It seems to be an array.
PHP Code:
|
#6
|
|||
|
|||
$my_values is an array build from a mySQL query before being passed into the function. I was looking at other code examples and saw similar examples of populating the array with a mySQL query, then useing fetch_array in a while loop to scan through the array.
I've added the step of passing the array as a parameter to a function and deconstructing there. I've been unable to work on my board the past few days so i don't know if this will actually work, but this was a problem I was having trouble with when I last had time to work on it, and I'm trying to learn how to do this so I don't get stumped in 5 minutes next time I work on it... Thanks for the help! |
#7
|
|||
|
|||
$my_values must be a valid MySQL resource if used with fetch_array().
Maybe you should post more code then only this function. |
#8
|
|||
|
|||
The query where $my_values is populated and the call to the function is located in the chunk of code below the function. Prior to that point, $my_values does not exist.
Quote:
Sorry I missed the php tags in the original post and grabbed the code ones instead. Thanks again for your help and patience! |
#9
|
|||
|
|||
Actually that is a resource, not an array. You will however need to pass the resource by reference, not by value.
|
#10
|
|||
|
|||
That may explain why I couldn't get it to work going through the array. I've gotten some ideas from "passing references" over on php.net to try now thanks to that suggestion and I'll see where that gets me. I'll post back with results my next chance to play!
Thanks Marco! |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|