Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,845 members, 7,817,496 topics. Date: Saturday, 04 May 2024 at 01:14 PM

[PHP] How Can I Use REQUIRE For A Footer That Exist In Another Folder? - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / [PHP] How Can I Use REQUIRE For A Footer That Exist In Another Folder? (1126 Views)

Php How To Determine If A Quantity Is Declining? / Help on how to design header, footer and add width / How To Insert Script Into Your Header And Footer (2) (3) (4)

(1) (Reply) (Go Down)

[PHP] How Can I Use REQUIRE For A Footer That Exist In Another Folder? by branhampaul: 5:43am On Nov 22, 2018
I want to REQUIRE a footer that exist in another folder.
I tried require "../footer.php",
but this isn't working.
What am I doing wrong?
Re: [PHP] How Can I Use REQUIRE For A Footer That Exist In Another Folder? by hollyfat(m): 5:52am On Nov 22, 2018
Are you sure that the footer.php is in the folder you are referencing?
Re: [PHP] How Can I Use REQUIRE For A Footer That Exist In Another Folder? by branhampaul: 6:41am On Nov 22, 2018
hollyfat:
Are you sure that the footer.php is in the folder you are referencing?
Yes, it is.
Re: [PHP] How Can I Use REQUIRE For A Footer That Exist In Another Folder? by talk2hb1(m): 7:31am On Nov 22, 2018
branhampaul:
I want to REQUIRE a footer that exist in another folder.
I tried require "../footer.php",
but this isn't working.
What am I doing wrong?
Try require"footer.php"
I guess they are both in the same directory llevel.
A quick boner:
That ../ means hey php go up a directory from this current directory and include a file named footer.php here because it is required. But if the footer.php is in a folder as i assumed possibly in an include directory and at a upper level from where you are including it just do require"../include/footer.php
Re: [PHP] How Can I Use REQUIRE For A Footer That Exist In Another Folder? by holuphisayor(m): 7:39am On Nov 22, 2018
You are not referencing any folder with that require statement. What's your project structure or better still learn how to autoload ur files.
Re: [PHP] How Can I Use REQUIRE For A Footer That Exist In Another Folder? by webapi: 8:13am On Nov 22, 2018
branhampaul:
I want to REQUIRE a footer that exist in another folder.
I tried require "../footer.php",
but this isn't working.
What am I doing wrong?
Why are you using REQUIRE? instead use include....
Re: [PHP] How Can I Use REQUIRE For A Footer That Exist In Another Folder? by ensodev(m): 8:53am On Nov 22, 2018
You have to show the structure of your folder.
Meanwhile include is what you should use unless your footer file has a method that you have been refering to in some part of your folder.

Show us something
Re: [PHP] How Can I Use REQUIRE For A Footer That Exist In Another Folder? by afonja112: 9:12am On Nov 22, 2018
Let me explain something:

+ resources
++ views
+++ header.php
+++ footer.php
+ app
++ template.php
+ index.php

Let us assume you load the index folder, and want to include the app/template.php, here is what most people will do
include "app/template.php"; //relative addressing

Here is what i will do
include __DIR__."/app/template.php"; //absolute addressing

So let us now assume that inside the template.php you wish to include header.php, you best best is this

include __DIR__."/../resources/views/header.php"; //this is absolute addressing still


If you want to include footer.php inside header.php, then is best to do
include __DIR__."/footer.php";

If you follow this guideline, you will get it better.

1 Like

Re: [PHP] How Can I Use REQUIRE For A Footer That Exist In Another Folder? by branhampaul: 4:01pm On Nov 22, 2018
webapi:

Why are you using REQUIRE? instead use include....
Nope, I prefer REQUIRE, INCLUDE makes the page work even when that code isn't present, this may distort the workflow.
Re: [PHP] How Can I Use REQUIRE For A Footer That Exist In Another Folder? by ensodev(m): 4:06pm On Nov 22, 2018
afonja112:
Let me explain something:

+ resources
++ views
+++ header.php
+++ footer.php
+ app
++ template.php
+ index.php

Let us assume you load the index folder, and want to include the app/template.php, here is what most people will do
include "app/template.php"; //relative addressing

Here is what i will do
include __DIR__."/app/template.php"; //absolute addressing

So let us now assume that inside the template.php you wish to include header.php, you best best is this

include __DIR__."/../resources/views/header.php"; //this is absolute addressing still


If you want to include footer.php inside header.php, then is best to do
include __DIR__."/footer.php";

If you follow this guideline, you will get it better.


Nice one
Re: [PHP] How Can I Use REQUIRE For A Footer That Exist In Another Folder? by afonja112: 6:36pm On Nov 22, 2018
I have built frameworks before, and I know this is the technique used by most frameworks to load stuffs so that you dont enter wahala with require/include things.
Re: [PHP] How Can I Use REQUIRE For A Footer That Exist In Another Folder? by branhampaul: 6:57pm On Nov 22, 2018
Thanks all, I have figured it out.
Re: [PHP] How Can I Use REQUIRE For A Footer That Exist In Another Folder? by afonja112: 8:54pm On Nov 22, 2018
Very good, how did you do it? So that others can learn.
Re: [PHP] How Can I Use REQUIRE For A Footer That Exist In Another Folder? by branhampaul: 10:52pm On Nov 22, 2018
afonja112:
Very good, how did you do it? So that others can learn.
my original code was correct, I was the one peddling with the wrong PHP file.
Re: [PHP] How Can I Use REQUIRE For A Footer That Exist In Another Folder? by afonja112: 9:26am On Nov 23, 2018
Great that you have solved it. I will also use this opportunity to chip in a few things:

1. For developers, try to use simple directory structures.
2. Use simple naming conventions e.g. if you are like me, just name your files with lowercase and be consistent with it. Or name your files with camelCase - and require it that way.

Many developers enter a jam for this, see this:

+ index.php
+ Head.php
+ Foot.php


inside index.php, you now do this
include 'head.php';
include 'foot.php';

This will work on windows and mac, but by the time you get to a live server e.g. linux, you will start getting file not found, and start wondering if it is your village people that are chasing you.

I don wake dey go be that.

2 Likes

Re: [PHP] How Can I Use REQUIRE For A Footer That Exist In Another Folder? by ensodev(m): 11:11pm On Nov 23, 2018
afonja112:
Great that you have solved it. I will also use this opportunity to chip in a few things:

1. For developers, try to use simple directory structures.
2. Use simple naming conventions e.g. if you are like me, just name your files with lowercase and be consistent with it. Or name your files with camelCase - and require it that way.

Many developers enter a jam for this, see this:

+ index.php
+ Head.php
+ Foot.php


inside index.php, you now do this
include 'head.php';
include 'foot.php';

This will work on windows and mac, but by the time you get to a live server e.g. linux, you will start getting file not found, and start wondering if it is your village people that are chasing you.

I don wake dey go be that.

NICE ALERT.. .Red flag for the wise. Great info!!!!!!!!

(1) (Reply)

Never Give Up, Keep Coding / Who Can Build Payment Plugins For CMS? / Software Engineering

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 23
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.