Welcome Guest, Not a member yet? Register   Sign In
Link Info, Arrays, and Foreach
#1

[eluser]Bl4cKWid0w[/eluser]
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:

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("<img src='".$data[' width='36' height='36' alt='&nbsp;' border='0'>", $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("&nbsp;|&nbsp;", $data['user']);
        $this->load->view('overall_header', $data);

This is my view code:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;&lt;?=$title; ?&gt;&lt;/title&gt;
&lt;link rel=stylesheet href='&lt;?=$url; ?&gt;/system/application/views/style.css' /&gt;&lt;/head>

&lt;body class="twoColElsRtHdr"&gt;

<div id="container">
  <div id="header">
    <img src="images/logo.gif" width="437" height="130" alt="Fourth Core" />
&lt;!-- end #header --&gt;</div>
  
  <div id='navigation'>
    &lt;?php
    foreach($links as $name => $href): ?&gt;
    <a href='&lt;?=$href; ?&gt;'>&lt;?=$name; ?&gt;</a>
    &lt;?php endforeach; ?&gt;
  </div>
        
  <div id='usermenu'>
      &lt;?=$welcome; ?&gt;<br />
    &lt;?php
    foreach($user as $name => $href): ?&gt;
    <a href='&lt;?=$href; ?&gt;'>&lt;?=$name; ?&gt;</a>
    &lt;?php endforeach; ?&gt;
  </div>
    
  <div id='body'>

Thanks for any help.
#2

[eluser]Bl4cKWid0w[/eluser]
bump
#3

[eluser]Murodese[/eluser]
Your implode line is horribly broken.

e; forums parsing things badly
#4

[eluser]davidbehler[/eluser]
As far as I can tell your $user var does not hold an array but a string. As you are using
Code:
$data['user'] = implode("&nbsp;|&nbsp;", $data['user']);
and implode() returns a string. Try without the implode part, that should work then.
#5

[eluser]Bl4cKWid0w[/eluser]
It does hold an array:
Code:
$data['user'] = array(
                                  'Login' => $data['url']."/login",
                                  'Register' => $data['url']."/register",
                                  'Find Pass' => $data['url']."/login/forgot"
                                  );
            $data['welcome'] = "Welcome, guest!";

        $data['user'] = implode("&nbsp;|&nbsp;", $data['user']);
#6

[eluser]Michael Wales[/eluser]
No, it was holding an array, but then you imploded it into a string. View the documentation for implode - it returns a string.
#7

[eluser]Bl4cKWid0w[/eluser]
implode is mainly used to display arrays. they use it in the examples.

Taken from PHP.net:
Code:
&lt;?php

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone

?&gt;
#8

[eluser]davidbehler[/eluser]
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:
Quote: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:
Code:
$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
Code:
$data['user'] = implode("&nbsp;|&nbsp;", $data['user']);

Simply remove that last line and your code should work.




Theme © iAndrew 2016 - Forum software by © MyBB