Page 1 of 1

Passing an object property to a function - can I? [solved]

Posted: Tue Apr 25, 2006 2:13 pm
by cordie
I'm such a newb that I don't know the right terminology for my question so searching has been fruitless. Here's what I want to do...

I have a recordset object and I pass that to a function. I also want to pass a property to the function, along with a value to test this against.

Within the function I loop through the recordset and if the test value == the object's property value on this iteration then I want to do something but I can't figure out how to pass in some kind of reference to the property I want to test against and the work around I have is plain clunky.

Here is what I have:

Code: Select all

function getNavListItems($rsRecordset, $iFieldToTest, $iValueToTest)
{
// Ya di ya...
	
	foreach ( $rsRecordset as $record )
	{
		$bTestResult = false;
		
		switch ($iFieldToTest)
		{
			case 0:
				if ( $record->pageId == $iValueToTest )
				{
					$bTestResult = true;
				}
				break;
			case 1:
				if ( $record->parentPageId == $iValueToTest )
				{
					$bTestResult = true;
				}
				break;
		}
		
		if ( $bTestResult )
		{
			// Do stuff
		}
		else
		{
			// Do other stuff
		}
	}
	
	return $sStuff;
}
Here is what I want!

Code: Select all

function getNavListItems($rsRecordset, $propertyToTest, $iValueToTest)
{
// Ya di ya...
	
	foreach ( $rsRecordset as $record )
	{
		if ( $record->$propertyToTest == $iValueToTest )
		{
			// Do stuff
		}
		else
		{
			// Do other stuff
		}
	}
	
	return $sStuff
}
Hope this all makes sense - TIA!

Posted: Tue Apr 25, 2006 2:26 pm
by feyd
Have you tried your "what you want" version?

Posted: Tue Apr 25, 2006 2:50 pm
by cordie
Yes - sorry I should have said :oops:

I take it that I have the right idea so I guess I'm passing the property incorrectly.

Here's the call:

Code: Select all

$sSecondaryNav = getLinkListItems($pageDetails, pageId, $iPageId);
And the error:

Code: Select all

Notice: Use of undefined constant pageId - assumed 'pageId' in C:\Documents and Settings\Cordie\My Documents\websites\localhost\www\test\includes\functions\getpagedetails.php on line 425

Posted: Tue Apr 25, 2006 2:54 pm
by feyd
put quotes around pageId, you're giving the function a string. :P

Posted: Tue Apr 25, 2006 3:00 pm
by cordie
I could have sworn I tried that already but all is working a treat now - thankyou!