1

I have created a php file to retrieve the privileges of each user against their db data. And the code display it as a drop down box. The code below is used to retrieve and display it.

//session_start();
include('db.php');
//include('../CheckSession.php');

$userid=$_SESSION['UserID'];
echo'<ul class="dropdown"><li><a href="index.php">Home</a></li>';

$sql = "select privilege.privilege_catagory from privilege inner join user_privilege on privilege.privilege_id=user_privilege.privilege_id where user_privilege.UserID=$userid group by privilege.privilege_catagory ";
$result= mysql_query($sql) or die(mysql_error());
$num_row=mysql_num_rows($result);
if($num_row!=0)
{
    while($rb=mysql_fetch_array($result))
    {
        $p_cat=$rb['privilege_catagory'];
        echo'<li><a href="#">'.$p_cat.'</a>';

        $sql2 = "SELECT privilege.url_Location,privilege.option_Name FROM privilege inner join user_privilege on privilege.privilege_id=user_privilege.privilege_id WHERE privilege.privilege_catagory='$p_cat' AND user_privilege.UserID=$userid" ;
                //$sql2 = "SELECT privilege.url_Location,privilege.option_Name FROM privilege inner join user_privilege on privilege.privilege_id=user_privilege.privilege_id  WHERE user_privilege.UserID =$userid";
        $result2= mysql_query($sql2) or die(mysql_error());
        $num_row2=mysql_num_rows($result2);
        if($num_row2!=0)
        {
            echo'<ul class="sub_menu">';
            while($rb2=mysql_fetch_array($result2))
            {
                echo'<li><a href="'.$rb2['url_Location'].'">'.$rb2['option_Name'].'</a></li>';
            }
                echo'</ul>';
         }
         echo'</li> ';
    }

    mysql_free_result($result);      
} 
else
{

}
echo'</ul>';
?>

The Db Table is as below:- privilege_id, privilege_name, privilege_catagory, url_Location, option_Name (this is what to be displayed in drop down menu)

I include this php page in the index.page. Every thing works fine there. But when the page redirect to any other page the url of drop down is concatenate with the current url e.g. when i redirect to localhost/demo/index.php to localhost/demo/Admission/NewAdmission.php it works

but when i click on home('index.php' is the url value) from there it goes to : localhost/demo/Admission/index.php !!

Funny. But i am in serious trouble. All the attention is warm welcomed. Thanks in advance.

Bob
  • 1,489
  • 1
  • 16
  • 28
Abel Jojo
  • 758
  • 3
  • 14
  • 30
  • 1
    Please, stop using the `mysql_*` functions. – Dan Lugg Jun 30 '12 at 14:39
  • @Bracketworks why not use mysql_* functions? What other choice do we have? – Rajat Feb 24 '13 at 18:45
  • 1
    @r20rock For starters, the big red box at [the `mysql_query()` page](http://www.php.net/manual/en/function.mysql-query.php), and furthermore, the far superior API offered by [PDO](http://www.php.net/manual/en/book.pdo.php) – Dan Lugg Feb 24 '13 at 22:38

3 Answers3

3

try by giving base url of your site in href.

echo'<li><a href="'.$base_url.$rb2['url_Location'].'">'.$rb2['option_Name'].'</a></li>';
Ashu
  • 166
  • 2
  • This answer is correct; the href "index.php" is a _relative_ URL. You need to put an _absolute_ URL for the href, meaning that it must start with "/", like "/index.php" – Mark Eirich Jun 30 '12 at 13:39
1

The reason is you are using relative links in href attribute. If you use

<a href="new.php">Links</a>
and you are on a page with url 'http://sample/demo/index.php' then yafte you click on the 'Links', your new URL will be 'http://sample.com/demo/index.phpnew.php'!!! which you can avoid by using absolute url like <a href="http://sample.com/demo/new.php">Links</a>. You should use a constant or variable in PHP which can store the base URL of you website and then echo it in all the links so that if you change the domain of your website, you don't have to go to each link and alter the domain name.
Rajat
  • 313
  • 1
  • 8
  • 19
0

Thanks to all your support. Sorry guys, I made the code work. It was actually an logical error by placeing the navigator php file for php in some other folder. I placed the 'Useraccount.php' {dropdown code} in the main folder and made '../' for all links.

Once again thanks for all.

Abel Jojo
  • 758
  • 3
  • 14
  • 30