PHP creating an horizontal menu

December 2016


Issue


I'm trying to create an horizontal menu in which items are displayed according to the user.
For example the admin will have access to all the menu and a typical user will have a limited choice.

Solution


If all the user's information are stored in a database.
You only use this information as it can certainly identify the type od users (simple member, admin, etc ...)

Example:
In the database, a column type identifies the type of membership by a numerical value.
1 = simple member
2 = moderator
3 = administrator

The menu is composed of me:
<ul> 
<li> <a href="page1.php"> Page 1 </ a> </ li> 
<li> <a href="page2.php"> Page 2 </ a> </ li> 
<li> <a href="page3.php"> Page 3 </ a> </ li> 
<li> <a href="moderation.php"> Moderation </ a> </ li> 
<li> <a href="admin.php"> Administration </ a> </ li> 
</ Ul> 

The moderator must have access to the administrator link Moderation and this one and the next.
Just use a query to retrieve the member type from the database and then use a condition.

<? Php 
$ Sql ??= 'SELECT * FROM members WHERE id_membre = "'. Mysql_real_escape_string ($ _SESSION ['id_membre']).'"'; 
$ Req = mysql_query ($ sql) or die ('Error SQL! <br />'. $ Sql. '<br />'. Mysql_error ()); 
$ Data = mysql_fetch_array ($ req); 

echo '<ul> 
<li> <a href="page1.php"> Page 1 </ a> </ li> 
<li> <a href="page2.php"> Page 2 </ a> </ li> 
<li> <a href="page3.php"> Page 3 </ a> </ li> '; 
if ($ data ['type']==' 2' or $ data ['type']==' 3') 
{Echo '<a <li> href="moderation.php"> Moderation </ a> </ li>';} 
if ($ data ['type']==' 3') 
{Echo '<a <li> href="admin.php"> Administration </ a> </ li>';} 
</ Ul> '; 
?> 


I suppose in this example that the id of the member is contained in a session variable.
$Data ['type'] retrieves the type of membership contained in the database and the condition checks its value menu and displays the type of membership.

Thanks to Arthezeus for this tip.

Related :

This document entitled « PHP creating an horizontal menu » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.