0

I want a menu showing links/buttons depending on the permissions of the account. And I don't want to copy/paste this code to every page. I use an if-statement to check if the link/button will show for the user who is logged in at the moment. How can I like create a variable or function to just print the menu that is on another PHP file?

Like this menu:

<div id="menu">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
    <a href="#">Link 4</a>';

    if(condition) {
        echo '
        <a href="#">Link 5</a>';
    }

</div>
5
  • Just use echo/print in 'menu.php' and then include it where I need it in the code? Will that print the code? Commented Aug 30, 2013 at 18:05
  • 1
    either include or store you links in an array and loop over the array. Commented Aug 30, 2013 at 18:06
  • Where is the condition coming from? A session variable or what? Commented Aug 30, 2013 at 18:09
  • Aah sorry, feeling a bit dumb right now.. Thx @u_mulder :) Thought I had to echo it out or something. Never tried this before, but we learn every day. :) Commented Aug 30, 2013 at 18:15
  • @gtr1971 : The condition check with the database for permission on the account. Commented Aug 30, 2013 at 18:16

1 Answer 1

2

Store your menu HTML in a file, and include it in every other page in which you want your menu to appear.

menu.php:

<div id="menu">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
    <a href="#">Link 4</a>';

    <?php
    if(condition) {
        echo '
        <a href="#">Link 5</a>';
    }
    ?>

</div>

foo.php:

<?php

include 'menu.php';

# code ...

That way, the contents of menu.php will get evaluated and be put into your foo.php file as if it were copied and pasted.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.