Hello,

I have a menu in a webpage. I am looking to make the code reusable for another page, that way I can manage the links in one place rather than multiple page. Anyone know a way to do this?

Thanks

Drjay

Hey,

You could try using a header system (Let me explain):

Basically, have a file called: header.php which contains all the content around the where the different pages will exist (so like index.php, about.php etc) and then on each page you can call the header.php. This meaning that if you wanted to change the links then you can do this easily by just changing the header.php file and it will change every single page that has included the header..

Another solution could be to create a function:

<?php
 // links.php
function links()
{
   echo '<a href="link1.php">link 1</a>';
}
?>

<?php
   // index.php
     // include the links function
     include ('links.php');

     links();
?>

Hope this helps :)

Hello,

Thanks for the idea. I actually tried but failed to implement this since it seems I don't have PHP is working on my PC and Mac.

Do I have to start the Apache server to start the get php to work?

Because even this isn't really working for me on my browser!

<?php
    echo "Hi";
?>

Thanks,

Drjay

Yes, you need to have a webserver running with PHP enabled to be able to view this.

I recommend XAMPP since you seem like a beginner.

Just would like to add that this is a common approach for web development. Depending on the server side scripting language, the technique may be different. For example, if this site was developed in asp.net, you could use master pages.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.