Page 1 of 1

[SOLVED]while-loop, do until false

Posted: Mon Jul 26, 2004 7:19 am
by vigge89
I've got this code for checking if the current page showing has any parent-pages available:

Code: Select all

<?php
###### get the pages parent(s)
$more['parents'] = true;
$more['pid'] = $output['pid'];

// while there's still more parents
while ($more['parents'] == true) {

	$query['parents'] = @mysql_query ("SELECT `pid`,`name` FROM `contents` WHERE `id` = '{$more['pid']}'");
	$cparent = @mysql_fetch_assoc ($query['parents']); // fetch content

	$more['parents'] = false;
	if (!empty ($cparent['name'])) {
		$output['parents'] .= "» {$cparent['name']} ";
		if (!empty ($cparent['pid'])) $more['parents'] = true;
	}

}
##### /get the pages parent(s)
?>
what i'm trying to do is to check if the current page has any parents, and if it gets a match (pid is the current pages parent page-id), it adds the name of the parent page into $output['parents'], and tries to find a parent of the parent-page. The problem is that the code i'm using above just results in an timeout. Doesn't this type of thing work? Does anyone have any idea on how to fix it?

Thanks in advance.

Posted: Mon Jul 26, 2004 7:46 am
by Weirdan
you should update $more['pid'] in your while loop. Otherwise you would fetch the same parent over an over again... resulting in a timeout.

Posted: Mon Jul 26, 2004 7:54 am
by vigge89
I know i was missing something, thanks! :D
Ok, here's the new code:

Code: Select all

<?php
###### get the pages parent(s)
$more['parents'] = true;
$more['pid'] = $output['pid'];

// while there's still more parents
while ($more['parents'] == true) {

	$query['parents'] = @mysql_query ("SELECT `pid`,`name` FROM `contents` WHERE `id` = '{$more['pid']}'");
	$cparent = @mysql_fetch_assoc ($query['parents']); // fetch content

	$more['parents'] = false;
	if (!empty ($cparent['name'])) {
		$output['parents'] .= "» {$cparent['name']} ";
		if (!empty ($cparent['pid'])) $more['parents'] = true;
		$more['pid'] = $cparent['pid'];
	}

}
##### /get the pages parent(s)
?>
The output looks like this:
» characters » snake eater » bb-bio
That's wrong though, the reall page-tree looks like this:
» snake eater » characters » bb-bio
Should i add the parents to an array and reverse it before outputting, or are there any better ways of doing it?

Posted: Mon Jul 26, 2004 10:48 am
by feyd
you can do the array, array_reverse, implode thing; or you could do a leading concatenate if you don't want to deal with arrays i.e.

Code: Select all

<?php

$blah = "whatever";
$blah = "Something else " . $blah;

?>

Posted: Mon Jul 26, 2004 1:59 pm
by vigge89
hmm, it sure was long ago sicne i last coded something, i've managed to forget the most simple things ;)
thanks feyd, you solved my problem :) :wink: