I'm working on a website and I want to change the page contents when a user selects which page they want to navigate to. What I'm trying to accomplish would be like ASP.NET where you have only 1 navigation component that is used across all pages and the content of the page changes when a user selects a different page. How would I be able to accomplish this if I'm building a website with HTML/CSS and PHP. Any information I'm getting is how to change page content dynamically from PHP. I want to change the page content from other files in my directory
CodePudding user response:
easy, you'll create normal pages without navigations, with their normal links, then you'll create navigation to add it as a component.
nav.php
<nav>
<ul>
...
</ul>
</nav>
index.php
<?php include_once "nav.php"; ?>
<p>index</p>
contacts.php
<?php include_once "contacts.php"; ?>
<p>contacts</p>
EDIT
If you have too many included and you want a short include to them, you can do this by including all files you want to include then include thi file wherever you want, it's preferred to add them in a separated folder from the pages like components or includes, like
includes / css_files.php
<link rel="stylesheet" href="...">
<link rel="stylesheet" href="...">
<link rel="stylesheet" href="...">
<link rel="stylesheet" href="...">
<link rel="stylesheet" href="...">
<link rel="stylesheet" href="...">
includes / metas.php
<meta bla="" bla="" bla=""\>
<meta bla="" bla="" bla=""\>
<meta bla="" bla="" bla=""\>
<meta bla="" bla="" bla=""\>
include / seo.php
<meta keywords="StackOverflow, HTML">
<meta keywords="StackOverflow, HTML">
<meta keywords="StackOverflow, HTML">
for includes.php files you have to choices:
- is to put the include file with the pages in the same directory, and it will be like
includes.php
<?php
include "includes/seo.php";
include "includes/metas.php";
include "includes/css_files.php";
- you can but it with other includes in the same directory, but dont remove the
includes/before includes. includes.php
include "includes/seo.php";
include "includes/metas.php";
include "includes/css_files.php";
then the pages will be like
<head><?php include "includes.php"; ?></head>
<p>...</p>
because including in PHP includes the code COPY&PASTE, so you'll treat it as you write it in pages. For example, index.php will be like if you didn't write includes/ before the filename:
<head>
<?php
include "seo.php";
include "metas.php";
include "css_files.php";
?>
</head>
index lorem ipsum
and it will not include it, so nor the include only takes the file and place it in the file as it is without changing anything
CodePudding user response:
You can use PHP include function to control elements from only one file. Write this in your index.php in the place where you want to place header
<?php include('path\header.php'); ?>
and in header.php write the code like
<header>
...
</header>
and you could add css, javascript, jquery resource files into index.php
And advantage of include code is when visitor look at your page source or developer tools' source it will appear as your header.php file not php include line.
