• 0

PHP While Loops and Functions


Question

I've been trying various ideas for getting something like this to work. Most of them end up in a infinite loop...

Imagine the following;

<?php
while ($row = mssql_fetch_row(mssql_query("$query"))) {
	print_r($row);
}
?>

Just like mssql_fetch_row returns a array for each row found. How can I accomplish the same thing in my do_something function.

IE; do_something would take some parameters given to it and end up returning say 5 arrays, just like mssql_fetch_row would return 5 outputs of $row when it find 5 records.

<?php
	function do_something($this,$that) {
		//outputs a number of things
	}
	while ($temp = do_something($this,$that)) {
		echo "$temp"; //output each number of those things separately as the $temp variable to manipulate no set way
	}
?>

I realize this could be done using a foreach instead of a while loop, where the function creates a nested array and your foreach uses that function and goes through each array, etc. Im curious to know how the mssql_fetch_row(result) does this.

:-/

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

You need to ensure that your function will return FALSE when there is no more output. Doing that will ensure the while loop eventually evaluates to false, thus terminating it.

Link to comment
Share on other sites

  • 0

You need to ensure that your function will return FALSE when there is no more output. Doing that will ensure the while loop eventually evaluates to false, thus terminating it.

Just read your reply as I figured it out. Let me know if this looks about right. Its functional and outputs 1-100 everytime its run.

<?php
	function do_somthing() {
		//setup a array to initially store your information
		static $temp = array();
		//store the information that the function will output during each loop
		static $count;
		while ($count <= 100) {
			array_push($temp,$count);
			$count++; //at end of initialization $count = 101, so this is no longer run and will not be reset till call of function loop is returned to false and unset.
		}
		//start looping through each time the function is called
		if ($var = next($temp)) {
			return($var); //return current value
		}
		else {
			//cleanup
			unset($temp);
			unset($count);
			return false;  //no value to return
		}
	}
	while ($temp = do_somthing()) {
		echo "$temp<br/>";
	}
?>

Link to comment
Share on other sites

  • 0

I am curious as to what you are actually trying to accomplish, mate, but if you simply want to iterate a set number of times then a for loop is much better than a while loop.

for ($i = 0; $i < 100; $i++) {
	echo $i;
}

You also don't need to declare static on variables, and $array[] = 'value' is faster than array_push($array, 'value') if you are only adding one element.

As for printing 1-100...

echo implode(', ', range(1, 100));

;)

Link to comment
Share on other sites

  • 0

You also don't need to declare static on variables

Actually, in the case of the function as written, it is necessary otherwise the array will get destroyed and re-created on each call of the function, thus making an infinite loop where 1 is printed over and over.

As for the printing of 1-100, this was just an example, a proof of concept. He's trying to emulate the *_fetch_row() function behaviour more or less.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.