crowds40 Posted June 29, 2014 Share Posted June 29, 2014 For some reason this array will only return 'white'. Is there a problem with the code? <?php $test = array('blue','green','white'); foreach($test as $val) ?> <?php echo "$val"; ?> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted June 29, 2014 Share Posted June 29, 2014 (edited) 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 June 29, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 29, 2014 Share Posted June 29, 2014 Why are you exiting php mode in your foreach?? <?php $test = array('blue','green','white'); foreach($test as $val) echo "$val<br>"; exit(); Quote Link to comment Share on other sites More sharing options...
crowds40 Posted June 29, 2014 Author Share Posted June 29, 2014 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? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 29, 2014 Share Posted June 29, 2014 Works for me. Although you might want to change & to just Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted June 29, 2014 Share Posted June 29, 2014 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? Quote Link to comment Share on other sites More sharing options...
crowds40 Posted June 29, 2014 Author Share Posted June 29, 2014 Hi mac_gyver, I am trying to have one link that will contain the 'blue' 'green' and 'white' values from the array Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted June 29, 2014 Share Posted June 29, 2014 what format do you want the link to be in, because it's impossible to write code without knowing what end result you are trying to produce. Quote Link to comment Share on other sites More sharing options...
crowds40 Posted June 29, 2014 Author Share Posted June 29, 2014 I don't exactly know what you mean by what format. Sorry I am new to this. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted June 29, 2014 Share Posted June 29, 2014 So you want to output a separate link for each color in your array? <?php $colors = array('blue','green','white'); foreach($colors as $color) { echo '<a href="page.php?color='. $color. '">' . $color. '</a><br />'; } ?> Quote Link to comment Share on other sites More sharing options...
crowds40 Posted June 29, 2014 Author Share Posted June 29, 2014 Thanks for the response. I wish to output all three colors (blue,green,white) in the same link from the array if possible Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted June 29, 2014 Share Posted June 29, 2014 You need to answer mac_gyver question here, as you replies make no sense. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted June 30, 2014 Share Posted June 30, 2014 (edited) @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 June 30, 2014 by mac_gyver Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.