0

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

Attachments menuPermsTbl.PNG 24.21 KB menuTbl.PNG 46.09 KB
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by diafol
0

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.

Edited by diafol: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.