Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    20
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Simplify 'include else if include'?

    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)?

    Code:
    <?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');
    	}
    ?>
    Last edited by Charlie8776; 01-09-2012 at 03:20 AM. Reason: Resolved

  • #2
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,447
    Thanks
    71
    Thanked 102 Times in 101 Posts
    Why would you format it like that and not have them all in the same file as functions?
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    20
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by myfayt View Post
    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?

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Alter the basic way it works. Something like:

    Code:
    <?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
    Last edited by MattF; 01-09-2012 at 03:03 AM.

  • Users who have thanked MattF for this post:

    Charlie8776 (01-09-2012)

  • #5
    New Coder
    Join Date
    Dec 2011
    Posts
    20
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MattF View Post
    Alter the basic way it works. Something like:

    Code:
    <?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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •