Member Avatar for doctorphp

Hey everyone.

I am working on a website for a friend and I have finished designing the website and have now started developing it.

What I am trying to do but am having problems with is making a menu that uses two tables in the database. menu and menuPerms. Below are the table structures.

menu

menuPerms


Below is my code but it doesn't work and I am not too sure about what to do.

//menu
$menuQ = $db->query( "SELECT * FROM `menu` ORDER BY `sort` ASC" );
while( $menu = $menuQ->fetch_assoc() ) {
  
  $autoId = $menu['autoId'];
  $class = $menu['class'];
  $href = $menu['href'];
  $id = $menu['id'];
  $onClick = $menu['onClick'];
  $target = $menu['target'];
  $title = $menu['title'];			  
  $text = $menu['text'];
  
  if( $class != '' ) {
	  $class = ' class="' . $class . '"';  
  }
  
  if( $href != '' ) {
	  $href = ' href="' . $href . '"';  
  } else {
	  $href = ' href="#"';					  
  }
  
  if( $id != '' ) {
	  $id = ' id="' . $id . '"';					  
  }
  
  if( $onClick != '' ) {
	  $onClick = ' onClick="' . $onClick . '"';					  
  }
  
  if( $target != '' ) {
	  $target = ' target="' . $target . '"';					  
  }
  
  if( $title != '' ) {
	  $title = ' title="' . $title . '"';					  
  } else {
	  $title = ' title="' . $text . '"';				  
  }
  
  //menu perms
  $menuPermsQ = $db->query( "SELECT * FROM `menuPerms` WHERE `menuId` = '" . $autoId . "'" );
  $menuPerms = $menuPermsQ->fetch_assoc();
  
  $menuLoggedIn = $menuPerms['loggedIn'];
  $menuLoggedOut = $menuPerms['loggedOut'];
  $menuAdmin = $menuPerms['admin'];
  
  if( $menuLoggedOut == 1 && isLoggedIn() == 0 ) {
	echo '<li><a' . $class .''. $href .''. $id .''. $onClick .''. $target .''. $title .'>' . $text . '</li>';  
  }
}


Any help would be very much appreciated.
Thanks in advance

Member Avatar for diafol

Maybe like this?

if(isLoggedIn()==2){ //admin user??
	$where = "p.loggedIn = 1 AND p.admin = 1";
}elseif(isLoggedIn()==1){ //normal user
	$where = "p.loggedIn = 1 AND p.admin = 0";
}elseif(isLoggedIn()==0){ //not logged in
	$where = "p.loggedOut = 1";
}

$menuQ = $db->query("SELECT m.class,m.href,m.id,m.onClick,m.target,m.title,m.text FROM menu AS m INNER JOIN menuPerms AS p On m.autoId = p.menuId WHERE $where ORDER BY m.sort ASC");
$output = "";
while( $menu = $menuQ->fetch_assoc() ) {
	$kr = array_slice(array_keys($menu),0,6); //get the first 6 keys of the array (class, href,id,onClick,target,title
	$attributes = "";
	foreach($kr as $v){
		$attributes .= " $v = \"{$menu[$v]}\"";	
	}
	$output .= "<li><a$attributes>{$menu['text']}</li>";
}

...

echo "<ul>$output</ul>";

Obviously not tested. ALso a bit untidy, but you'll get the idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.