Forum Moderators: coopster

Message Too Old, No Replies

URL Page Function

Load various pages depending on the url.

         

new2php

12:08 pm on Jan 19, 2006 (gmt 0)

10+ Year Member



Hello, I'm relatively new to php. I've got about 3 days under my belt now.

Anyhow, I'm trying to create a function that will call an include file depending on the url, this function will get called at various parts of a page(in index.php), ie. on the left side of the table to include the correct menu and on the right side of the table for the content include.

I have created the function and it kinda works, but only for 1 variable! For the second variable I just get the "Nothing" statement (see code). I've tried to echo the filename to see what it's trying to call, and for the first one it works (it tells me and includes the correct filename), but for the second one it's just trying to call " " (nothing).

By the way, it works perfectly if I only have 1 variable, but I need it to work with 2, or possibly more some day.

For info.: if the file doesn't exist you get message: "File doen't exist", if in the url its index?menu= then you get: "No file name", and if in the url it's only index.php you get: "Nothing"

Someone please help!

For example:
url: index.php?menu=main&content=front
--

index.php :
------------
load_include("menu") //call function, to get the menu (main in this case) included.

<?php
/*
* usage: load_include("thisitem");
* file.php?thisitem=filename&anotheritem=anotherfile (without extension)
*/

function load_include($section_name)
{
if(isset($_GET[$section_name]))
{
while (list($key, $value) = each($_GET))
{

switch ($key)
{
case $section_name:
$temp = $value;
break;
}
}
if ($temp!= "")
{
$path = 'includes/';
$page = $temp;
$extension = '.php';

$filename = $path.$page.$extension;
echo $filename;

if(file_exists($filename))
{
include $filename;
}
else
{
echo "File doen't exist";
}
}
else
{
echo "No file name: $filename";
}
}
else
{
print "<br />Nothing<br />";
}
}
?>

new2php

4:03 pm on Jan 19, 2006 (gmt 0)

10+ Year Member



hmm...

I kinda solved it, I just made it a lot simpler:

function load_include($filetocall)
{
$page = $_GET[$filetocall];
$path = 'includes/';
$extension = '.php';
$filename = $path.$page.$extension;
if(file_exists($filename))
{
include $filename;
}
else
{
echo "<br /><br /><b>File: $filename does not exist!</b><br /><br />";
}
}

but I have no clue why the one before didn't work...

Any ideas?

Thanks.