Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] Menu Access


[SOLVED] Menu Access

Reply
  #1 (permalink)  
Old 01-26-09, 10:06 AM
rid rid is offline
Newbie Coder
 
Join Date: Oct 2005
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy [SOLVED] Menu Access

Dear all...,
I have develop script, but I have a problem to get an access per menu item.
Every menu item have a difference level access.
The lower level is "0", and the higher level is "9". If the menu item have access "0123456789", so all user include guest, have an access to these menu item.

SQL data table:

(see attached image below)

PHP menu script (original):
PHP Code:

<?php

$_m_q 
"SELECT * FROM cogo_menu";
$_m_r mysql_query($_m_q);
if (
mysql_num_rows($_m_r) > 0) {
    
$_m '<ul>';
    while(
$_m_i mysql_fetch_row($_m_r)) {
        
$_m.= '<li><a href="'.$_m_i[2].'>'.$_m_i[1].'</a></li>';
    }
    
$_m.= '</ul>';
}

?>


PHP menu script (modified):

PHP Code:

<?php

$_m_q 
"SELECT * FROM cogo_menu";
$_m_r mysql_query($_m_q);
if (
mysql_num_rows($_m_r) > 0) {
    
$_m '<ul>';
    while(
$_m_i mysql_fetch_row($_m_r)) {
        if (
strpos($_m_i[3], $_SESSION['user_level']) === false) {
            
$_m.= '';
        } else {
            
$_m.= '<li><a href="'.$_m_i[2].'>'.$_m_i[1].'</a></li>';
        }
    }
    
$_m.= '</ul>';
}

?>
When using the modified script, all of menu items will not display.
Help me please....
Why I confused with that logical....?
Attached Images
File Type: png menutbl.png (5.6 KB, 150 views)
Reply With Quote
  #2 (permalink)  
Old 01-26-09, 12:29 PM
landing's Avatar
landing landing is offline
Coding Addict
 
Join Date: Jul 2006
Location: Scotland
Posts: 302
Thanks: 0
Thanked 0 Times in 0 Posts
strpos() will match a string. I'm assuming the value of $_SESSION['user_level'] is an integer?

Taken from the PHP manual on strpos
Quote:
If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.

Easy solution though. Just convert your user_level to a string whenever you require

PHP Code:

$_m_q "SELECT * FROM cogo_menu";
$_m_r mysql_query($_m_q);
if (
mysql_num_rows($_m_r) > 0) {
    
$_m '<ul>';
    while(
$_m_i mysql_fetch_row($_m_r)) {
        if (
strpos($_m_i[3], strval($_SESSION['user_level']))) === false) { // See strval()
            
$_m.= '';
        } else {
            
$_m.= '<li><a href="'.$_m_i[2].'>'.$_m_i[1].'</a></li>';
        }
    }
    
$_m.= '</ul>';

__________________
Always sanitise your data


Best regards
Reply With Quote
  #3 (permalink)  
Old 01-27-09, 09:08 PM
rid rid is offline
Newbie Coder
 
Join Date: Oct 2005
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
thanks landing....
please help me to solve my problem...
Reply With Quote
  #4 (permalink)  
Old 01-28-09, 06:22 AM
landing's Avatar
landing landing is offline
Coding Addict
 
Join Date: Jul 2006
Location: Scotland
Posts: 302
Thanks: 0
Thanked 0 Times in 0 Posts
I did. Look at my code.
__________________
Always sanitise your data


Best regards
Reply With Quote
  #5 (permalink)  
Old 01-28-09, 06:28 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
I think strpos() is actually smart enough to convert the integer.

I guess the error is a missing double quote, which could make the link "invisible" in the HTML output (not the actual source code).

This:
PHP Code:

$_m.= '<li><a href="'.$_m_i[2].'>'.$_m_i[1].'</a></li>'
... should be:
PHP Code:

$_m.= '<li><a href="'.$_m_i[2].'">'.$_m_i[1].'</a></li>'
Reply With Quote
  #6 (permalink)  
Old 01-28-09, 10:55 AM
rid rid is offline
Newbie Coder
 
Join Date: Oct 2005
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
dear nico, sorry, i forgot to add a double-quote, but it can't fixing my problem. none of the menu item is appear.

[sorry 4 my poor english]
Reply With Quote
  #7 (permalink)  
Old 01-28-09, 11:02 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Did you echo both, $_SESSION['user_level'] and $_m_i[3] to make sure they actually contain the expected values?
Reply With Quote
  #8 (permalink)  
Old 01-28-09, 11:18 AM
rid rid is offline
Newbie Coder
 
Join Date: Oct 2005
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
yes...
i want all menu items have a different access for different user level.
e.g: menu "Dashboard" have level access for user level: 1, 2, 3, 4, 5, 6, 7, 8, 9 but not for guest (guest level is zero "0") so these menu item is just for user with user level 1, 2, 3, 4, 5, 6, 7, 8 and 9.
Reply With Quote
  #9 (permalink)  
Old 01-29-09, 11:32 AM
landing's Avatar
landing landing is offline
Coding Addict
 
Join Date: Jul 2006
Location: Scotland
Posts: 302
Thanks: 0
Thanked 0 Times in 0 Posts
I ran your original code and as you said the menu didn't appear.

I ran it again this time strval($_SESSION['user_level']) and it worked fine.

I did forcefully set $_SESSION['user_level'] however, so as Nico says if it doesn't carry the expected value it won't work either.
__________________
Always sanitise your data


Best regards
Reply With Quote
  #10 (permalink)  
Old 01-29-09, 11:52 AM
rid rid is offline
Newbie Coder
 
Join Date: Oct 2005
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
dear landing,
i've got an error msg like below when i inserted the strval() function:
Code:
Warning:  strpos(): Empty delimiter. in W:\www\project\includes\menu.php on line 8
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Superfish jquery menu problems with modification transcend2005 CSS 2 03-25-09 01:30 AM
css problem with the scroll bar crazy.works CSS 0 11-04-08 05:34 PM
Anyone willing to help a programmer in trouble? PAID JOB! aXXo Job Offers & Assistance 1 08-26-08 12:59 AM
AnyLink Drop Down Menu - Smarty Error? shadyy510 JavaScript 2 05-08-08 11:44 AM
Xml / Dom / Css Mark_SC.SE JavaScript 0 06-29-05 08:05 AM


All times are GMT -5. The time now is 08:26 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.