NameSilo

Making a multi-level list from mysql database

SpaceshipSpaceship
Watch

alecgorge

New Member
Impact
0
I want to make a unordered list like this...
Code:
<ul>
<li>asdfas1
     <ul>
          <li>asdf2</li>
     </ul>
</li>
<li>asdf3</li>
</ul>
...from a mysql table like this:
Code:
| id | name | parent |
| 1  | asdf1|        |
| 2  | asdf2| 1      |
| 3  | asdf3|        |

Does that make sense?

Thanks so much!
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
the easier (and slower/more taxing on system) method would be to have multiple queries. for that, you'd do "SELECT * FROM `table` WHERE `parent`='1'" and just do a loop through all of the parent values.

the more complex way would be to read everything to a huge multidimensional array and using different loops (foreach, while, etc) make it so it displays the array values how you want it. this can be REALLY difficult/frustrating, though, so only try it if you're good at... computer science (like following/generating computer logic).
 
0
•••
it is for a cms so i need it to be the second one

nevermind i fixed it!

here is the code:
Code:
	public function navigation ($ul_id ="ul-navigation", $sub_ul_class ="ul-sub-navigation", $lis = "selected", $link = true, $one_row = false, $array = false) {
		$sql = "SELECT * FROM ".DBTABLEPREFIX."content_pages ORDER by orderingid";
		$run = mysql_query($sql);
		if($one_row == true) {
			$r .= "\n<ul id=\"".$ul_id."\">";
			while ($rec = mysql_fetch_assoc($run)) {
				$r .= "\n\t<li";
				if($id == $rec['id']) {
					$r .= " class='".$lis."'";
				}
				$r .= ">";
				if($link == true) {
					$r .= "<a href='".BASE_URL."content/".make_clean_url($rec['title'])."/".$rec['id']."'>";
				}
				$r .= $rec['menutitle'];
				if($link == true) {
					$r .= "</a>";
				}
				$r .= '</li>';
			}
			$r .= "\n</ul>\n";		
			return $r;	
		}
		while ($rec = mysql_fetch_assoc($run)) {
			if($rec['parent_id'] != "0") {
				$blacklist[] = $rec['id'];
			}
		}
		$sql = "SELECT * FROM ".DBTABLEPREFIX."content_pages ORDER by orderingid";
		$run = mysql_query($sql);
			if($array == true) {
				while ($rec = mysql_fetch_assoc($run)) {
					$r[$rec['menutitle']] .= $rec['title']."&&||&&".$rec['content'];
					
				}
			}
		$r .= "\n<ul id=\"".$ul_id."\">";
		while ($rec = mysql_fetch_assoc($run)) {
			if($rec['parent_id'] == "0") {
				$r .= "\n\t<li";
				if($page_id == $rec['id']) {
					$r .= " class='".$lis."'";
				}
				$r .= ">";
				if($link == true) {
					$r .= "<a href='".BASE_URL."content/".strtolower(str_replace(" ", "-",sanitize_alpha_numeric_space($rec['title'])))."/".$rec['id']."'>";
				}
				$r .= $rec['menutitle'];
				if($link == true) {
					$r .= "</a>";
				}
				$this->x = $this->x + 1;
				$r .= $this->navigationParent($rec['id'], $sub_ul_class, $lis, $link).'</li>';
			}
		}
		$r .= "\n</ul>\n";		
		return $r;
	}
	public function navigationParent ($parentid = HOME_PAGE_ID, $class="ul-sub-navigation", $lis = "selected", $link = true, $array = false) {
		$sql = "SELECT * FROM ".DBTABLEPREFIX."content_pages WHERE parent_id = ".$parentid." ORDER by orderingid";
		$run = mysql_query($sql);
		$x = mysql_num_rows($run);
		if($x !== 0) {
			$r .= "\n\t\t<ul class='".$class."'>";
		}
		while ($rec = mysql_fetch_assoc($run)) {
			$r .= "\n\t\t\t<li";
			if($page_id == $rec['id']) {
				$r .= " class='".$lis."'";
			}
			$r .= ">";
			if($link == true) {
				$r .= "<a href='".BASE_URL."content/".strtolower(str_replace(" ", "-",sanitize_alpha_numeric_space($rec['title'])))."/".$rec['id']."'>";
			}
			$r .= $rec['menutitle'];
			if($link == true) {
				$r .= "</a>";
			}
			$r .= $this->navigationParent($rec['id'], $class, $link).'</li>';
		}
		if($x !== 0) {
			$r .= "\n\t\t</ul>\n\t";
		}
		return $r;
	}
 
0
•••
Dynadot — .com TransferDynadot — .com Transfer
Appraise.net

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back