dslreports logo
site
 
    All Forums Hot Topics Gallery
spc

spacer




how-to block ads


Search Topic:
uniqs
735
share rss forum feed


Ashke
Flips page and continues reading
Premium
join:2007-09-11
Minneapolis, MN

2 edits

foreach array help?

I'm having trouble running the foreach function for the arrays.

I'm trying to create a breadcrumb engine of my own.... I have 3 different arrays. each of them is each tier in the directory.
<?php
    echo 'breadcrumbs.php speaking...<br>' . $new;
    echo 'Declaring variables....<br>' . $new;
    //Variable declarations
    //The ones below are
    //used for creating
    //breadcrumbs.
    $breadcrumbs = '';
    $originalURI = '';
    $funstuff = '';
    $favorites = '';
    $firstLevel = '';
    
 
    echo 'Assigning default values...<br>' . $new;
    //Assignment of default
    //values
    $breadcrumbs .= 'home';
    
    // Done in this way:
    // array('Page Title' => 'page_name');
 
    @$firstLevel .= array('Home' => 'home',
                        'About' => 'about',
                        'Resume' => 'resume',
                        'Fun Stuff' => 'funstuff',
                        'Contact Me' => 'contact',
                        'Site Map' => 'sitemap');
    $funstuff = array('My Tips' => 'tips',
                    'Favorites' => 'favorites');
    $favorites = array('Comics' => 'favorite_comics',
                    'Posts' => 'favorite_posts', 
                    'Quotes' => 'favorite_quotes',
                    'Sites' => 'favorite_sites');
    
    echo '...done<br>' . $new;
    echo '$breadcrumbs =' . $breadcrumbs . '<br>' . $new;
    
    echo '$firstLevel =<br>';
    foreach ($firstLevel as $one =< $two){
        echo "&nbsp; &nbsp; &nbsp;" . $one . ' => ' . $two . '<br>';
    }
    echo '$funstuff =<br>';
    foreach($funstuff as $value){
        echo "&nbsp; &nbsp; &nbsp;" . $value . '<br>';
    }
    unset($value);
    
    echo '$favorites =<br>';
    foreach ($favorites as $value){
        echo "&nbsp; &nbsp; &nbsp;" . $value . '<br>';
    }
    unset($value);
    
    echo 'proceeding with breadcrumbing<br><br>' . $new;
    
    
    //Obtains the URI
    if (isset($_GET['page'])){
        $originalURI = $_GET['page'];
    }
    echo '$originalURI =' . $originalURI . '<br><br>' . $new;
    if($originalURI != 'home'){
        $breadcrumbs .= ' >> ';
        foreach($funstuff as $check){
            if($originalURI == $check){
                $breadcrumbs .= 'Fun Stuff >> ' . $check;
            }
        }
        unset($check);
        foreach($favorites as $check){
            if($originalURI == $check){
                $breadcrumbs .= ' >> ' . $check;
            }
        }
        unset($check);
        
    }
?>
 

I'm having trouble with the foreach part. I want it to look at each particle of the array.

If $_GET['page'] is equal to the page_name portion of the array, then I want it to add the Page Title to the breadcrumbs string. But it's showing errors as in invalid argument when I try to do that. It says that it was expecting ')' at Line 40...

Can someone please help out?

--
-Ashke


cdru
Go Colts
Premium,MVM
join:2003-05-14
Fort Wayne, IN
kudos:7
Try
foreach ($firstLevel as $one => $two){


Ashke
Flips page and continues reading
Premium
join:2007-09-11
Minneapolis, MN
said by cdru:

Try
foreach ($firstLevel as $one => $two){

Nope... Didn't work.

This is the error message that I am getting... Warning: Invalid argument supplied for foreach() in C:\wamp\www\template\breadcrumbs.php on line 40
--
-Ashke


Ashke
Flips page and continues reading
Premium
join:2007-09-11
Minneapolis, MN
reply to cdru
Sorry if my code seems to be the stating-the-stupid's-obvious way. I'm just putting it into debugging mode until the code works out smoothly. Helps me pin point the errors.
--
-Ashke


cdru
Go Colts
Premium,MVM
join:2003-05-14
Fort Wayne, IN
kudos:7
reply to Ashke
I corrected a typo immediately, but it looks like you used the original one. The =< should have been => as is currently reflected in my post above.


Ashke
Flips page and continues reading
Premium
join:2007-09-11
Minneapolis, MN
said by cdru:

I corrected a typo immediately, but it looks like you used the original one. The =< should have been => as is currently reflected in my post above.

Not quite sure I follow...

The foreach is generally perplexing to me when it comes to multi-dimensional arrays.
--
-Ashke


Ashke
Flips page and continues reading
Premium
join:2007-09-11
Minneapolis, MN
reply to cdru
I tried all of the following:

foreach($firstLevel as $one => $two){
//Some code here
}
 
foreach ($firstLevel as $one =< $two){
//Some code here
}
 
foreach ($firstLevel as $one <= $two){
//Some code here
}
 
foreach ($firstLevel as $one >= $two){
//Some code here
}
 

None of the above work :(

--
-Ashke


Ashke
Flips page and continues reading
Premium
join:2007-09-11
Minneapolis, MN
Scratch that... I had a typo when I did my coding to do as you suggested... the => works.
--
-Ashke