dennismonsewicz Posted September 30, 2008 Share Posted September 30, 2008 How would i turn the following code into a foreach statement? echo '<div><input type="checkbox" name="pm1" class="checkbox" id="pm1" /> <label for="pm1">PM</label></div>'; echo '<div><input type="checkbox" name="designer1" class="checkbox" id="designer1" /> <label for="designer1">Designer</label></div>'; echo '<div><input type="checkbox" name="pm2" class="checkbox" id="pm2" /> <label for="pm2">PM</label></div>'; echo '<div><input type="checkbox" name="writer" class="checkbox" id="writer" /> <label for="writer">Writer</label></div>'; echo '<div><input type="checkbox" name="pm3" class="checkbox" id="pm3" /> <label for="pm3">PM</label></div>'; echo '<div><input type="checkbox" name="designer2" class="checkbox" id="designer2" /> <label for="designer2">Designer</label></div>'; echo '<div><input type="checkbox" name="pm4" class="checkbox" id="pm4" /> <label for="pm4">PM</label></div>'; echo '<div><input type="checkbox" name="christine" class="checkbox" id="christine" /> <label for="christine">Christine</label></div>'; echo '<div><input type="checkbox" name="pm5" class="checkbox" id="pm5" /> <label for="pm5">PM</label></div>'; echo '<div><input type="checkbox" name="designer3" class="checkbox" id="designer3" /> <label for="designer3">Designer</label></div>'; echo '<div><input type="checkbox" name="pm6" class="checkbox" id="pm6" /> <label for="pm6">PM</label></div>'; echo '<div><input type="checkbox" name="julie" class="checkbox" id="julie" /> <label for="julie">Julie</label></div>'; echo '<div><input type="checkbox" name="pm7" class="checkbox" id="pm7" /> <label for="pm7">PM</label></div>'; echo '<div><input type="checkbox" name="designer4" class="checkbox" id="designer4" /> <label for="designer4">Designer</label></div>'; echo '<div><input type="checkbox" name="pm8" class="checkbox" id="pm8" /> <label for="pm8">PM</label></div>'; echo '<div><input type="checkbox" name="proofreading" class="checkbox" id="proofreading" /> <label for="proofreading">Proofreading</label></div>'; echo '<div><input type="checkbox" name="pm9" class="checkbox" id="pm9" /> <label for="pm9">PM</label></div>'; echo '<div><input type="checkbox" name="layne" class="checkbox" id="layne" /> <label for="layne">Layne</label></div>'; echo '<div><input type="checkbox" name="pm10" class="checkbox" id="pm10" /> <label for="pm10">PM</label></div>'; echo '<div><input type="checkbox" name="programming" class="checkbox" id="programming" /> <label for="programming">Programming</label></div>'; echo '<div><input type="checkbox" name="pm11" class="checkbox" id="pm11" /> <label for="pm11">PM</label></div>'; echo '<div><input type="checkbox" name="testing" class="checkbox" id="testing" /> <label for="testing">Testing</label></div>'; Quote Link to comment https://forums.phpfreaks.com/topic/126482-foreach-help/ Share on other sites More sharing options...
Maq Posted September 30, 2008 Share Posted September 30, 2008 Why do you need a foreach? Is this a static amount of checkboxes? You could just iteratively loop through. Quote Link to comment https://forums.phpfreaks.com/topic/126482-foreach-help/#findComment-654016 Share on other sites More sharing options...
dennismonsewicz Posted September 30, 2008 Author Share Posted September 30, 2008 yes it is a static number... I reckon I was trying to come up with a lazy way of producing the information lol... How would I save the post information into an array and then write a foreach statement that will loop through the results? Quote Link to comment https://forums.phpfreaks.com/topic/126482-foreach-help/#findComment-654026 Share on other sites More sharing options...
Maq Posted September 30, 2008 Share Posted September 30, 2008 I think you can do this (not tested!): for($x=1;x{ $post_var = "pm" . $x; $array = $_POST['$post_var']; } Quote Link to comment https://forums.phpfreaks.com/topic/126482-foreach-help/#findComment-654110 Share on other sites More sharing options...
QuietWhistler Posted September 30, 2008 Share Posted September 30, 2008 You can do it like this: <?php $checkboxes = array( "designer1" => "Designer", "pm1" => "PM", "writer" => "Writer", "pm2" => "PM" ); if( is_array( $checkboxes ) ) { foreach( $checkboxes as $key => $value ) { print( "<input type=\"checkbox\" name=\"" . $key . "\" class=\"checkbox\" id=\"" . $key . "\" /> <label for=\"" . $key . "\">" . $value . "</label></div> )"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/126482-foreach-help/#findComment-654291 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.