I currently have a table that holds among other things:
ID | pagetitle | parent
1 | Page 1 | 0
2 | Page 2 | 1
3 | Page 3 | 2
I am trying to select all the pages, along with their parent’s pagetitle.
Currently I am looping through the results in PHP, grabbing the pagetitles for those pages with parents, and it looks pretty ugly:
function showPageParent($pageid) {
do {
$result = mysql_query("SELECT parent FROM pages WHERE id=" . qt($pageid));
while($row = mysql_fetch_array($result)) {
$crumbs[] = $row['parent'];
$pageid1 = $row['parent'];
}
} while($pageid1!=0);
sort($crumbs);
foreach($crumbs as $crumb) {
$result = mysql_query("SELECT id,pagetitle FROM pages WHERE id=" . $crumb);
while($row = mysql_fetch_array($result)) {
$out .= $row['pagetitle'] . " > ";
}
}
if(count($crumbs) < 2) {
return showPageTitle($pageid);
} else {
return $out;
}
}
It works, but it is very old code, I am in the processes of re-writing a LOT of this sytem, and would love to pretty this thing up with one call to the database to return something like this:
ID | pagetitle | parent-pagetitle
Is this possible using inner selects?
edit
I’d like to point out, I am not the original author of the PHP, I am aware it’s poorly written :p
You can add another join for each level of depth, if needed.