php - Switch case only displaying the first key in an array -
good afternoon,
what i'm trying accomplish script program returns 3 relevant links based on selected areas of interest. on first page form use <input type="checkbox">
name
attribute set interests[]
store users selections in array. code block after jump form processing script echoes out links if form validates. i'm running trouble switch case function displaylinks();
displaying interests[0]
after form processed though print_r
shows
array ( [0] => web development [1] => startups [2] => video games )
if multiple interests selected 1 in first position of array displayed. ideas?
//success condition:: name interests if($_post["fullname"] != "" && (isset($_post['interests']))) { echo '<div class="interests">'; displaylinks();//links displayed when form validates echo '<a href="/exercise1">return start.</a>'; echo '</div>'; } function displaylinks(){ $interests = $_post['interests']; print_r($interests);//just see whats in array debugging switch (true) { case in_array("design", $interests): echo "<h2>design</h2>"; echo '<p><a href="http://dribbble.com" target="_blank">dribbble</a><br/>'; echo '<a href="http://news.layervault.com" target="_blank">designer news</a><br/>'; echo '<a href="http://beta.psdboard.com/" target="_blank">psdboard</a></p>'; break; case in_array("web development", $interests): echo "<h2>web development</h2>"; echo '<p><a href="http://jqapi.com/" target="_blank">jquery api</a><br/>'; echo '<a href="https://developers.google.com/speed/libraries/" target="_blank">google api library</a><br/>'; echo '<a href="https://developer.mozilla.org/en-us/" target="_blank">mozilla developer network</a></p>'; break; case in_array("startups", $interests): echo "<h2>startups</h2>"; echo '<p><a href="http://news.ycombinator.com" target="_blank">hacker news</a><br/>'; echo '<a href="http://andrewchen.co/" target="_blank">@andrewchen</a><br/>'; echo '<a href="http://techcrunch.com/" target="_blank">tech crunch</a></p>'; break; case in_array("video games", $interests): echo "<h2>video games</h2>"; echo '<p><a href="http://twitch.tv" target="_blank">twitchtv</a><br/>'; echo '<a href="http://www.giantbomb.com/" target="_blank">giant bomb</a><br/>'; echo '<a href="http://tap-repeatedly.com" target="_blank">tap repeatedly</a></p>'; break; case in_array("online magazines", $interests): echo "<h2>online magazines</h2>"; echo '<p><a href="https://medium.com/" target="_blank">medium</a><br/>'; echo '<a href="http://svbtle.com" target="_blank">svbtle</a><br/>'; echo '<a href="http://explorecreaterepeat.com/" target="_blank">explore create repeat</a></p>'; } }//end display links function
edit: in response first answer
this homework assignment , rubric gives more points using switch case.
you're using wrong tool. switch
statement designed select 1 condition. want whole string of individual if
blocks.
if (in_array("design", $interests)) { echo "<h2>design</h2>"; echo '<p><a href="http://dribbble.com" target="_blank">dribbble</a><br/>'; echo '<a href="http://news.layervault.com" target="_blank">designer news</a><br/>'; echo '<a href="http://beta.psdboard.com/" target="_blank">psdboard</a></p>'; } if (in_array("web development", $interests)) { echo "<h2>web development</h2>"; echo '<p><a href="http://jqapi.com/" target="_blank">jquery api</a><br/>'; echo '<a href="https://developers.google.com/speed/libraries/" target="_blank">google api library</a><br/>'; echo '<a href="https://developer.mozilla.org/en-us/" target="_blank">mozilla developer network</a></p>'; } // etc...
Comments
Post a Comment