Jump to content

Recommended Posts

Alright... Im getting some errors with my foreach loop..

Can anyone tell me whats wrong with it?

 

It does print like it should correctly, but i get this error also:

Invalid argument supplied for foreach() in C:\xxxxx\xxxxx\xxxxx on line xxx.

 

Note: It do print the things it's supposed to - but the error comes anyway? o.O

 

Here is the code:

<?php foreach($field_foo as $hai) {print $hai['value'] . ", ";}?>

 

And i will also like to write ", " while there is more.. But if its the last, i just want a ".".

 

Hope you understand (:

 

Regards,

Incredinot

Link to comment
https://forums.phpfreaks.com/topic/191594-help-with-simple-foreach/
Share on other sites

Providing $field_foo is an array (which the error suggests its not), it would be much simpler & more efficient to use implode.

 

<?php echo implode(', ', $field_foo); ?>

 

If i use the above code, it just print "Array, Array, Array, Array "

And if i write something like:

 

<?php echo implode(', ', $field_foo['value']); ?>

i get nothing..

Dont really know what your array looks like but see if this works.

 

<?php 
$i=1;
$num=count($field_foo);
foreach($field_foo as $hai) 
{
if ($i != $num)
{
print $hai['value'] . ", ";
}
else
{
print $hai['value'] . ".";
}
$i++;
}
?>

 

 

HTH

Teamatomic

This is what i get from:

<pre><?php print_r($node->field_spillesteder);?></pre>

 

Array
(
    [0] => Array
        (
            [value] => Vestjylland
            [safe] => Vestjylland
            [view] => Vestjylland
        )

    [1] => Array
        (
            [value] => Nordjylland
            [safe] => Nordjylland
            [view] => Nordjylland
        )

    [2] => Array
        (
            [value] => Sjælland
            [safe] => Sjælland
            [view] => Sjælland
        )

    [3] => Array
        (
            [value] => Fyn
            [safe] => Fyn
            [view] => Fyn
        )

)

Alright, let me explain (:

 

I have this "page".

 

When i create a page i have a select box where i can select different things.

 

To be exact it is:

 

Sydjylland

Vestjylland

Midtjylland

Nordjylland

Sjælland

Fyn

Bornholm

 

In the above example, i have chosen "Vestjylland, Nordjylland, Sjælland, Fyn".

 

Now i want to output that this it what i have chosen.

 

Hope you understand

<select multiple="multiple" name="field_spillesteder[value][]">
<option value="Sydjylland">Sydjylland</option>
<option selected="selected" value="Vestjylland">Vestjylland</option>
<option value="Midtjylland">Midtjylland</option>
<option selected="selected" value="Nordjylland">Nordjylland</option>
<option selected="selected" value="Sjælland">Sjælland</option>
<option selected="selected" value="Fyn">Fyn</option>
<option value="Bornholm">Bornholm</option>
</select>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.