SitePoint Sponsor

User Tag List

Results 1 to 22 of 22

Thread: PHP question..

  1. #1
    SitePoint Enthusiast kvk007's Avatar
    Join Date
    Sep 2002
    Location
    United States of America
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP question..

    Hi all,
    I hope I won't be confusing, but will try my best to explain this.

    Now, if you go to http://www.anandtech.com/siteinfo.html , you will see

    About AnandTech
    AnandTech Staff
    AnandTech Logos
    Advertise on AnandTech
    When you click on one of them, like "About AnandTech", it will take you to http://www.anandtech.com/siteinfo.html?about=y

    If you noticed, it STAYS siteinfo.htm, but adds "?about=y" to it.

    Now i believe ALL the pages are already in that file? And when you click on of them it take you to the part needed.

    Now how do i put all information on one PHP file, and display ONLY the pages needed?

  2. #2
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are a few different ways of doing this

    1) Print the page from your PHP file

    PHP Code:
    <?php

        $page 
    $_GET['page'];

        switch ( 
    $page ) {

            case 
    'home':
                echo 
    'This is the home page';
            break;

            case 
    'contact':
                echo 
    'This is the contact page';
            break;

            default:
                echo 
    'No page specified';


        }

    ?>
    Call this by going to sscriptname.php?page=home and you will get 'This is the home page', contact will get 'This is the contact page', anything else or nothing will get 'No page specified'

    2) An alternative is to include a file for each option instead of printing directly from the PHP file:

    PHP Code:
    <?php

        $page 
    $_GET['page'];

        switch ( 
    $page ) {

            case 
    'home':
                include ( 
    'home.htm' );
            break;

            case 
    'contact':
                include ( 
    'contact.htm' );
            break;

            default:
                include ( 
    'nopage.htm' );


        }

    ?>
    Hope that helps

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  3. #3
    SitePoint Enthusiast kvk007's Avatar
    Join Date
    Sep 2002
    Location
    United States of America
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    seanf, thanks alot!! I think I will use the 1st one... but please tell me, if i put EVERYTHING in one php file, and do as you said by calling it, would it make the site any slower?

    So which one would be efficient? the 1st or 2nd choice?

  4. #4
    SitePoint Enthusiast kvk007's Avatar
    Join Date
    Sep 2002
    Location
    United States of America
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh yea, if I wanna use the 2nd choice, how do i call them?

    as Call this by going to sscriptname.php?page=home and you will get 'home.htm'?

  5. #5
    SitePoint Enthusiast kvk007's Avatar
    Join Date
    Sep 2002
    Location
    United States of America
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i keep getting the following error when i use the 2nd choice yo gave me, the include ('bla.htm');

    Fatal error: input in flex scanner failed in /home/omanserv/public_html on line 1

  6. #6
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by kvk007
    if i put EVERYTHING in one php file, and do as you said by calling it, would it make the site any slower?
    PHP is very fast. If you wanted to print a lot of code you could always do it like this (everything outside of the PHP tags will be ignored by PHP and just sent straight to the browser):

    PHP Code:
    <?php

        $page 
    $_GET['page'];

        switch ( 
    $page ) {

            case 
    'home':
                
    ?>

                <html>
                <head>
                <title></title>
                <body>
                ...

                <?php
            
    break;

            case 
    'contact':
                echo 
    'This is the contact page';
            break;

            default:
                echo 
    'No page specified';


        }

    ?>
    Originally posted by kvk007
    Oh yea, if I wanna use the 2nd choice, how do i call them?

    as Call this by going to sscriptname.php?page=home and you will get 'home.htm'?
    Yes. If you go to scriptname.php?page=home PHP will include a file called home.htm. So, you must make sure this file exists. Your users wont be able to tell you have done this, as all they will see is scriptname.php?page=home

    Originally posted by kvk007
    i keep getting the following error when i use the 2nd choice yo gave me, the include ('bla.htm');
    That's an odd error. Can we see the code you are using? Have you created the HTML files? Are the HTML files in the same folder as the script?

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  7. #7
    SitePoint Zealot Sork's Avatar
    Join Date
    Jul 2002
    Location
    Portugal
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Here's a simple function that i use to my scripts.

    First i have all pages stored in an array:
    PHP Code:
    $PAGES = array(
    'extension'            =>    array('script'    =>    '.php',                    'template'    =>    '.tpl.php'),
    'default'            =>    array('script'    =>    'main',                    'template'    =>    'main'),
    'main_main'        =>    array('script'    =>    'main',                    'template'    =>    'main'),
    //PAGES------------------------------------------------
    'curso_gestao'        =>    array('script'    =>    'cursos/admin_curso',    'template'    =>    'cursos/admin_curso'),
    'curso_registo'        =>    array('script'    =>    'cursos/reg_curso',        'template'    =>    'cursos/reg_curso'    ),
    'curso_me'            =>    array('script'    =>    'cursos/cursosME',        'template'    =>    'cursos/cursoME'    ),... 
    Now the function:
    PHP Code:
    function page() {
            
            global 
    $PAGES;
            
            if (isset(
    $_GET['act']) AND isset($_GET['cod'])) {
            
                
    $page $PAGES[$_GET['act'].'_'.$_GET['cod']];
            
            } elseif (isset(
    $_POST['act']) AND isset($_POST['cod'])) {
            
                
    $page $PAGES[$_POST['act'].'_'.$_POST['cod']];
            
            } else {
            
                
    $page $PAGES['default'];
            
            }
            
            return array(
    'script' => $page['script'].$PAGES['extension']['script'],
    'template' => $page['template'].$PAGES['extension']['templates']);
        } 
    Hope you find it usefull

  8. #8
    SitePoint Enthusiast kvk007's Avatar
    Join Date
    Sep 2002
    Location
    United States of America
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks alot seanf!! Check out " http://omanserv.net " and click on the buttons such as "about us" , etc.. i used your 1st choice.

    Sork, your is too confusing, so I hope you can explain it :\

  9. #9
    SitePoint Zealot Sork's Avatar
    Join Date
    Jul 2002
    Location
    Portugal
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let's if i can explain you

    My links look like this[VBS]http://www.domain.com/?act=action&cod=code[/VBS]So here's the array $PAGES enter
    PHP Code:
    $PAGES = array(
    //the extension of the pages---------------------------
    'extension'            =>    '.php',
    //PAGES------------------------------------------------
    'action_code'        =>    actionCode',
    ...
    ); 
    Now here's the function simplified(notice that the other func. also handles templates, this one no)
    PHP Code:
    function page() {
            
            global 
    $PAGES;
            
            if (isset(
    $_GET['act']) AND isset($_GET['cod'])) {
                
    //Notice .'_'. so we get action_code
                
    $page $PAGES[$_GET['act'].'_'.$_GET['cod']];
            
            } elseif (isset(
    $_POST['act']) AND isset($_POST['cod'])) {
            
                
    $page $PAGES[$_POST['act'].'_'.$_POST['cod']];
            
            }
            
            return 
    $page.$PAGES['extension'];
        } 
    And you can call it like this
    PHP Code:
    $page page();

    include(
    $page); 
    Now i think you'll understand
    Last edited by Sork; Dec 14, 2002 at 14:53.

  10. #10
    SitePoint Enthusiast kvk007's Avatar
    Join Date
    Sep 2002
    Location
    United States of America
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Sork for trying to help. But i didn't get you still! I am still beginer in PHP, and dont know where to learn much for PHP, although I bought Kevin's book.

  11. #11
    SitePoint Enthusiast kvk007's Avatar
    Join Date
    Sep 2002
    Location
    United States of America
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The pages thing is going cool..


    What about if i wanna do this

    PHP Code:
        <?php
            
    if($page=contact)    
                <
    img src="../images/contact_bar.gif" border="0">
            if(
    $page=about)    
                <
    img src="../images/contact_bar2.gif" border="0">
            else
                echo(
    "nothing");
        
    ?>
    So if the page goes on "contact", I want it to give an image from X directory.. how do i do that?

  12. #12
    SitePoint Zealot Sork's Avatar
    Join Date
    Jul 2002
    Location
    Portugal
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you understand my code you could store the images in the array $PAGES

  13. #13
    SitePoint Enthusiast kvk007's Avatar
    Join Date
    Sep 2002
    Location
    United States of America
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Sork
    if you understand my code you could store the images in the array $PAGES
    :|

    that's the problem, I don't understand it..

  14. #14
    SitePoint Zealot Sork's Avatar
    Join Date
    Jul 2002
    Location
    Portugal
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    tell me exactly what you don't understand

  15. #15
    SitePoint Enthusiast kvk007's Avatar
    Join Date
    Sep 2002
    Location
    United States of America
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sork, I would be SO GRATEFUL if you go, line by line

    (that's how I obtain my iformation hehe)

  16. #16
    SitePoint Zealot Sork's Avatar
    Join Date
    Jul 2002
    Location
    Portugal
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll try...
    first we prepare an array with all the pages and with the keys equivalent to the action you want to make, an example:
    Code:
    http://www.domain.com/index.php?page=home
    So your array key will be 'home'.
    Here's the complete array
    PHP Code:
    $PAGES = array(   //definitions-------------
                    
    'extension'        =>    '.php',
                    
    'default'       =>    'main'
                    
    //pages-------------------
                    
    'home'            =>    'main',
                    
    'news'            =>    'news',
                    
    'news_read'        =>    'newsRead'
                
    ); 
    Now to call the page you want, suppose you call the page home:
    PHP Code:
    echo $PAGES['home'].$PAGES['extension']; 
    So far so good, now to handdle with your links:
    PHP Code:
    echo $PAGES[$_GET['page']].$PAGES['extension']; 
    Got it

    Right now you start to figurate out the function:
    PHP Code:
    function page($pages) {
        
        
    //---------------------------------------------------------------
        //First check if we have both page and code to indicate our page  
        //---------------------------------------------------------------
        
    if (isset($_GET['page']) AND isset($_GET['code']))
        {
            
    $page $pages[$_GET['page'].'_'.$_GET['code']];
        }
        
    //---------------------------------------------------------------
        //If we only have page  
        //---------------------------------------------------------------
        
    elseif (isset($_GET['page']))
        {
            
    $page $pages[$_GET['page']];
        }
        
    //---------------------------------------------------------------
        //This code do the something above but for $_POST 
        //---------------------------------------------------------------
        
    elseif (isset($_POST['page']) AND isset($_POST['code']))
        {
            
    $page $pages[$_POST['page'].'_'.$_POST['code']];
        }
        elseif (isset(
    $_POST['page']))
        {
            
    $page $pages[$_POST['page']];
        }
        
    //---------------------------------------------------------------
        //If any are set we load the default page
        //---------------------------------------------------------------
        
    else
        {
            
    $page $pages['default'];
        }
        
        
    //---------------------------------------------------------------
        //Now we return the page with the extension  
        //---------------------------------------------------------------
        
    return $page.$pages['extension'];
        

    Notice at this line:
    PHP Code:
    $page $pages[$_GET['page'].'_'.$_GET['code']]; 
    the _ is the connection with page and code in the array (off course it can be another):
    PHP Code:
    'news_read'        =>    'newsRead' 
    To end, in the 'index.php' you put something like this:
    PHP Code:
    $page page($PAGES);

    include(
    $page); 
    I think you now understand it and you can figurate out to attach an image to each page.
    Yes you right in the array

    Hope it helps, sorry my english and i'm here to explain your doubts

    Sork

  17. #17
    SitePoint Enthusiast kvk007's Avatar
    Join Date
    Sep 2002
    Location
    United States of America
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Sork! But I have 3 finals tomorrow and day after tomorrow.. So I will read them slowly when I am done. HOPEFULLY I Understand them

  18. #18
    SitePoint Enthusiast kvk007's Avatar
    Join Date
    Sep 2002
    Location
    United States of America
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi again guys!

    Quick question; if I have 100's of cases in my page, would that effect the speed of my site, when someone clicks on a link and then tries to search for the link it needs?

  19. #19
    SitePoint Zealot Sork's Avatar
    Join Date
    Jul 2002
    Location
    Portugal
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    kvk007, Hi!

    Do you understood my code? if so you don't need to do a switch()

    Sork

  20. #20
    SitePoint Enthusiast kvk007's Avatar
    Join Date
    Sep 2002
    Location
    United States of America
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi sork!

    I kind of got your point from the code.. But one question, why do we need the rest for this:

    PHP Code:
    function page($pages) {
        
        
    //---------------------------------------------------------------
        //First check if we have both page and code to indicate our page  
        //---------------------------------------------------------------
        
    if (isset($_GET['page']) AND isset($_GET['code']))
        {
            
    $page $pages[$_GET['page'].'_'.$_GET['code']];
        }
        
    //---------------------------------------------------------------
        //If we only have page  
        //---------------------------------------------------------------
        
    elseif (isset($_GET['page']))
        {
            
    $page $pages[$_GET['page']];
        }
        
    //---------------------------------------------------------------
        //This code do the something above but for $_POST 
        //---------------------------------------------------------------
        
    elseif (isset($_POST['page']) AND isset($_POST['code']))
        {
            
    $page $pages[$_POST['page'].'_'.$_POST['code']];
        }
        elseif (isset(
    $_POST['page']))
        {
            
    $page $pages[$_POST['page']];
        }
        
    //---------------------------------------------------------------
        //If any are set we load the default page
        //---------------------------------------------------------------
        
    else
        {
            
    $page $pages['default'];
        }
        
        
    //---------------------------------------------------------------
        //Now we return the page with the extension  
        //---------------------------------------------------------------
        
    return $page.$pages['extension'];
        

    ANOTHER question.. Normally if I wanna a call a site, i type in:
    PHP Code:
    <a href="../index.php?page=home">Go back home</a
    What about your code?

  21. #21
    SitePoint Zealot Sork's Avatar
    Join Date
    Jul 2002
    Location
    Portugal
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about your code?
    Don't get it

    Is the array() you don't understand?

    Sork

  22. #22
    Now with customized title Jump's Avatar
    Join Date
    Sep 2002
    Location
    The Restaurant at The End of The Universe
    Posts
    1,423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think he's asking what code does he put for his links on the page to switch between pages.


Bookmarks

Posting Permissions

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