Jump to content

Recommended Posts

Can't seem to wrap my head around this query.

I have a table of locations. Each location has a parent location represented by the parents unique ID on the same table. An example would be

Miami > Florida > United States > Northern Hemisphere > Earth > Milky Way [top level and does not have a parent]

ID | NAME | PARENT

2  | Florida | 18

5  | Miami | 2

18 | United States | 58

 

What I want to do is display a the table like this

Name | Parent

Miami | Florida

Phoenix | Arizona

 

How do I join the Name of the parent on locations.Parent = locations.ID

 

any input appreciated

Link to comment
https://forums.phpfreaks.com/topic/139345-solved-query-help/
Share on other sites

I think you have to do a multiple query with a recursive function. I do not think it is possible with 1 query. Or just do one query and use PHP to traverse the array returned and order it like you want.

 

Here is an example of what I used to accomplish this:

 

<?php
/*
* My attempt at a category script.
*/

/*
create table categories (
catid INT(11) NOT NULL auto_increment,
parentid INT(11) NOT NULL default '0',
catname varchar(50) NOT NULL,
catseoname varchar(50) NOT NULL,
disporder INT(3) NOT NULL default '0',
primary key(catid)
);
*/

mysql_connect("localhost", "root", "");
mysql_select_db("cat");

/*
my test data
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('0', 'Test1', 'test1', '0');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('1', 'Test1-1', 'test1-1', '0');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('1', 'Test1-2', 'test1-2', '1');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('5', 'Test2-1', 'test2-1', '0');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('0', 'Test2', 'test2', '1');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('5', 'Test2-3', 'test2-2', '2');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('6', 'Test2-2-2', 'test2-2-2', '1');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('6', 'Test2-2-1', 'test2-2-1', '0');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('5', 'Test2-3', 'test2-2', '2');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('7', 'Test2-2-2-1', 'test2-2-2-1', '1');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('7', 'Test2-2-2-2', 'test2-2-2-2', '0');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('7', 'Test2-2-2-3', 'test2-2-2-3', '0');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('3', 'Test1-2-1', 'test1-2-1', '1');
insert into categories (`parentid`, `catname`, `catseoname`, `disporder`) VALUES ('3', 'Test1-2-2', 'test1-2-2', '0');

*/

function retrieveCategoryList($parentid=false) {
if ($parentid === false) {
	$query = mysql_query("SELECT catid, parentid, catname, catseoname, disporder FROM categories WHERE parentid = 0 ORDER BY disporder, catname");
}else {
	$query = mysql_query("SELECT catid, parentid, catname, catseoname, disporder FROM categories WHERE parentid = " . $parentid . " ORDER BY disporder, catname");
}
$numRows = mysql_num_rows($query);

$catArray = "none";
if ($numRows > 0) {
	$catArray=array();
	while ($row = mysql_fetch_assoc($query)) {
		$catArray[$row['catname']]['id'] = $row['catid'];
		$catArray[$row['catname']]['parentid'] = $row['parentid'];
		$catArray[$row['catname']]['catseoname'] = $row['catseoname'];
		$catArray[$row['catname']]['catname'] = $row['catname'];
		$catArray[$row['catname']]['disporder'] = $row['disporder'];
		$catArray[$row['catname']]['subcats'] = retrieveCategoryList($row['catid']);
	}
}

return $catArray;
}

function displayCats($cat=false, $charDisp=0) {
if (!$cat) {
	if ($charDisp != 0) {
		$cats = retrieveCategoryList($charDisp);
	}else {
		$cats = retrieveCategoryList();
	}
	foreach ($cats as $cat) {
		displayCats($cat);
	}
}else {
	echo str_repeat("_", $charDisp) . $cat['catname'] . "<br />";
	if ($cat['subcats'] != "none") {
		foreach ($cat['subcats'] as $value) {
			displayCats($value, $charDisp+1);
		}
	}
}
}

displayCats(false, 6);

echo "<pre>";
// Lets just pull out a set of categories.
print_r(retrieveCategoryList());
?>

Link to comment
https://forums.phpfreaks.com/topic/139345-solved-query-help/#findComment-728827
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.