简体   繁体   中英

Dynamic linking in PHP

I am creating a custom CMS for learning. I have the plan to have the following pages,

  1. Login page
  2. All Posts page
  3. Edit Post page
  4. index page
  5. header.php (the website's header)
  6. footer.php (the website's footer)
  7. sidebar.php (the website's sidebar)

I am confused how would the index page link header, footer and sidebar. Please guide me how can I link these php files in index.php.

You can simply add an Array of files you want to include:

$array = ('header.php', 'footer.php', 'sidebar.php');

Then add some HTML Code structure...

and then you can access the Array and load files.

include_once($array[0]);

.. to include the header.php

include_once($array[1]);

.. to include the footer.php

....

you can use the require_once function to make your site not loading other content if the file does not exists.

if you want to add these files automaticly just add a loop.

foreach($array as $file){
if(file_exists($file)){
require_once($file);
}
else{
die($file.' does not exist!');
}
}

You can use either require_once or include . I personally use require once option cause it only needs to be include once and not again on later stage.

Inside your body tags:

require_once('/path/to/header.php');
require_once('/path/to/breadcrumb.php');
require_once('/path/to/content.php');
require_once('/path/to/footer.php');
require_once('/path/to/copyright.php');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM