PDA

View Full Version : PHP - Include (Multiple "else"s)



Joseph89Digimon
April 17th, 2004, 10:48
This is what I have so far:

<?
$page = $goto.".html";
if (file_exists($page)) {
include $page;
}
else if (file_exists($goto)) {
include $goto;
}
else{
require("404.html");
}
?>

Basicaly, I'm trying to get multiple else's. This is what I want my code to do:

Include (goto).html, if it doesn't exist include (goto).html, if neither exist, include (goto).php, if all 3 dont exist, include (goto), and lastly, if nothing works include 404.html.

Maybe having more than one else isn't the best method to go about solving this, but I have no idea what I'm doing.

The code above doesn't work, but if goto=index.html, it is supposed to do this:
include index.html.html
else include index.html.php
else include index.html
else include 404.html

In the above case, the code should make index.html appear if its the only valid file, if not, display 404.html as a last resort. In a nut shell, it is designed to keep searching until it finds a valid file extention or include no extention at all. In case I have both index.html and index.php in the same directory, I want it to search in a certain specified order, like for an index.html 1st, and if that doesn't exist it will search for index.php in a 2nd try.

Can anybody help me get this to work? Thanks

phuckedup
April 17th, 2004, 10:51
first thing that came to my head, i believe elseif is 1 word ;)

phuckedup
April 17th, 2004, 11:02
ok my last post wasn't extremely helpful, it's after 1am, but i found what you're looking for-


<?PHP
error_reporting (E_ALL ^ E_NOTICE);
if(!$go){ $go = $HTTP_GET_VARS['go']; }

if($go=="" or $go=="home"){
include ("home.php");
}elseif($go=="page2"){
include ("page2.php");
}else {
include ("404.php");
}
?>

so if you go to url.com/?go=home, where the script is will include home.php. You can change ?go to anything else, just change the three go's.

Joseph89Digimon
April 17th, 2004, 11:04
Holy sh** ur right. I deleted 1 space and now it works. Damn, I didn't think I knew wut the hell I was doing...

oh and that other thing works too. thanx man

phuckedup
April 17th, 2004, 11:05
ok my last post wasn't extremely helpful..

heh maybe it was :p

maansy
June 19th, 2004, 07:38
there is:
index.php
in the same path ( folder) there is :
includes folder which has several .txt files and .htm file and .php files

now i am using this code to connect to .txt files:
<?php
if (!isset($_GET['p'])) { // no page specified -> load default page
include("includes/default.txt");
} else include("includes/" . $_GET['p'] . ".txt");
?>

this code works file if i ONLY want to connect to .txt files, ok

Now i would like to use the same method to connect not only to .txt files but also .htm and .php files as well.

like this:

Main site---> index.php
---> includes folder
--->1.txt
--->2.htm
--->3.php


noe in the index.php i have links for:
1.txt
2.htm
3.php

now when users click on 1.txt the should go to 1.txt
and if they click on 2.htm they shold go to 2.htm
and if they click on 3.php the should go to 3.php

the code should do this basicly:
<?php
if (!isset($_GET['p'])) { // no page specified -> load default page
include("includes/default.txt");
} else include("includes/" . $_GET['p'] . ".txt" OR . ".htm" OR . ".php" );
?>


any help apreciated ^_^

phuckedup
June 19th, 2004, 08:03
is there a question in there?

maansy
June 19th, 2004, 12:01
hehe oops so now you can tell this is not my first lang.

anyways, yes there is a Q.

the Q is how to manag to get this code working?


<?php
if (!isset($_GET['p'])) { // no page specified -> load default page
include("includes/default.txt");
} else include("includes/" . $_GET['p'] . ".txt" OR . ".htm" OR . ".php" );
?>

bloodyveins
June 19th, 2004, 13:24
it's so simple. just assign an array of filetypes and make a loop to include such file.

here is an example



$ext = array('txt','php','html','htm'); //just add others based on your requirement
if (!isset($_GET['p'])) { // no page specified -> load default page
include("includes/default.txt");
else {
$found = false;
for ($i=0; $i<count($ext); $i++) {
if(file_exists($_GET['p'].".".$ext[$i])) {
include $_GET['p'].".".$ext[$i];
$found = true;
break; //stop the loop if there is a file to include
}
}
if(!$found)
include "includes/default.txt"; //we found nothing, load default page
}


Notice that you have to set priority of file types to be included. Above, the priority is txt(1st) -> php -> html -> htm

maansy
June 21st, 2004, 04:09
i tried the code :


<?php

/*if (!isset($_GET['p'])) { // no page specified -> load default page
include("includes/default.txt");
} else include("includes/" . $_GET['p'] . ".txt");*/

$ext = array('txt','php','html','htm'); //just add others based on your requirement
if (!isset($_GET['p'])) { // no page specified -> load default page
include("includes/default.txt");
}
else {
$found = false;
for ($i=0; $i<count($ext); $i++) {
if(file_exists($_GET['p'].".".$ext[$i])) {
include $_GET['p'].".".$ext[$i];
$found = true;
break; //stop the loop if there is a file to include
}
}

if(!$found)
include "includes/default.txt"; //we found nothing, load default page
}

?>


no mattar what link i click it only shows the defualt.txt page

any idea?

spec
June 22nd, 2004, 01:36
what is in your default.txt