...

View Full Version : Resolved Simplify 'include else if include'?



Charlie8776
01-09-2012, 03:07 AM
Goal: All php files are numbered by integer (e.g. 1.php, 2.php, 3.php, etc.) When users access index.php?1, then 1.php is included (and so forth for any integer). How can I simplify the code below so I don't have to type out each number?

Bonus: All the php files are stored in index/include folder. How can I specify the default (calling index.php without a variable) to include the top ten php files in that folder (e.g. 900.php through 910.php)?


<?PHP
if(isset($_GET['1'])){
include('index/include/1.php');
} else if(isset($_GET['2'])){
include('index/include/2.php');
} else if(isset($_GET['3'])){
include('index/include/3.php');
} else if(isset($_GET['4'])){
include('index/include/4.php');
} else {
include('index/include/1.php');
}
?>

myfayt
01-09-2012, 03:40 AM
Why would you format it like that and not have them all in the same file as functions?

Charlie8776
01-09-2012, 03:44 AM
Why would you format it like that and not have them all in the same file as functions?

Because I'm a noob? Aaaand open to suggestions. How would I do that?

MattF
01-09-2012, 03:57 AM
Alter the basic way it works. Something like:



<?php

$id = (isset($_GET['id']) ? intval($_GET['id']) : 1);

if (file_exists('index/include/'.$id.'.php'))
{
include('index/include/'.$id.'.php');
}
else
{
# This file is used if a nonexistent file is requested.
include('index/include/default.php');
}

?>


You'd then use a URI along the lines of:

http://example.org?id=3

Charlie8776
01-09-2012, 04:20 AM
Alter the basic way it works. Something like:



<?php

$id = (isset($_GET['id']) ? intval($_GET['id']) : 1);

if (file_exists('index/include/'.$id.'.php'))
{
include('index/include/'.$id.'.php');
}
else
{
# This file is used if a nonexistent file is requested.
include('index/include/default.php');
}

?>


You'd then use a URI along the lines of:

http://example.org?id=3

Fantastic! Thank you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum