Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 06-24-2008, 02:25 PM
AsscBB AsscBB is offline
 
Join Date: Dec 2007
Posts: 20
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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
}
Reply With Quote
  #2  
Old 06-24-2008, 02:27 PM
MoT3rror MoT3rror is offline
 
Join Date: Mar 2007
Posts: 423
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You forgot
PHP Code:
global $db
in your function.
Reply With Quote
  #3  
Old 06-24-2008, 07:27 PM
Opserty Opserty is offline
 
Join Date: Apr 2007
Posts: 4,103
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #4  
Old 06-24-2008, 07:41 PM
AsscBB AsscBB is offline
 
Join Date: Dec 2007
Posts: 20
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #5  
Old 06-25-2008, 08:26 AM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

What type of variable is $my_values? It seems to be an array.

PHP Code:
    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 
$db->fetch_array() needs a MySQL resource as parameter, not a simple array.
Reply With Quote
  #6  
Old 06-25-2008, 12:22 PM
AsscBB AsscBB is offline
 
Join Date: Dec 2007
Posts: 20
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

$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!
Reply With Quote
  #7  
Old 06-25-2008, 01:29 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

$my_values must be a valid MySQL resource if used with fetch_array().

Maybe you should post more code then only this function.
Reply With Quote
  #8  
Old 06-25-2008, 08:11 PM
AsscBB AsscBB is offline
 
Join Date: Dec 2007
Posts: 20
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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:
PHP Code:
$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

I'm positive that I've included all of the code to figure out if what I'm trying is possible or not, anything else would just be noise. If "valid MySQL resource" means something other than this piece of code, then I'm afraid you've lost me.

Sorry I missed the php tags in the original post and grabbed the code ones instead.


Thanks again for your help and patience!
Reply With Quote
  #9  
Old 06-26-2008, 07:49 AM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Actually that is a resource, not an array. You will however need to pass the resource by reference, not by value.
Reply With Quote
  #10  
Old 06-26-2008, 06:51 PM
AsscBB AsscBB is offline
 
Join Date: Dec 2007
Posts: 20
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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!
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:04 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04557 seconds
  • Memory Usage 2,262KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (3)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete