php check if link has been clicked - php

Hi I'm a novice at php could some please help. I'm making a website it has a menu, I need it so that if a link like "link1" is clicked page1.php will load into the the mainSection div and if link2 is clicked page2.php will load in mainSection etc. so all the pages: page1, page2, page3 etc will load into this single page depending on what link has been clicked. Is this possible I don't know where to start. Thanks
<body>
<?php
<ul>
<li>link 1</li>
<li>link 2</li>
<li>link 3</li>
<li>link 4</li>
</ul>
?>
<div id="mainSection">
<?php
if (link1 == true){
include 'page1.php';
}
if (link2 == true){
include 'page2.php';
}
if (link3 == true){
include 'page3.php';
}
if (link4 == true){
include 'page4.php';
}
?>
</div>
</body>

Here's something you can start with
<body>
<ul>
<li>link 1</li>
<li>link 2</li>
<li>link 3</li>
<li>link 4</li>
</ul>
<div id="mainSection">
<?php
$link=$_GET['link'];
if ($link == '1'){
include 'page1.php';
}
if ($link == '2'){
include 'page2.php';
}
if ($link == '3'){
include 'page3.php';
}
if ($link == '4'){
include 'page4.php';
}
?>
</div>
</body>

In addition to majid's code you have to check if the link has been set or else it throws an error of undefined $link.
link 1
link 2
link 3
link 4
<div id="mainSection">
<?php
if(isset($_GET['link'])){
$link=$_GET['link'];
if ($link == '1'){
include 'page1.php';
}
if ($link == '2'){
include 'page2.php';
}
if ($link == '3'){
include 'page3.php';
}
if ($link == '4'){
include 'page4.php';
}
} ?>
</div>
</body>

Change the format of your links to:
link 1...
and then change your PHP to:
<?php
if ($_SERVER['QUERY_STRING'] == 1){
include 'page1.php';
}
if ($_SERVER['QUERY_STRING'] == 2){
include 'page2.php';
}
if ($_SERVER['QUERY_STRING'] == 3){
include 'page3.php';
}
if ($_SERVER['QUERY_STRING'] == 4){
include 'page4.php';
}
?>

Related

RewriteRule is directing to index.php

I'm Rewriting with expression
RewriteRule ^([a-z_]+)$ index.php?action=$1
mydomain/following works , mydomain/created_quizes also works
but the proplem is with mydomain/followers ! it redirect to index.php
The weird part that they all are written similarly
The Navbar
<li class="nav-item">
<a class="nav-link<?php if($action == "followers"){echo " active";} ?>" href="followers">Followers</a>
</li>
<li class="nav-item">
<a class="nav-link<?php if($action == "following"){echo " active";} ?>" href="following">Following</a>
</li>
<li class="nav-item">
<a class="nav-link<?php if($action == "created_quizes"){echo " active";} ?>" href="created_quizes">Created Quizes</a>
</li>
<li class="nav-item">
<a class="nav-link<?php if($action == "joined_quizes"){echo " active";} ?>" href="joined_quizes">Joined Quizes</a>
</li>
Then included in index.php
if($_GET['action'] == "followers"){ //Followers
include 'includes/temps/navbar.php';
}else if($_GET['action'] == "following"){ //Following
include 'includes/temps/navbar.php';
}else if($_GET['action'] == "joined_quizes"){ //Created Quizes
include 'includes/temps/navbar.php';
}else if($_GET['action'] == "created_quizes"){ //Joined Quizes
include 'includes/temps/navbar.php';
}else{
Redirect('index.php');
}
I really can't find where is the problem

Make navigation menu buttons highlighted for the current page

