ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Link Info, Arrays, and Foreach

December 25, 2008 3:41am

Subscribe [2]
  • #1 / Dec 25, 2008 3:41am

    Bl4cKWid0w

    42 posts

    I have a controller that stores the navigation link information in an array.  Then I use a foreach statement in my view file to load the link information.  I do this so I can easily use a link divider image in my menu.  The problem is that I get a CI error saying that an invalid foreach argument has been supplied, even though the code is correct compared to PHP.net’s examples of a foreach.  This is my controller code:

    $data['links'] = array(
                                   'Home' => '',
                                   'Forum' => $data['url']."/forums/index.php",
                                   'Support' => $data['url']."/support",
                                   'Roster' => $data['url']."/roster",
                                   'Events' => $data['url']."/events",
                                   'Prospects' => $data['url']."/prospects",
                                   'Contact' => $data['url']."/contact"
                                   );
            $data['link'] = implode("".$data[", $data['links']);
    
    $data['user'] = array(
                                      'Login' => $data['url']."/login",
                                      'Register' => $data['url']."/register",
                                      'Find Pass' => $data['url']."/login/forgot"
                                      );
                $data['welcome'] = "Welcome, guest!";
    
            $data['user'] = implode(" | ", $data['user']);
            $this->load->view('overall_header', $data);

    This is my view code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html >
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title><?=$title; ?></title>
    <link rel=stylesheet href='<?=$url; ?>/system/application/views/style.css' /></head>
    
    <body class="twoColElsRtHdr">
    
    <div id="container">
      <div id="header">
        images/logo.gif
    <!-- end #header --></div>
      
      <div id='navigation'>
        <?php
        foreach($links as $name => $href): ?>
        <a href="http://&lt?=$href"><?=$name; ?></a>
        <?php endforeach; ?>
      </div>
            
      <div id='usermenu'>
          <?=$welcome; ?>
    
        <?php
        foreach($user as $name => $href): ?>
        <a href="http://&lt?=$href"><?=$name; ?></a>
        <?php endforeach; ?>
      </div>
        
      <div id='body'>

    Thanks for any help.

  • #2 / Dec 26, 2008 2:40am

    Bl4cKWid0w

    42 posts

    bump

  • #3 / Dec 26, 2008 3:54am

    Murodese

    53 posts

    Your implode line is horribly broken.

    e; forums parsing things badly

  • #4 / Dec 26, 2008 6:58am

    davidbehler's avatar

    davidbehler

    708 posts

    As far as I can tell your $user var does not hold an array but a string. As you are using

    $data['user'] = implode(" | ", $data['user']);

    and implode() returns a string. Try without the implode part, that should work then.

  • #5 / Dec 26, 2008 8:56pm

    Bl4cKWid0w

    42 posts

    It does hold an array:

    $data['user'] = array(
                                      'Login' => $data['url']."/login",
                                      'Register' => $data['url']."/register",
                                      'Find Pass' => $data['url']."/login/forgot"
                                      );
                $data['welcome'] = "Welcome, guest!";
    
            $data['user'] = implode(" | ", $data['user']);
  • #6 / Dec 26, 2008 9:12pm

    Michael Wales's avatar

    Michael Wales

    2070 posts

    No, it was holding an array, but then you imploded it into a string. View the documentation for implode - it returns a string.

  • #7 / Dec 27, 2008 12:24am

    Bl4cKWid0w

    42 posts

    implode is mainly used to display arrays.  they use it in the examples.

    Taken from PHP.net:

    <?php
    
    $array = array('lastname', 'email', 'phone');
    $comma_separated = implode(",", $array);
    
    echo $comma_separated; // lastname,email,phone
    
    ?>
  • #8 / Dec 27, 2008 5:16am

    davidbehler's avatar

    davidbehler

    708 posts

    Yeah, you can use implode to display an array as a list of it’s values seperated by the given delimiter. To do that you simply echo the return value of the implode function which happens to be a string.

    From PHP.net:

    Return Values
    Returns a string containing a string representation of all the array elements in the same order, with the glue string between each element.

    Here you assing an array to your $user var:

    $data['user'] = array(
                                      'Login' => $data['url']."/login",
                                      'Register' => $data['url']."/register",
                                      'Find Pass' => $data['url']."/login/forgot"
                                      );

    And here you turn that array into an string by assigning the return value of the implode function to your $user var

    $data['user'] = implode(" | ", $data['user']);

    Simply remove that last line and your code should work.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases