[SOLVED]while-loop, do until false

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

[SOLVED]while-loop, do until false

Post 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.
Last edited by vigge89 on Mon Jul 26, 2004 1:59 pm, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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;

?>
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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:
Post Reply