I have created a navigation menu that loads different "includes" on the same page. I want the menu buttons to stay highlighted for the current page. All the answer I have seen here only deal with navigating to a different page for each link. Is is possible to do it on the same page? Because the navigation bar doesn't actually reload.
<nav class="menu">
<ul>
<li class="menuitem"><a href="?link=1" name="fluidBalance" <?php echo ($_GET['link'] == 1) ? 'class="highlight"' : ''; ?>>Calculated Fluid Balance</a></li>
<li class="menuitem"><a href="?link=2" name="actualFluidBalance" <?php echo ($_GET['link'] == 2) ? 'class="highlight"' : '';?>>Actual Fluid Balance</a></li>
<li class="menuitem"><a href="?link=3" name="graphicalView" <?php echo ($_GET['link'] == 3) ? 'class="highlight"' : ''; ?>>Graphical View</a></li>
</ul>
</nav>
<div class="contentFluidBalance" id="mainSection">
<?php
if(isset($_GET['link'])){
$link=$_GET['link'];
if ($link == '1'){
include 'includes/fluidBalance1.php';
}
if ($link == '2'){
include 'includes/fluidBalance2.php';
}
if ($link == '3'){
include 'includes/fluidBalance5.php';
}
}
?>
You can try this :
<nav class="menu">
<ul>
<li class="menuitem"><a href="?link=1" name="fluidBalance" <?php echo ($_GET['link'] == 1) ? 'class="highlight"' : ''; ?>>Calculated Fluid Balance</a></li>
<li class="menuitem"><a href="?link=2" name="actualFluidBalance" <?php echo ($_GET['link'] == 2) ? 'class="highlight"' : '';?>>Actual Fluid Balance</a></li>
<li class="menuitem"><a href="?link=3" name="graphicalView" <?php echo ($_GET['link'] == 3) ? 'class="highlight"' : ''; ?>>Graphical View</a></li>
</ul>
</nav>
where class="highlight" is the name of the class to highlight the menu.
In the end The following code worked for me:
<nav class="menu">
<ul>
<li<?php if (isset($_GET['link']) && $_GET['link'] == '1') echo 'class="current"'?>>Calculated Fluid Balance</li>
<li<?php if (isset($_GET['link']) && $_GET['link'] == '2') echo ' class="current"'?>>Actual Fluid Balance</li>
<li<?php if (isset($_GET['link']) && $_GET['link'] == '3') echo ' class="current"'?>>Graphical View</li>
</ul>
</nav>
<div class="contentFluidBalance" id="mainSection">
<?php
if(isset($_GET['link'])) {
$link=$_GET['link'];
if ($link == '1'){
include 'includes/fluidBalance1.php';
}
if ($link == '2'){
include 'includes/fluidBalance2.php';
}
if ($link == '3'){
include 'includes/fluidBalance5.php';
}
}
?>
</div>
First, check if the current page is equal to one of your menuitem :
<nav class="menu">
<ul>
<li class="menuitem <?php if (isset($_GET['link']) && $_GET['link'] == '1') : ?>current<?php endif; ?>">Calculated Fluid Balance</li>
<li class="menuitem <?php if (isset($_GET['link']) && $_GET['link'] == '2') : ?>current<?php endif; ?>">Actual Fluid Balance</li>
<li class="menuitem <?php if (isset($_GET['link']) && $_GET['link'] == '3') : ?>current<?php endif; ?>">Graphical View</li>
</ul>
</nav>
<div class="contentFluidBalance" id="mainSection">
<?php
if(isset($_GET['link'])) {
$link=$_GET['link'];
if ($link == '1'){
include 'includes/fluidBalance1.php';
}
if ($link == '2'){
include 'includes/fluidBalance2.php';
}
if ($link == '3'){
include 'includes/fluidBalance3.php';
}
}
?>
</div>
Then, in your CSS, do whatever you want with the current class :
.menuitem.current a {
color:red;
}

Changing the <li> class place when changing page & include the file in different pages

How do I include this: header.php file in different pages?
header.php:
<li>page1</li>
<li class="current" >page2</li>
<li>page3</li>
And then change: <li **class="current"** >page2</li> to page1 when I click on a link and change the page.
Page1:
<li class="current" >page1</li>
<li>page2</li>
<li>page3</li>
You can assign a variable
// At page1
$currentPage = 'page1';
<?php include 'yourHeaderFolder/header.php'; ?>
header.php:
<li <?php if ($currentPage === 'page1') { echo 'class="current"'; } ?> >page1</li>
<li <?php if ($currentPage === 'page2') { echo 'class="current"'; } ?> >page2</li>
<li <?php if ($currentPage === 'page3') { echo 'class="current"'; } ?> >page3</li>

