dennismonsewicz Posted September 30, 2008 Share Posted September 30, 2008 I have this code: <?php include "includes/sql.php"; $action = $_GET['action']; switch($action) { case "foreach": echo '<form action="foreach.php?action=results" method="post">'; echo '<div><input type="checkbox" name="checkboxes[pm1]" class="checkbox" id="pm1" value="1" /> <label for="pm1">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[designer1]" class="checkbox" id="designer1" value="1" /> <label for="designer1">Designer</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm2]" class="checkbox" id="pm2" value="1" /> <label for="pm2">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[writer]" class="checkbox" id="writer" value="1" /> <label for="writer">Writer</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm3]" class="checkbox" id="pm3" value="1" /> <label for="pm3">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[designer2]" class="checkbox" id="designer2" value="1" /> <label for="designer2">Designer</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm4]" class="checkbox" id="pm4" value="1" /> <label for="pm4">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[christine]" class="checkbox" id="christine" value="1" /> <label for="christine">Christine</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm5]" class="checkbox" id="pm5" value="1" /> <label for="pm5">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[designer3]" class="checkbox" id="designer3" value="1" /> <label for="designer3">Designer</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm6]" class="checkbox" id="pm6" value="1" /> <label for="pm6">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[julie]" class="checkbox" id="julie" value="1" /> <label for="julie">Julie</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm7]" class="checkbox" id="pm7" value="1" /> <label for="pm7">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[designer4]" class="checkbox" id="designer4" value="1" /> <label for="designer4">Designer</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm8]" class="checkbox" id="pm8" value="1" /> <label for="pm8">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[proofreading]" class="checkbox" id="proofreading" value="1" /> <label for="proofreading">Proofreading</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm9]" class="checkbox" id="pm9" value="1" /> <label for="pm9">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[layne]" class="checkbox" id="layne" value="1" /> <label for="layne">Layne</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm10]" class="checkbox" id="pm10" value="1" /> <label for="pm10">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[programming]" class="checkbox" id="programming" value="1" /> <label for="programming">Programming</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm11]" class="checkbox" id="pm11" value="1" /> <label for="pm11">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[testing]" class="checkbox" id="testing" value="1" /> <label for="testing">Testing</label></div>'; echo '<input type="submit" value="Submit" />'; echo '</form>'; break; case "results": foreach($_POST['checkboxes'] as $key=>$val) { $qry = mysql_query("INSERT INTO fake ($key) VALUES ($val)") or die(mysql_error()); if($qry) { echo "Check: $key, Value: $val <br />"; } } break; } ?> When I fill out the form it submits but creates individual rows with results and not all of the results in the same row Any help is appreciated! Link to comment https://forums.phpfreaks.com/topic/126507-looping-problems/ Share on other sites More sharing options...
sKunKbad Posted September 30, 2008 Share Posted September 30, 2008 Do you mean that you want all of the key=>val pairs in one database row, or do you want all of the key=>value pairs on one line of HTML? Both? Link to comment https://forums.phpfreaks.com/topic/126507-looping-problems/#findComment-654217 Share on other sites More sharing options...
dennismonsewicz Posted September 30, 2008 Author Share Posted September 30, 2008 both is fine, I am only printing them out to show the results Link to comment https://forums.phpfreaks.com/topic/126507-looping-problems/#findComment-654219 Share on other sites More sharing options...
sKunKbad Posted September 30, 2008 Share Posted September 30, 2008 $val=''; foreach($_POST['checkboxes'] as $key=>$val) { $data = $key . "," . $val . ","; } $qry = mysql_query("INSERT INTO fake ('data') VALUES ($data)") or die(mysql_error()); Something like this would take all of the keys and associated values, and create a comma delimited string that is inserted into the fake table, in the data column. Link to comment https://forums.phpfreaks.com/topic/126507-looping-problems/#findComment-654227 Share on other sites More sharing options...
dennismonsewicz Posted September 30, 2008 Author Share Posted September 30, 2008 ok so i have it working with this code (but with a small problem) $val=''; foreach($_POST['checkboxes'] as $key=>$val) { $data_name = $key . ","; $data_value = $val . ","; } $qry = mysql_query("INSERT INTO fake ($data_name) VALUES ('$data_value')") or die(mysql_error()); The problem is, is that there is a comma that is created at the end of the $data_name var and its exploding the SQL statement. How can I truncate the last comma? Link to comment https://forums.phpfreaks.com/topic/126507-looping-problems/#findComment-654243 Share on other sites More sharing options...
QuietWhistler Posted September 30, 2008 Share Posted September 30, 2008 First of all, skunkbad made a mistake in his script. foreach($_POST['checkboxes'] as $key=>$val) { $data_name .= $key . ","; //mind the dot $data_value .= $val . ","; //mind the dot } $qry = mysql_query("INSERT INTO fake ($data_name) VALUES ('$data_value')") or die(mysql_error()); Now to get rid of the last comma, I always use this method: $i = 0; foreach($_POST['checkboxes'] as $key=>$val) { $data_name .= $key; $data_value .= $val; if( $i != sizeof( $_POST[ "checkboxes" ] ) - 1 ) { $data_name .= ","; $data_value .= ","; } $i++; } $qry = mysql_query("INSERT INTO fake ($data_name) VALUES ('$data_value')") or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/126507-looping-problems/#findComment-654263 Share on other sites More sharing options...
sKunKbad Posted September 30, 2008 Share Posted September 30, 2008 First of all, skunkbad made a mistake in his script. Considering the information given to us, there's no way to know what the OP wanted specifically. I was assuming that the key value pairs would all be inserted into a single database field (which I still believe is what was wanted???). Link to comment https://forums.phpfreaks.com/topic/126507-looping-problems/#findComment-654349 Share on other sites More sharing options...
dennismonsewicz Posted October 1, 2008 Author Share Posted October 1, 2008 the problem i am having is the if statement is being hit as well as the data being processed in the foreach statement. Here is the updated code: <?php include "includes/sql.php"; $action = $_GET['action']; switch($action) { case "foreach": echo '<form action="foreach.php?action=results" method="post">'; echo '<div><input type="checkbox" name="checkboxes[pm1]" class="checkbox" id="pm1" value="1" /> <label for="pm1">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[designer1]" class="checkbox" id="designer1" value="1" /> <label for="designer1">Designer</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm2]" class="checkbox" id="pm2" value="1" /> <label for="pm2">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[writer]" class="checkbox" id="writer" value="1" /> <label for="writer">Writer</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm3]" class="checkbox" id="pm3" value="1" /> <label for="pm3">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[designer2]" class="checkbox" id="designer2" value="1" /> <label for="designer2">Designer</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm4]" class="checkbox" id="pm4" value="1" /> <label for="pm4">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[christine]" class="checkbox" id="christine" value="1" /> <label for="christine">Christine</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm5]" class="checkbox" id="pm5" value="1" /> <label for="pm5">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[designer3]" class="checkbox" id="designer3" value="1" /> <label for="designer3">Designer</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm6]" class="checkbox" id="pm6" value="1" /> <label for="pm6">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[julie]" class="checkbox" id="julie" value="1" /> <label for="julie">Julie</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm7]" class="checkbox" id="pm7" value="1" /> <label for="pm7">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[designer4]" class="checkbox" id="designer4" value="1" /> <label for="designer4">Designer</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm8]" class="checkbox" id="pm8" value="1" /> <label for="pm8">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[proofreading]" class="checkbox" id="proofreading" value="1" /> <label for="proofreading">Proofreading</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm9]" class="checkbox" id="pm9" value="1" /> <label for="pm9">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[layne]" class="checkbox" id="layne" value="1" /> <label for="layne">Layne</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm10]" class="checkbox" id="pm10" value="1" /> <label for="pm10">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[programming]" class="checkbox" id="programming" value="1" /> <label for="programming">Programming</label></div>'; echo '<div><input type="checkbox" name="checkboxes[pm11]" class="checkbox" id="pm11" value="1" /> <label for="pm11">PM</label></div>'; echo '<div><input type="checkbox" name="checkboxes[testing]" class="checkbox" id="testing" value="1" /> <label for="testing">Testing</label></div>'; echo '<input type="submit" value="Submit" />'; echo '</form>'; break; case "results": $i = 0; foreach($_POST['checkboxes'] as $key=>$val) { $data_name .= $key; $data_value .= $val; echo $data_name; if( $i != sizeof( $_POST[ "checkboxes" ] ) - 1 ) { $data_name .= ","; $data_value .= ","; } $i++; } $qry = mysql_query("INSERT INTO fake (" . $data_name . ") VALUES ('$data_value')") or die(mysql_error()); break; } ?> When I run the code and spit out the results, lets say I select the first two checkboxes and then click submit I receive this: pm1pm1,designer1 If I select three checkboxes: pm1pm1,designer1pm1,designer1,pm2 Any help? Link to comment https://forums.phpfreaks.com/topic/126507-looping-problems/#findComment-654641 Share on other sites More sharing options...
dennismonsewicz Posted October 1, 2008 Author Share Posted October 1, 2008 bump Link to comment https://forums.phpfreaks.com/topic/126507-looping-problems/#findComment-654715 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.