简体   繁体   中英

including different files based on if/else php

I've made 2 different footers to include them on different files.

The code is in the index.php, while the other files are included dynamically from the index page.

$page_id = array('1','2','3','4','5','6','7','8','9','10','11');
if ( $page_id ) {
   include('inc/footer_de.php');
} else {
   include('inc/footer_en.php');
};

On each page i have assigned the value of the page with eg.

 $page_id = '10';

But in all the pages the first footer is included twice and the second never appear. How to write the code in a correct way?

Your if statement is asking "if the variable $page_id exists", which it does (as an array), and so you always get the same footer_de.php included.

I think what you're wanting to do is check if the $page_id is in the array in which case you'd do...

$page_id_array = array('1','2','3','4','5','6','7','8','9','10','11');
if(in_array($page_id,$page_id_array){
    include('inc/footer_de.php');
}else{
    include('inc/footer_en.php');
};

There are a lot of issues on your code to begin with:

if($page_id){ 

*this will always be true since it is a value and not really a condition / comparison you might want use in_array (checking if the page exists in the array) or $page_id == 10 (if you are looking for a specific number / pageid)

and

include('inc/footer_en.php');
};

*if and else statements doesn't need semicolon (;) at the end

I can only guess what you want to achieve.

Your code simply overrides the $page_id variable with the array and therefore if ($page_id) just checks whether the array evaluates as true. I guess the reason for including it twice is outside the code block you pasted.

You probably want to use:

http://php.net/manual/en/function.in-array.php

$page_id = '10';
...
$de_pages = array('1','2','3','4','5','6','7','8','9','10','11');
if (in_array($page_id, $de_pages))
    include('inc/footer_de.php');
else
    include('inc/footer_en.php');

First, I don't know why you store string value in the array and not numbers, but I will go with what you have.

If I understood you correctly, if the page id exists in the array, then you want to include the first footer, else include the second. If that's the case here's my answer (using in_array php function )

I also assume you do not mean to use the same parameter. I presume you have the pages array as a variable. and you have the Page ID as a different variable (for each page).

$pagesArray = array('1','2','3','4','5','6','7','8','9','10','11');
if (in_array($page_id, $pagesArray)) {
     include('inc/footer_de.php');
}
else{
    include('inc/footer_en.php');
}

Make sure you remove the last ';' from your code.

You have also stated the footer is included twice. No proper explanation can be given, except that you are probably including it another time from outside the code block you have posted. To include it only once you can use: include_once instead of include For example:

include_once('inc/footer_de.php');

This prevents duplicate inclusions, but again if you want us to help you with that, you should post more information , or your whole code block.

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