Jump to content

Array problem


crowds40

Recommended Posts

Yes, you need to wrap echo "$val";  in curly braces

 <?php

$test = array('blue','green','white');
foreach($test as $val)
{
    echo "$val";
}
?>

If you are to go in and out of PHP mode then you should format your code like

<?php

$test = array('blue','green','white');

?>

<ul>
<?php foreach($test as $val): /* opens foreach block */ ?>
  <li><?php echo "$val"; ?></li>
<?php endforeach; /* closes foreach block */ ?>
</ul>
Edited by Ch0cu3r
Link to comment
Share on other sites

I had to take it one step further to:

 <?php 
  $test = array('blue','green','white');
foreach($test as $val)
$testValues[] = "color=$val"; 

echo "<a href='?";
echo join("&", $testValues);
echo "'>Color</a>"; ?>

The url is "website.com?color=blue&color=green&color=white"

 

Again this this will only return the white and not the blue, green, and white. Hope that makes sense. Any ideas?

Link to comment
Share on other sites

your link is repeating the color= parameter, so each value in turn is overwriting the previous value when php populates the $_GET['color'] variable.

 

 

what overall goal are you trying to accomplish? a separate link for each possible color or one link that contains all the values from the array?

Link to comment
Share on other sites

@crowds40, your question lacks context and definition that would would allow us to answer it (it didn't even start as being a question about making one url, which means that the answers given at first didn't apply to the actual problem.)

 

you have an array of one or more colors. you either need to combine them into one url parameter, i.e. ?color=blue|green|white or you need to use a separate parameter in the url for each one (which could actually be an array name so that php would receive an array when the url is submitted.)

 

which method you use depends on what you are using the values for, how cluttered/long you want to make the url, and how much code you want to write to extract the values when the link gets submitted.

Edited by mac_gyver
Link to comment
Share on other sites

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.