PHP - Highlight ONLY menu (set class "active") if any of other submenus are clicked

I´m starting in PHP, cause I´m designer, not programer. I´m having a problem when I try to set a menu highlighted if you click any of the child options. Maybe the code will be more clear than me...
<ul class="menu">
<li><a href="link-1.php" <?php if ($page == '/link-1.php') { ?>class="active"<?php } ?>>LINK 1</a></li>
<li>
LINK DUMMY
<ul class="sub_menu">
<li>LINK 2</li>
<li>LINK 3</li>
<li>LINK 4</li>
<li>LINK 5</li>
</ul>
</li>
<li><a href="link-6.php" <?php if ($page == '/link-6.php') { ?>class="active"<?php } ?>>LINK 6</a></li>
<li><a href="link-7.php" <?php if ($page == '/link-7.php') { ?>class="active"<?php } ?>>LINK 7</a></li>
</ul>
So if you hover LINK DUMMY you´ll see LINK 2, LINK 3, LINK 4, LINK 5, but what I need is to fix LINK DUMMY if you click any of it childs (LINK 2 to LINK 5) using class="active".
Any help is really appreciated.
TIA!
In your "Dummy Link" anchor, use:
if ($page == '/link-2.php' || $page == '/link-3.php' || $page == '/link-4.php' || $page == '/link-5.php') {
echo "class='active'";
}
<?php $linkOnePages=array('/link-2.php','/link-3.php','/link-4.php', '/link-5.php');?>
<ul class="menu">
<li><a href="link-1.php" <?php if(in_array($page, $linkOnePages)) { ?>class="active"<?php } ?>>LINK 1</a></li>
<li> ....

Create a php menu that highlights current tab

So I have a menu in a php file that looks like this (This is the whole file. I'm totally new to PHP.)
menu.php:
<li id="current"><span>Home</span></li>
<li><span>Blog</span></li>
<li><span>Results</span></li>
<li><span>Pictures</span></li>
<li><span>Our Location</span></li>
Now in my pages I do this (index.php):
<div id="tabs1" >
<ul>
<!-- CSS Tabs -->
<?php include("menu.php"); ?>
</ul>
</div>
So what I want to be able to do is change the line above to this:
<?php include("menu.php?current=pictures"); ?>
Which would make the active tab the Pictures tab. How can I do this?
You could also try this:
Your php script
<?php
$selected = "pictures";
$current_id = ' id="current"';
include "menu.php";
?>
this is your menu:
<ul>
<li <?php if ($selected == "pictures") print $current_id; ?>><span>Home</span></li>
<li <?php if ($selected == "blog") print $current_id; ?>><span>Blog</span></li>
<li <?php if ($selected == "home") print $current_id; ?>><span>Results</span></li>
<li <?php if ($selected == "me") print $current_id; ?>><span>Pictures</span></li>
<li <?php if ($selected == "contacts") print $current_id; ?>><span>Our Location</span></li>
</ul>
Try this:
<li <?php if($_GET['current'] == 'home') {echo 'id="current"'}?>><span>Home</span></li>
<li <?php if($_GET['current'] == 'blog') {echo 'id="current"'}?>><span>Blog</span></li>
<li <?php if($_GET['current'] == 'results') {echo 'id="current"'}?>><span>Results</span></li></li>
and so on....
worth looking at
intelligent navigation
<nav>
<style>
#active{
color:#FFC801;
}
</style>
<?php
$activePage = basename($_SERVER['PHP_SELF'], ".php");
?>
<ul>
<li>About Us</li>
<li>Mentors</li>
<li>Tours</li>
<li>Animation</li>
<li>Blog</li>
<li>Testimonials</li>
<li>Press/Media</li>
<li>Facts</li>
</ul>
</nav>
I don't think its necessary for it to be done at the server side (using up CPU cycles).
Use javascript/CSS to achieve this.

Categories