Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar Mark Forums Read
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 10-15-2005, 02:13 PM   #1
mrthumbs
salad tossing sig guy
 
mrthumbs's Avatar
 
Join Date: Apr 2002
Location: mrthumbs*gmail.com
Posts: 11,702
php wizards i have a question

Imagine i have and object with 50 values:

$my_obect->value_1
$my_obect->value_2
$my_obect->value_3
etc etc..

I want to add all values:
$total = $my_object->value_1 + $my_object->value_2 etc etc..

Is there a faster way to do it?

Replacing the number for a var ($my_object->value_$i or {$i} etc) in a while loop doesnt work as it would with normal vars..
mrthumbs is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-15-2005, 02:19 PM   #2
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
I don't really use much OOP with PHP.....but if you used an array instead, you could just use the function array_sum()
__________________
Skype variuscr - Email varius AT gmail
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-15-2005, 02:24 PM   #3
mrthumbs
salad tossing sig guy
 
mrthumbs's Avatar
 
Join Date: Apr 2002
Location: mrthumbs*gmail.com
Posts: 11,702
Quote:
Originally Posted by Varius
I don't really use much OOP with PHP.....but if you used an array instead, you could just use the function array_sum()
What i really need to check if value_1 is the highest value of the 50..

so value_1 > then v2,v3,v4,v5,v6 etc..

Thats why i need a way to loop through all these vars without having
50 lines of text..

I could move the data to an array and loop through there but i still need
to compare the vars individually..

Hmm.. i could load it into an array.. do a sort.. and see if the highest number
equals value_1 AND is unique in the array....
mrthumbs is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-15-2005, 02:30 PM   #4
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
Quote:
Originally Posted by mrthumbs
What i really need to check if value_1 is the highest value of the 50..

so value_1 > then v2,v3,v4,v5,v6 etc..

Thats why i need a way to loop through all these vars without having
50 lines of text..

I could move the data to an array and loop through there but i still need
to compare the vars individually..

Hmm.. i could load it into an array.. do a sort.. and see if the highest number
equals value_1 AND is unique in the array....
To do it with a loop, you could do something like:
PHP Code:
$high_value $array[0];
$high_key 0;
foreach (
$array as $key=>$value) {
    if (
$value $high_value) { 
        
$high_value $value;
        
$high_key $key;
    }

However since their are only 50 items, yes sort might be faster.
__________________
Skype variuscr - Email varius AT gmail
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-15-2005, 02:37 PM   #5
mrthumbs
salad tossing sig guy
 
mrthumbs's Avatar
 
Join Date: Apr 2002
Location: mrthumbs*gmail.com
Posts: 11,702
Quote:
Originally Posted by Varius
To do it with a loop, you could do something like:
PHP Code:
$high_value $array[0];
$high_key 0;
foreach (
$array as $key=>$value) {
    if (
$value $high_value) { 
        
$high_value $value;
        
$high_key $key;
    }

However since their are only 50 items, yes sort might be faster.
im going to try that yes.. im on it.. thankie!
mrthumbs is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 10-15-2005, 03:35 PM   #6
nakki
Confirmed User
 
Join Date: May 2001
Location: http://topadultblogs.com/
Posts: 137
Hey, this is how I'd do it:

Code:
/*
 * Gets the name of the member variable with the highest value
 *
 * Returns the name of the variable or false on failure
 */
function getHighestVariableName($obj) {
	if (!is_object($obj)) return false;
	
	// get all the variables of the class
	$values = get_object_vars($obj);
		
	$field = false;
	$highest = null;
	
	// loop through the variables, try to find the highest one
	foreach ($values as $key => $value) {
		if ($value > $highest) {
			$highest = $value;
			$field = $key;
		}
	}
	
	return $field;
}
Of course, this won't work if you only want to count certain member variables, not all of them. But I'm sure it could be modified to do that too. :]
__________________
- adult blog aggregator - sign up here
NastyLittle.com - trade with an established adult blog
nakki is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks
Thread Tools



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.