• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

PHP - Include (Multiple "else"s)

Joseph89Digimon

New Member
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
 
ok my last post wasn't extremely helpful, it's after 1am, but i found what you're looking for-

PHP:
<?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.
 
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
 
related Q.

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 ^_^
 
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" );
?>
 
it's so simple. just assign an array of filetypes and make a loop to include such file.

here is an example

PHP:
$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
 
i tried the code :
PHP:
<?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?
 
Back
Top