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 9 of 9
  1. #1
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    I was determined to fix this myself.. but to no avail!

    What I am trying to do is output a set of links (3 maximum) but the output should not include the active page ($PHP_SELF).

    I use 1 array for the names of the links (well not links but pages) and one link for the URLS of the pages.

    I then loop through the array (they are both the same size, or should be) and if array value is equal to the current page, do not display that array index.

    PHP Code:
    <?php

    // list possible pages separated by a comma
    $pages "Home,Contact,About,Images";
    $urls "/breadcrumb/index.php,/breadcrumb/contact.php, /breadcrumb/about.php,/breadcrumb/images.php";
    // get current page
    $current_page $_SERVER['PHP_SELF'];
    // explode the string $pages to put them into array
    $pages_list explode(",",$pages);
    // explode the urls
    $url_list explode(",",$urls);

    // loop, do not display the current active document

    for($i 0$i<= count($pages_list); $i++)
        {
            if(!
    $urls_list[$i] == "$current_page")
                {
                echo 
    $pages_list[$i] . ", ";
                }
        }
    ?>
    Thanks in advance...

  2.  

  3. #2
    Senior Member
    Join Date
    Aug 2003
    Posts
    444
    Member #
    2801
    Here is your problem. Amended version below...

    PHP Code:
    if($urls_list[$i] != "$current_page")
                {
                echo 
    $pages_list[$i] . ", ";
                } 
    eKstreme
    eKstreme.com - Free website tools!
    fontfox - free fonts Hand-picked quality fonts.

  4. #3
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    Thanks ekstreme.

    I also noted, when I exploded() I named it url_list and when I checked in loop I done urls_list. Who put that s there!

  5. #4
    Senior Member
    Join Date
    Aug 2003
    Posts
    444
    Member #
    2801
    Quote Originally Posted by bfsog
    Who put that s there!
    The evil bug monster?
    eKstreme
    eKstreme.com - Free website tools!
    fontfox - free fonts Hand-picked quality fonts.

  6. #5
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    One more thing.

    After I explode the string, I print out each array position's value and manually echo a comma.

    I do not want a comma appear if it is the last element in the array do not show a comma.

    PHP Code:
    $pages_list explode(",",$pages);
    $url_list explode(",",$urls);
    for(
    $i 0$i<= count($pages_list); $i++)
        {
        if(
    $url_list[$i] != "$current_page")
                    {
                       echo 
    $pages_list[$i];
               if (
    $i count($pages_list)) echo ", ";
                    }
        } 
    Any idea?

  7. #6
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    PHP Code:
    if ($i < (count($pages_list))-1) echo ", "
    S. Rosland

  8. #7
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    yeah! i am just behind you with that discovery, although I changed to sizeof() instead of count.

    Thanks

  9. #8
    Senior Member visualAd's Avatar
    Join Date
    Jan 2003
    Location
    Slough, UK
    Posts
    201
    Member #
    434
    You can also do it backwards - IMO its easier
    PHP Code:
    for($i 0$i<= count($pages_list); $i++) { 
        if(
    $url_list[$i] != "$current_page") { 
            if (
    $i != 0) echo(',');
            echo 
    $pages_list[$i]; 
        }


  10. #9
    Senior Member
    Join Date
    Aug 2003
    Posts
    444
    Member #
    2801
    Quote Originally Posted by visualAd
    You can also do it backwards - IMO its easier
    PHP Code:
    for($i 0$i<= count($pages_list); $i++) { 
        if(
    $url_list[$i] != "$current_page") { 
            if (
    $i != 0) echo(',');
            echo 
    $pages_list[$i]; 
        }

    Not only is it easier, but also faster (some tests bear that out).

    Also, this code can be optimised more: you are using count() in every iteration of the loop, which is slow. The faster way to do this is like this:

    PHP Code:
    $count count($pages_list);
    for(
    $i 0$i<= $count$i++) { 
        if(
    $url_list[$i] != "$current_page") { 
            if (
    $i != 0) echo(',');
            echo 
    $pages_list[$i]; 
        }

    Depending how large the array is, you may need further optimisation; search Google for "PHP loop optimization". It's quite interesting.
    eKstreme
    eKstreme.com - Free website tools!
    fontfox - free fonts Hand-picked quality fonts.


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
  •  
All times are GMT -6. The time now is 10:54 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com