In php how to call or link another php page? - php

I am a beginner for this language and I don't know how to call another page or link
in other page please help how?

You can combine PHP and HTML together.
If you need a hyperlink in your page, you can either echo the html tags or embed the PHP code inside the HTML
Below is an example how I embedded the PHP inside the HTML tags
<html>
<head>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body style="width:1250px;">
<div>
<ul>
<li>Home</li>
<?php if (isset($_SESSION['user'])) { ?>
<li>
<li class="dropdown">
Account
<div class="dropdown-content">
Profile
My Articles
</div>
</li>
</li>
<li style="float:right">Logout</li>';
<?php } else { ?>
<li style="float:right">Register</li>
<li style="float:right">Login</li>';
<?php } ?>
</ul>
</div>
</body>
And below is the example of how to use html tags inside the PHP code:
<?php
echo "<html>";
echo "<title>HTML with PHP</title>";
echo "<b>My Example</b>";
print "<i>Print works too!</i>";
?>
For page redirects, you can use the below code in your PHP :
header('location: targetfile.php');

The PHP code located after the header() will be interpreted by the server, even if the visitor moves to the address specified in the redirection. In most cases, this means that you need a method to follow the header() function of the exit() function in order to decrease the load of the server:
<?php header('Location: /directory/mypage.php'); ?>

A basic link is created by wrapping the text (or other content, see Block level links) you want to turn into a link inside an element, and giving it an href attribute (also known as a Hypertext Reference , or target) that will contain the web address you want the link to point to

Related

how to make a main content of a sidebar without make a new tab/refresh the page to linked file?

<?php
session_start();
if (isset($_SESSION['id']) && isset($_SESSION['username'])) {
?>
<!DOCTYPE html>
<html>
<head>
<title>Dashboard</title>
<link rel="stylesheet" type="text/css" href="dashboard.css">
<script src="https://kit.fontawesome.com/463f6f67c2.js" crossorigin="anonymous"></script>
</head>
<body class="dashboard">
<h1>Halo,<?php echo $_SESSION['name']; ?></h1>
<div class="wrapper">
<div class="sidebar">
<h2>Sidebar</h2>
<ul><!--sidebar list-->
<li><i class="fa-solid fa-house-user"></i>Home</li>
<li><i class="fa-solid fa-user-tie"></i>Data Petugas</li>
<li><i class="fa-solid fa-users-rectangle"></i>Data Kelas</li>
<li><i class="fa-solid fa-money-check"></i>Data Siswa</li>
<li><i class="fa-solid fa-clock"></i>History Pembayaran</li>
</ul>
</div>
</div>
</div>
Logout
</body>
</html>
<?php
}else{
header("Location: index.php");
exit();
}
?>
in this case if i click 'petugas' on sidebar the web making the new tab/window without the sidebar,i want the sidebar stay in the page and load the 'petugas.php' without make a new tab/window,just in the 'petugas' section
This cannot be solved only by PHP.
PHP is executed server-sided. This means, the moment you see the page, all PHP is done. Calling another PHP script means calling a new page.
If you want to change content asynchronously on your page, you either need a different technology that works after the page is loaded, like JavaScript performing AJAX-Requests to another script and altering the HTML that has already been generated.
Edit: The answer by #Henrique shows methods for this approach.
Or you can "trick" this behaviour by loading everything upfront and hide the things you do not want to show from the beginning using CSS. Needless to say that this is not a good practice, but solves your question.
If you write each page yourself, you can put the sidebar to some sort of wrapper that loads with every page you link to. It will redraw on every link click, but it will be there for every page. And it seems the least complicated way for your situation.

If I want to mark as active page in the navigation menu in menu.php

