Page 1 of 1

function - using an if statement(true/false), not working

Posted: Mon Mar 05, 2007 7:53 pm
by duncanwilkie
OK; probably some simple solution to this, but I really can't quite get this right.

I'm attempting to create a function that I can call on, after some manipulation (done that bit).

The question is:
Why is the Original NOT the same as the function?
The boolean value $alternateRows does not change? - Therefore not changing the colour of the cells in that row.

Additional Question:
Is there a way to change boolean expressions to the opposite of its current value?
In a simplified form similar to +1 as ++.


The Original; which works:

Code: Select all

// While - Display all results
	while($myrow = $result->fetchRow()) {														


// Alternate Row colours
	if($alternateRows==false) {
		echo '<tr><td>';
		$alternateRows=true;
	}
	elseif($alternateRows==true)	{
		echo '<tr class="altrowcolor"><td>';	// altrowcolor class selected here; under "true" for alternateRows.
		$alternateRows=false;
	}
					
	echo $myrow[1] . '</td><td>';		// 
	echo $myrow[15] . '</td><td>';
	echo $myrow[3] . '</td><td>';
	echo $myrow[5] . '</td><td>';
	echo $myrow[6] . '</td><td>';
	echo $myrow[7] . '</td><td>';
	echo $myrow[8] . '</td><td>';
	echo $myrow[9] . '</td><td>';
	echo $myrow[10] . '</td><td>';
	echo $myrow[11] . '</td><td>';
	echo $myrow[12] . '</td><td>';
	echo $myrow[13] . '</td><td>';
	echo $myrow[14] . '</td>';

}	// end While statement
So, why doesn't this work the same?

Code: Select all

// Function displays the table data, needs to be called more than once.
	function displayResults($alternateRows, $myrow)	{
	// Alternate Row colours
		if($alternateRows==false) {	
			echo '<tr><td>';
			$alternateRows=true;
		}
		elseif($alternateRows==true)	{
			echo '<tr class="altrowcolor"><td>';	// altrowcolor class selected here; under "true" for alternateRows.
			$alternateRows=false;
		}		
		echo $myrow[1] . '</td><td>';	 
		echo $myrow[15] . '</td><td>';
		echo $myrow[3] . '</td><td>';
		echo $myrow[5] . '</td><td>';
		echo $myrow[6] . '</td><td>';
		echo $myrow[7] . '</td><td>';
		echo $myrow[8] . '</td><td>';
		echo $myrow[9] . '</td><td>';
		echo $myrow[10] . '</td><td>';
		echo $myrow[11] . '</td><td>';
		echo $myrow[12] . '</td><td>';
		echo $myrow[13] . '</td><td>';
		echo $myrow[14] . '</td>';
	}

// While - Display all results
	while($myrow = $result->fetchRow()) {	
		displayResults($alternateRows, $myrow);
	}

Re: function - using an if statement(true/false), not workin

Posted: Mon Mar 05, 2007 8:37 pm
by Christopher
You are changing the value of $alternateRows inside the function, so you need some way to maintain the value while looping. There are two ways. Passing a reference:

Code: Select all

// Function accepts a reference to the value
	function displayResults(&$alternateRows, $myrow)	{
	}

// While - Display all results
	while($myrow = $result->fetchRow()) {	
		displayResults($alternateRows, $myrow);
	}
Or returning the value:

Code: Select all

// Function returns value
	function displayResults($alternateRows, $myrow)	{
              return $alternateRows;
	}

// While - Display all results
	while($myrow = $result->fetchRow()) {	
		$alternateRows = displayResults($alternateRows, $myrow);
	}
The first may look cleaner, but the second clearly communicates to any programmer looking at the loop what is going on -- without having to see the function.

Posted: Tue Mar 06, 2007 3:46 am
by onion2k
I'd use a static variable.

Posted: Tue Mar 06, 2007 3:46 am
by duncanwilkie
Thank you.

All I needed was the ampersand in front of $alternateRows
I knew there was some reason I wasn't maintaining the value, thats helped a lot.

The reason I put the whole of the function in, was that I thought there may be a flaw in the way I'd done it, I'll take note though.

Posted: Tue Mar 06, 2007 4:52 am
by onion2k
Passing a reference to an undeclared variable means that the first time the function is called you'll initialise a variable that's only used inside one function in the global scope. In my opinion that's a bad idea. It'll work, but it's polluting the code. In another situation you might end up with some rather weird results. If you come to use $alternateRows in another section of the same script you won't know what it's value is (it'll depend on the number of times displayResults() has been called).

I'd still recommend using a static instead.

Code: Select all

function displayResults($myrow) {
  static $alternateRows; //$alternateRows will retain it's value between calls to displayResults()
  //do stuff
}

while($myrow = $result->fetchRow()) {   
  $alternateRows = displayResults($myrow);
}

Posted: Tue Mar 06, 2007 5:10 am
by duncanwilkie
OK Brilliant, thanks.

That makes sense, I hadn't read about static variable scope before, but now I have, and I think I understand it.