Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 10 of 10
  1. #1
    Senior Member Blackhawk095's Avatar
    Join Date
    May 2011
    Location
    Kentucky (USA)
    Posts
    384
    Member #
    27969
    Liked
    45 times
    I'm working on a section a website that has multiple parts to it. I'm trying to use a PHP switch statement so that the page will change the content on that page when sub navigation link is clicked. I'm doing this to prevent the need to create multiple extra pages.

    The script I wrote worked on the "news" section of the website.
    http://www.my-testing-area2.netau.ne...s/articles.php
    (click on one of the orange links and then look at the url)

    Here is the script for the "News" section:
    PHP Code:
    <?php
    $date 
    $_GET['date'];
    $id $_GET['id'];
    switch(
    $date && $id) {

        case 
    'articles.php?date=1-16-2012&id=1':
            echo 
    '
            <div class="newsfeed orange">
                <div class="newsfeed_h2"><a href="articles.php?id=1" target="_parent" title="News Topic One">News Topic One</a><span> | <span>Jan 16 , 2012</span></span></div>
                <div class="newsfeed_text">Welcome to the brand new TSA and Engineering Tech website. This new slick design will be entered into the tsa national competition....</div>
            </div>
            <input type="button" value="Back" onClick="history.go(-1)">
            '
    ;
        break;

        case 
    'articles.php?date=3-10-2012&id=2':
            echo 
    '
            <div class="newsfeed orange">
                <div class="newsfeed_h2"><a href="articles.php?date=3-10-2012&id=2" target="_parent" title="News Topic Two">News Topic Two</a><span> | <span>Mar 10 , 2010</span></span></div>
                <div class="newsfeed_text">Welcome to the brand new TSA and Engineering Tech website. This new slick design will be entered into the tsa national competition....</div>
            </div>
            <input type="button" value="Back" onClick="history.go(-1)">
            '
    ;
        break;

        case 
    'articles.php?date=2-23-2011&id=2':
            echo 
    '
            <div class="newsfeed orange">
                <div class="newsfeed_h2"><a href="articles.php?date=2-23-2011&id=2" target="_parent" title="News Topic Three">News Topic Three</a><span> | <span>Feb 23 , 2011</span></span></div>
                <div class="newsfeed_text">Welcome to the brand new TSA and Engineering Tech website. This new slick design will be entered into the tsa national competition....</div>
            </div>
            <input type="button" value="Back" onClick="history.go(-1)">
            '
    ;
        break;

        default:
            echo 
    '
            <h1>Recent News</h1>

            <div class="newsfeed orange">
                <div class="newsfeed_h2"><a href="articles.php?date=1-16-2012&id=1" target="_parent" name="1" title="News Topic One">News Topic One</a><span> | <span>Jan 16 , 2012</span></span></div>
                <div class="newsfeed_text">Welcome to the brand new TSA and Engineering Tech website. This new slick design will be entered into the tsa national competition....</div>
            </div>

            <div class="newsfeed orange">
                <div class="newsfeed_h2"><a href="articles.php?date=3-10-2012&id=2" target="_parent" title="News Topic Two">News Topic Two</a><span> | <span>Mar 10 , 2010</span></span></div>
                <div class="newsfeed_text">Welcome to the brand new TSA and Engineering Tech website. This new slick design will be entered into the tsa national competition....</div>
            </div>

            <div class="newsfeed orange">
                <div class="newsfeed_h2"><a href="articles.php?date=2-23-2011&id=2" target="_parent" title="News Topic Three">News Topic Three</a><span> | <span>Feb 23 , 2011</span></span></div>
                <div class="newsfeed_text">Welcome to the brand new TSA and Engineering Tech website. This new slick design will be entered into the tsa national competition....</div>
            </div>

        <h1>Older News</h1>

            <div class="accordionButton">February 2012</div>

            <div class="accordionContent blue">
                <div class="newsfeed">
                <div class="newsfeed_h2"><a href="" target="_parent" title="New Website">New Website</a><span> | <span>Feb 1, 2012</span></span></div>
                <div class="newsfeed_text">Welcome to the brand new TSA and Engineering Tech website. This new slick design will be entered into the tsa national competition....</div>
            </div>
            <div class="newsfeed">
                <div class="newsfeed_h2"><a href="" target="_parent" title="New Website">New Website</a><span> | <span>Feb 1, 2012</span></span></div>
                <div class="newsfeed_text">Welcome to the brand new TSA and Engineering Tech website. This new slick design will be entered into the tsa national competition....</div>
            </div>
            <div class="newsfeed">
                <div class="newsfeed_h2"><a href="" target="_parent" title="New Website">New Website</a><span> | <span>Feb 1, 2012</span></span></div>
                <div class="newsfeed_text">Welcome to the brand new TSA and Engineering Tech website. This new slick design will be entered into the tsa national competition....</div>
            </div>
            </div>

            <div class="accordionButton">January 2012</div>
            <div class="accordionContent blue">
                <div class="newsfeed">
                <div class="newsfeed_h2"><a href="" target="_parent" title="New Website">New Website</a><span> | <span>Jan 1, 2012</span></span></div>
                <div class="newsfeed_text">Welcome to the brand new TSA and Engineering Tech website. This new slick design will be entered into the tsa national competition....</div>
            </div>
            <div class="newsfeed">
                <div class="newsfeed_h2"><a href="" target="_parent" title="New Website">New Website</a><span> | <span>Jan 1, 2012</span></span></div>
                <div class="newsfeed_text">Welcome to the brand new TSA and Engineering Tech website. This new slick design will be entered into the tsa national competition....</div>
            </div>
            <div class="newsfeed">
                <div class="newsfeed_h2"><a href="" target="_parent" title="New Website">New Website</a><span> | <span>Jan 1, 2012</span></span></div>
                <div class="newsfeed_text">Welcome to the brand new TSA and Engineering Tech website. This new slick design will be entered into the tsa national competition....</div>
            </div>
            </div>

            <div class="accordionButton">2011</div>
            <div class="accordionContent blue">
                <div class="newsfeed">
                <div class="newsfeed_h2"><a href="" target="_parent" title="New Website">New Website</a><span> | <span>Dec 1, 2011</span></span></div>
                <div class="newsfeed_text">Welcome to the brand new TSA and Engineering Tech website. This new slick design will be entered into the tsa national competition....</div>
            </div>
            <div class="newsfeed">
                <div class="newsfeed_h2"><a href="" target="_parent" title="New Website">New Website</a><span> | <span>Nov 1, 2011</span></span></div>
                <div class="newsfeed_text">Welcome to the brand new TSA and Engineering Tech website. This new slick design will be entered into the tsa national competition....</div>
            </div>
            <div class="newsfeed">
                <div class="newsfeed_h2"><a href="" target="_parent" title="New Website">New Website</a><span> | <span>Oct 1, 2011</span></span></div>
                <div class="newsfeed_text">Welcome to the brand new TSA and Engineering Tech website. This new slick design will be entered into the tsa national competition....</div>
            </div>
            </div>
            <!--END ACCORDION -->
            '
    ;
        break;
    }

    ?>
    My post was too long. The following comment will explain the rest.
    [COLOR=rgb(0, 51, 102)]Student Web Designer[/COLOR]
    [COLOR=rgb(0, 51, 102)]If you thought something I said was helpful. LIKE IT![/COLOR]

  2.  

  3. #2
    Senior Member Blackhawk095's Avatar
    Join Date
    May 2011
    Location
    Kentucky (USA)
    Posts
    384
    Member #
    27969
    Liked
    45 times
    But for some reason, the script won't work on the "competitions" section:
    http://www.my-testing-area2.netau.ne...mpetitions.php

    The url changes, but the content stays the same.

    This is the html of the sub navigation that holds the links to the switch statements:

    HTML Code:
    <div id="dc_nav">
    <ul>
        <li><span>Competition Links:</span></li>
        <li><a href="<?php echo $url ?>pages/tsa/competitions.php" id="dc_nav_first" target="_parent" title="Main Competitions Page">Main</a></li>
        <li><a href="competitions.php?p=our_projects" target="_parent" title="Our Projects">Our Projects</a></li>
        <li><a href="competitions.php?p=dress_code" target="_parent" title="Dress Code">Dress Code</a></li>
        <li><a href="competitions.php?p=code_of_conduct" target="_parent" title="Code of Conduct">Code of Conduct</a></li>
        <li><a href="competitions.php?p=competitions_list" target="_parent" title="Competition List">Competition List</a></li>
    </ul>
    </div>
    This is the PHP script:

    PHP Code:
    <?php

    $p 
    $_GET['p'];
    switch(
    $p) {

        case 
    'competitions.php?p=our_projects':
            echo 
    '
            <h1>Our Projects</h1>
                <p>This is some test text</p>
            '
    ;
        break;

        case 
    'competitions.php?p=dress_code':
            echo 
    '
            <h1>Dress Code</h1>
            '
    ;
        break;

        case 
    'competitions.php?p=code_of_conduct':
            echo 
    '
            <h1>Code of Conduct</h1>
            '
    ;
        break;

        case 
    'competitions.php?p=competitions_list':
            echo 
    '
            <h1>Competitions List</h1>
            '
    ;
        break;

        default:
            echo 
    '
                <h1>Competitions</h1>

            <p>When becoming a member of TSA, students will have the opportunity to participate in technology competitions and activities that help prepare them for into college and/or the work force. At the regional, state, and national level, students are able to participate in over 60 competitive events in any of the 33 different engineering specialties areas.</p>

            <p>As students work on projects, they learn to work as a team, to accept and give recognition and to assist others. TSA students will also gain valuable Science, Technology, Engineering, and Mathematical skills needed in real world situations.</p>

            <p class="blue">For a detail list of competitions, detail descriptions, guidelines, and rubrics, access the National TSA website. (<a href="http://www.tsaweb.org/Competitions" target="_parent">http://www.tsaweb.org/Competitions</a> )</p>
    <br />
    <div class="accordionButton">Regional Competition</div>
    <div class="accordionContent">
        <h1>Regional Competition Info</h1>
    </div>

    <div class="accordionButton">State Competition</div>
    <div class="accordionContent">
        <h1>State Competition Info</h1>
    </div>

    <div class="accordionButton">National Competition</div>
    <div class="accordionContent">
        <h1>National Competition Info</h1>
    </div>
            '
    ;
        break;

    }

    ?>
    With the exception of different variable names, the scripts are the same. Did I miss something?
    [COLOR=rgb(0, 51, 102)]Student Web Designer[/COLOR]
    [COLOR=rgb(0, 51, 102)]If you thought something I said was helpful. LIKE IT![/COLOR]

  4. #3
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    PHP Code:
     
    <?php

    $p 
    $_GET['p'];
    switch(
    $p) {

        case 
    'our_projects':
            echo 
    '
            <h1>Our Projects</h1>
                <p>This is some test text</p>
            '
    ;
        break;

        case 
    'dress_code':
            echo 
    '
            <h1>Dress Code</h1>
            '
    ;
        break;

        case 
    'code_of_conduct':
            echo 
    '
            <h1>Code of Conduct</h1>
            '
    ;
        break;

        case 
    'competitions_list':
            echo 
    '
            <h1>Competitions List</h1>
            '
    ;
        break;

        default:
            echo 
    '
                <h1>Competitions</h1>

            <p>When becoming a member of TSA, students will have the opportunity to participate in technology competitions and activities that help prepare them for into college and/or the work force. At the regional, state, and national level, students are able to participate in over 60 competitive events in any of the 33 different engineering specialties areas.</p>

            <p>As students work on projects, they learn to work as a team, to accept and give recognition and to assist others. TSA students will also gain valuable Science, Technology, Engineering, and Mathematical skills needed in real world situations.</p>

            <p class="blue">For a detail list of competitions, detail descriptions, guidelines, and rubrics, access the National TSA website. (<a href="http://www.tsaweb.org/Competitions" target="_parent">http://www.tsaweb.org/Competitions</a> )</p>
    <br />
    <div class="accordionButton">Regional Competition</div>
    <div class="accordionContent">
        <h1>Regional Competition Info</h1>
    </div>

    <div class="accordionButton">State Competition</div>
    <div class="accordionContent">
        <h1>State Competition Info</h1>
    </div>

    <div class="accordionButton">National Competition</div>
    <div class="accordionContent">
        <h1>National Competition Info</h1>
    </div>
            '
    ;
        break;

    }

    ?>
    Blackhawk095 likes this.


  5. #4
    Senior Member Blackhawk095's Avatar
    Join Date
    May 2011
    Location
    Kentucky (USA)
    Posts
    384
    Member #
    27969
    Liked
    45 times
    Thank you for helping me.

    But for the sake of knowing, why did this work:
    PHP Code:
    case 'articles.php?date=1-16-2012&id=1'
    and not this:
    PHP Code:
    competitions.php?p=our_projects 
    Both sets of code use the main file name (example: 'competitions.php') and then add the extra code to the end of the URL, but one script worked, and the other didn't.

    Could you explain why so I know for the future?
    [COLOR=rgb(0, 51, 102)]Student Web Designer[/COLOR]
    [COLOR=rgb(0, 51, 102)]If you thought something I said was helpful. LIKE IT![/COLOR]

  6. #5
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    Because the first one isn't working either.

    I click on News Topic 1, 2 or 3 and all of them display: Jan 16, 2012


  7. #6
    Senior Member Blackhawk095's Avatar
    Join Date
    May 2011
    Location
    Kentucky (USA)
    Posts
    384
    Member #
    27969
    Liked
    45 times
    That is strange. They are all working for me.
    [COLOR=rgb(0, 51, 102)]Student Web Designer[/COLOR]
    [COLOR=rgb(0, 51, 102)]If you thought something I said was helpful. LIKE IT![/COLOR]

  8. #7
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,717
    Member #
    5580
    Liked
    718 times
    See attached ...
    Attached Images Attached Images


  9. #8
    Senior Member Ganners's Avatar
    Join Date
    Feb 2011
    Location
    United Kingdom
    Posts
    415
    Member #
    27007
    Liked
    92 times
    Blackhawk, what you have to understand is when you switch ($date && $id), you're effectively looking for any argument that is true (if these are both true, or not null - which they are). So to prove this, try changing:

    Code:
    $date = $_GET['date'];
    $id = $_GET['id'];
    to

    Code:
    $date = false;
    $id = false;
    You'll see that nothing is selected. This is because each of your cases is true (as they are not NULL) so it will default to the first case.
    Mark Gannaway Software Developer

    Recent Experiments
    - Backpropogation Neural Network language solving (http://ann.ganners.co.uk/)
    - Animated image to ASCII (http://google.ganners.co.uk/)
    - 3D Paper Characters (http://cybergame.ganners.co.uk/)
    - Anagram solving (http://roflol.co.uk/)

  10. #9
    Senior Member Blackhawk095's Avatar
    Join Date
    May 2011
    Location
    Kentucky (USA)
    Posts
    384
    Member #
    27969
    Liked
    45 times
    Quote Originally Posted by mlseim, post: 231114
    See attached ...
    Quote Originally Posted by Ganners, post: 231116
    Blackhawk, what you have to understand is when you switch ($date && $id), you're effectively looking for any argument that is true (if these are both true, or not null - which they are). So to prove this, try changing:

    Code:
    $date = $_GET['date'];
    $id = $_GET['id'];
    to

    Code:
    $date = false;
    $id = false;
    You'll see that nothing is selected. This is because each of your cases is true (as they are not NULL) so it will default to the first case.
    I finally noticed the problem with the script. I was getting on here to ask about it, but I guess you guys beat me to it. :redcard:

    For now, I'll do some research and try to figure out the solution myself. If I have too much trouble, I'll ask how to fix the script on this thread.
    [COLOR=rgb(0, 51, 102)]Student Web Designer[/COLOR]
    [COLOR=rgb(0, 51, 102)]If you thought something I said was helpful. LIKE IT![/COLOR]

  11. #10
    Senior Member Blackhawk095's Avatar
    Join Date
    May 2011
    Location
    Kentucky (USA)
    Posts
    384
    Member #
    27969
    Liked
    45 times
    I have fixed the problem....and I completely forgot about this thread. I have decided to keep it simple and combine the variables.
    PHP Code:
    <?php

    $articleID 
    1-1-12_id_1//Instead of comparing two variables ($date and $id), I pushed them together to create one single variable)

    switch($articleID) {

    //code continues
    [COLOR=rgb(0, 51, 102)]Student Web Designer[/COLOR]
    [COLOR=rgb(0, 51, 102)]If you thought something I said was helpful. LIKE IT![/COLOR]


Remove Ads

Posting Permissions

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

Search tags for this page

simple php content switch script

Click on a term to search for related topics.
All times are GMT -6. The time now is 12:49 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com