Jump to content

While loop question.


ram4nd

Recommended Posts

The condition doesn't work, it does the cycle only once, but it has to make it twice:

static $last, $now;
while($last == $now) {
$last = $now;
foreach($tutorials_from_categories as $tutorial) {
$tutorials_from_categories = array_merge($tutorials_from_categories,
	$this->model->get_categories_by_parent($tutorial));
}
$tutorials_from_categories = array_unique($tutorials_from_categories);
$now = count($tutorials_from_categories);
}

My model:

function get_categories_by_parent($parent_id) {
static $return;
$this->db->select('id');
$this->db->where('parent_id', $parent_id);
$query = $this->db->get('categories');
if($query->num_rows()>0) {
	foreach($query->result() as $row) {
		$return[] = $row->id;
	}
	return $return;
}
else return array();
}

I can also post part of mysql table, but I think it's not necessary.

 

Link to comment
https://forums.phpfreaks.com/topic/192260-while-loop-question/
Share on other sites

Works like this, I don't understand the while command:

$tutorials_from_categories[0] = $category;
		static $last, $now, $temp;
		while(1) {
			$last = $now;
			$temp = $tutorials_from_categories;
			foreach($temp as $tutorial) {
				$tutorials_from_categories = array_merge($tutorials_from_categories,
					$this->model->get_categories_by_parent($tutorial));
			}
			$tutorials_from_categories = array_unique($tutorials_from_categories);
			$now = count($tutorials_from_categories);
			if($last == $now) break;
		}

Link to comment
https://forums.phpfreaks.com/topic/192260-while-loop-question/#findComment-1013571
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.