Web Design and Development Forums

[SOLVED] foreach problem -- table of contents script

This is a discussion on "[SOLVED] foreach problem -- table of contents script" within the PHP Forum section. This forum, and the thread "[SOLVED] foreach problem -- table of contents script are both part of the Program Your Website category.


Go Back   Webforumz.com > Program Your Website > PHP Forum

Welcome to Webforumz.com.
Register Now Register now!

Reply
 
LinkBack Thread Tools Rate Thread
Old Apr 3rd, 2008, 13:29   #1 (permalink)
Moderator
 
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,155
[SOLVED] foreach problem -- table of contents script

I'm writing some PHP to generate a table of contents. The idea is for me to write the minimum code each time I make a TOC.

So normally, a single entry might look like this:

Code: Select all
<li><a href=\"".$host."/articles/serving-guide/badminton-serve-types.php\">The four types of serve</a></li>
Instead, I want PHP to create this code from (part of) a variable like this:

Code: Select all
$tocList = "||badminton-serve-types|The four types of badminton serve";
Here, I use the "||" to separate items and the "|" to separate the URL from the anchor text.

With me so far? That bit is okay, but I also need to nest lists to create subsections (one level of nesting). I do this by adding the signal characters "+" and "-" onto the end of the title. "+" signals "opening a nested list" and "-" signals "closing a nested list".

This part -- the nested lists -- is causing problems with my foreach loop. It only applies the test for the last entry; all other signalled entries are being ignored by the loop. Here's my full code:

Code: Select all
<?php
$tocArray = 
    "|Introduction
    ||basic-technique/|Basic serving technique+
        ||backhand-low-serve-technique|Backhand low serve
        ||backhand-flick-serve-technique|Backhand flick serve-
    ||tactics|Serving tactics";
    

$tocList=explode("||", $tocArray);
$directory = "";        // placeholder empty string;
foreach($tocList as $value) {
    $temp = explode("|",$value);
    $temp3 = trim($temp[1]); $temp4 = rtrim($temp3, "+-");            // removes nesting-signal characters from anchor text
    if ( (substr($temp[0], -1) != "/") && ( strlen($temp[0]) > 0) ) {$temp[0] .= ".php";}            // adds .php for all non-directory URLs
    $tocURL = $host."/articles/".$article."/".$directory.$temp[0];                               // creates a URL
    if ($tocURL == ($host.$path)) { $entry ="<li class=\"here\"><span>".$temp4."</span>"; }        // "you are here" formatting
    else { $entry = "<li><a href=\"".$tocURL."\">".$temp4."</a>"; }        // normal link
    
    $temp2 = substr($temp[1], -1);
    if ($temp2 == "+") {                // for the start of a section
        $directory = $temp[0];        // capturing the directory string for later
        echo $entry."<ol>\n\t";     // starting a nested list
        echo "DEBUG: I dont understand PHP";
    }
    if ($temp2 == "-") {                                // for the end of a section
        echo $entry."</li>\n\t</ol></li>";     // ending nested list
        $directory = "";        // resetting directory
    }
    if ($temp2 != "-" && $temp2 != "+") { echo $entry."</li>\n"; }
}
?>
The only way to activate my debug text is by changing the last item to have a signal "+", like this:

Code: Select all
 $tocArray = 
    "|Introduction
    ||basic-technique/|Basic serving technique+
        ||backhand-low-serve-technique|Backhand low serve
        ||backhand-flick-serve-technique|Backhand flick serve-
    ||tactics|Serving tactics+";
If I do this, then if ($temp2 == "+") evaluates as true, and the debug text appears.

What's going on?
MikeHopley is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 4th, 2008, 11:51   #2 (permalink)
Moderator
 
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,155
Re: foreach problem -- table of contents script

Problem solved (solutions in this thread at the WebSqueeze).
MikeHopley is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] &lt;c:forEach&gt; question pesho318i JavaScript Forum 2 Dec 16th, 2007 17:26
[SOLVED] Image script problem philsando JavaScript Forum 5 Oct 4th, 2007 13:58
Viewing the contents of a table in MySQL nemmea MySQL 3 Sep 6th, 2007 22:01
script to list folder contents as links taras JavaScript Forum 0 May 26th, 2007 09:59
Fastest way to dump a table contents? u2orange MSSQL & Access 6 Mar 1st, 2004 20:43



Latest Updates

All Points SEO Security Advisory - CHECK YOUR SITE NOW!

Creative Coding :: February 2008

Webforumz is sponsored by: WESH UK Web Hosting
All times are GMT. The time now is 10:59.

Sleep Study Scoring :: Free Bet :: Website Templates :: Online Betting :: Bookmakers :: Funny Quotes :: Internet Recruitment Software :: Microsoft CRM Experts :: Online Casino :: Decorated Christmas Trees :: Midwife Forums :: Football Betting :: Ecommerce Software :: Web Hosting :: Football Stats :: Dry Cleaning Collection :: xtreme wales - extreme clothing :: Apuestas :: Sharepoint Consultants :: Website Optimisation :: Office Clearance London :: Sharepoint Experts :: Sports Betting :: Casino :: Website Templates :: Web Design Development India :: Online Gambling

Powered by: vBulletin Version 3.7, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
© 2003-2008 Webforumz.com : All Rights Reserved
Search Engine Friendly URLs by vBSEO 3.2.0 RC6


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59