Reputation: 1416
I have a website with the following setup:
overview.php hotel1.php hotel2.php hotel3.php
Where overview has a sidebar with links to hotels 1, 2 and 3 and each hotel page has a sidebar with links to the other two hotels (excluding itself) along with the overview page.
At the minute, I am hard coding the sidebar in each php page, as they are all slightly different.
I am wondering if there is a way I can code one sidebar in a separate file (sidebar.php) with links to each of the four pages and add the sidebar with a php include() function.
Then, depending on the page (which will have an identifier), show all the links, except the link to itself.
Problem is, I'm not sure how to do it, or if it can be done.
The site is php and html (with css and javascript).
And if it is relevant, I have about 100 folders containing an overview and then multiple hotels which I would like to implement this on.
Upvotes: 0
Views: 750
Reputation: 3787
The easiest way to do it is to wrap your sidebar code in a function.
sidebar.php
<?php
function print_all_sidebar_links_except_self($self = NULL){
$hotels_text = array(
"hotel1" => "Gryffindor Tower",
"hotel2" => "Ravenclaw Tower",
"hotel3" => "Slytherin Dungeons"
);
if (isset($self)){
unset($hotels_text[$self]);
}
foreach ($hotels_text as $page => $name){
echo "<a href='".$page.".php'>".$name."</a>";
}
}
?>
print_all_sidebar_links_except_self()
;print_all_sidebar_links_except_self('hotel1');
print_all_sidebar_links_except_self('hotel2');
print_all_sidebar_links_except_self('hotel3');
Upvotes: 2