I used 'include' php to separate header of my website.
So, I can easily fix if I need to change the navigation menu part in the header, instead fixing more than 20 pages each.
My question is I like to add a class, 'current' in the one of navigation button.
For example, if I am in 'Home' page, then I want to change font color of 'Home' button to red.
If I move to 'Contact' page, I want 'Contact' button to be changed to red and want 'Home' button to normal color.
Since all navigation button codes are in the header.html.
How can I add class 'current', so users can know which page they are looking at?
Thanks in advance.
If you are using php then you can set it like this.
1) Give class to each link
<li class="home">Home</li>
<li class="about">About</li>
<li class="contact">About</li>
Note : Give filename & classname same (If filename is home.php then class for this menu is "home")
2) In header.php use this code.
<?php
$class = basename($_SERVER['REQUEST_URI'], '.php?' . $_SERVER['QUERY_STRING']);
/* This basename function returns filename from url. For example if url is http://www.example.com/home.php?id=15, then this will return "home" only. */
?>
<script type="text/javascript" src="jquery.min.js"> <!-- Link your jquery library -->
<script type="text/javascript">
$(document).ready(function(){
$(".<?php echo $class; ?>").addClass('current');
});
</script>
This is a very basic and unsafe example, just so that you hopefully get the idea.
Find out first what page you're on, maybe you have a URL parameter called page that you call like index.php?page=home or index.php?page=contact.
<?php $page=$_REQUEST['page']; ?>
Then write your HTML:
Home<br>
Contact
Now add the class-checks to your links:
Home<br>
Contact
(This uses fancy inline IF statements, just because they fit the purpose so nicely. If you don't know them yet, I recommend to read up on them.)
If your $page variable is set to "home", this will generate the HTML source like so:
Home<br>
Contact
You could also include the entire class assignment into the PHP check:
<a href="index.php?page=home"<?=($page=='home'?' class="current"':'');?>>Home</a><br>
<a href="index.php?page=contact"<?=($page=='contact'?' class="current"':'');?>>Contact</a>
And that would generate the HTML source like this:
Home<br>
Contact
The most practical way would be to quite simply make a little function that generates everything for you, like for example this one:
<?php
function makeNavLink($pageName){
global $page;
$link='<a href="index.php?page='.$pageName.'"';
$link.=($page==$pageName?' class="current"':'').'>';
$link.=ucwords($pageName).'</a>';
return $link;
}
?>
That would allow you to call the function in your page like this:
<?=makeNavLink("home");?><br>
<?=makeNavLink("contact");?>
And it would also make the HTML output look like this if your page is "contact":
Home<br>
Contact
I can't comment because I don't have 50 rep, but I did some research and found this link How to have the class=“selected” depending on what the current page/url is. $_SERVER['REQUEST_URI'] is the approach that is used in this example. So if you need further clarification, you can look that up too.
Edit: This example does not require JQuery. Or you could try this:
<div class="menu">
<div id="whatever" class="whatever">
<ul>
<li><a href="index.php" <?php if (basename($_SERVER['PHP_SELF']) == "index.php") { ?> class="current" <?php } ?>>Home</a></li>
<li><a href="about.php" <?php if (basename($_SERVER['PHP_SELF']) == "about.php") { ?> class="current" <?php } ?>>About</a> </li>
</ul>
</div>
</div>

Sidemenu In codeigniter

I just try to bulid control panel using MVC (codeigniter), I just need way to how make a side menu in home page and when I click in the link in menu the page open in the right side, and the menu stay in the left side.
I need coorect way to do this using MVC.
I use this simple way to make Views to make exactly what your asking.
<?php
//This is mainpage.php
$data['page_id'] = $_GET['page_id'];
?>
<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
<div>
<!-- HERE Comes Side Bar -->
<?php $this->load->view('sidebar'); ?>
</div>
<div>
<!-- HERE Comes Main Content -->
<?php $this->load->view('maincontent',$data) ?>
</div>
</body>
</html>
Mainpage.php is a master page who holds code for both, side menu as well as the content page
This code if for side bar sidebar.php in view folder
<div>
<ul>
<li>Dashboard</li>
<li>Settings</li>
<li>Logout</li>
</ul>
</div>
sidebar.php page only holds different links, but all links have GET variable named "page_id" which decides which page is to be displayed in the content page.
Now, in mainpage.php you can notice that maincontent.php is loaded as view with passing $data which has page_id as variable, which is drived from sidebar. this will help to display the content in content side.
This code is for maincontent.php in view folder
<?php
if(file_exists(APPPATH.'views/'.$page_id.'.php')){
$this->load->view($page_id);
}else{
show_404();
}
this has always worked for me in non ajax page displays...so will work for you to.
Thanks...

Implement "pagename.html#id_of_section" similar functionality in php

I am currently working on a php project in which i needed to implement a page redirection.In HTML the page redirection
<a href=abc.html#section>Pagename<a/>
works proper to get me to any section of the page. But in php i tried to implement the same with tag and include method which gives me error.the code i tried is
if(?tag==tagname)
include('pagename.php#section');
How can i redirect visitor to some other page on some section which is in middle of the page?? implementing abc.html#section in php. Please give your guidance. Thanx in advance.
The structure is
<html>
<body>
<header>
<Navigation menu>
<span>Home</span>
</end Navigation menu>
<php>
if(?tag==home)
include(home.php);
</end php>
<footer>
</end body>
</end html>
This works fine but it will always open new page from the top. I needed to take users to home.php#section. Please help me out.
use $_GET to access query-paramters.
e.g.:
index.php?foo=bar&some=thing
$_GET['foo']; //contains bar
$_GET['some']; // contains thing
<html>
<body>
<header>
<Navigation menu>
<span>Home</span>
</end Navigation menu>
<?php
if(isset($_GET) && $_GET['tag'] && $_GET['tag'] == 'home') {
include(home.php);
}
?>
<footer>
</end body>
</end html>

How can I use a PHP Session variable in a dynamically loaded navbar?

I have a series of pages that load a navbar from an external HTML file like so:
<head>
<script>
$(function() {
$("#navbar").load("navbar.html");
});
</script>
</head>
<body>
<div id="navbar"></div>
<!--other stuff here-->
This is all in a PHP page. However, there's part of that navbar that I want to set to set to a PHP $_SESSION variable (a username). Is there a way to easily do this?
Consider change navbar.html to navbar.php, then replace the username part by:
<?php echo($_SESSION["username"] ?>
then
$("#navbar").load("navbar.php");
You won't be able to do it in the HTML you are including of course, but you could echo it out in your PHP page in a known element:
<span class='echoedUsername'><?php echo($_SESSION["username"] ?></span>
and then apply it to an element in your navbar:
$("#navbar .username").text($('.echoedUsername').text())

Categories