dennismonsewicz Posted October 1, 2008 Share Posted October 1, 2008 Here's the code: foreach($_POST['checkboxes'] as $key=>$val) { $data_name = $key . ","; $data_value .= $val; $length = strlen($data_name); $fname = trim(substr($data_name,$length-1,$length)); $fname = str_replace($fname, " ", $data_name); echo $fname; } It is taking out every single one of the commas from the $data_name var and I need it to only take out the last comma.. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/126622-solved-stripping-last-char-problem/ Share on other sites More sharing options...
Zhadus Posted October 1, 2008 Share Posted October 1, 2008 Use strrchr to find the last occurence and just remove that from the string with substr_replace. <?php $data_name = substr_replace($data_name, "", strrchr($data_name, ",")); ?> Quote Link to comment https://forums.phpfreaks.com/topic/126622-solved-stripping-last-char-problem/#findComment-654760 Share on other sites More sharing options...
dennismonsewicz Posted October 1, 2008 Author Share Posted October 1, 2008 the page is now just white Here is the updated code: foreach($_POST['checkboxes'] as $key=>$val) { $data_name = $key . ","; $data_value .= $val; $data_name = substr_replace($data_name, "", strrchr($data_name, ",")); echo $data_name; } Quote Link to comment https://forums.phpfreaks.com/topic/126622-solved-stripping-last-char-problem/#findComment-654764 Share on other sites More sharing options...
discomatt Posted October 1, 2008 Share Posted October 1, 2008 Use strrchr to find the last occurence and just remove that from the string with substr_replace. <?php $data_name = substr_replace($data_name, "", strrchr($data_name, ",")); ?> Unless strrchr returns false... equating to 0. Your best bet is rtrim( $str, ',' ); Quote Link to comment https://forums.phpfreaks.com/topic/126622-solved-stripping-last-char-problem/#findComment-654771 Share on other sites More sharing options...
dennismonsewicz Posted October 1, 2008 Author Share Posted October 1, 2008 ok so here is the latest problem i am having updated code: <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); 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) { $data_name = $key . ","; $data_value = $val; $data_name = rtrim($data_name, ','); echo $data_name; } /*$qry = mysql_query("INSERT INTO fake (" . $data_name . ") VALUES ('$data_value')") or die(mysql_error());*/ break; } ?> the problem is, is that your code is treating each $key as an individual answer and so it stripping each comma occurance. Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/126622-solved-stripping-last-char-problem/#findComment-654775 Share on other sites More sharing options...
The Little Guy Posted October 1, 2008 Share Posted October 1, 2008 How about this: $string = 'this, has, multiple, commas.'; $pos = strrpos($string,','); echo substr_replace($string, '',$pos,1); Quote Link to comment https://forums.phpfreaks.com/topic/126622-solved-stripping-last-char-problem/#findComment-654796 Share on other sites More sharing options...
dennismonsewicz Posted October 1, 2008 Author Share Posted October 1, 2008 Still the same problem updated code: <?php ini_set ("display_errors", "1"); error_reporting(E_ALL); 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) { $data_name = $key . ","; $data_value = $val; $pos = strrpos($data_name, ","); echo substr_replace($data_name, '',$pos, 1); } /*$qry = mysql_query("INSERT INTO fake (" . $data_name . ") VALUES ('$data_value')") or die(mysql_error());*/ break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/126622-solved-stripping-last-char-problem/#findComment-654803 Share on other sites More sharing options...
The Little Guy Posted October 1, 2008 Share Posted October 1, 2008 move my code outside of the foreach loop. also, if the last comma is ALWAYS at the end of the string, you could replace my code with: echo trim($data_name,','); Quote Link to comment https://forums.phpfreaks.com/topic/126622-solved-stripping-last-char-problem/#findComment-654806 Share on other sites More sharing options...
dennismonsewicz Posted October 1, 2008 Author Share Posted October 1, 2008 now its deleting everything in the string accept for the last string For Example: 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>'; If you run the above code then select two or more boxes: foreach($_POST['checkboxes'] as $key=>$val) { $data_name = $key . ","; $data_value = $val; } echo trim($data_name, ','); It will delete everyone of the answers in the checkboxes array except for the last one checked. So if you check box1, box2, box3 the foreach code will spit out only box3 Quote Link to comment https://forums.phpfreaks.com/topic/126622-solved-stripping-last-char-problem/#findComment-654856 Share on other sites More sharing options...
The Little Guy Posted October 1, 2008 Share Posted October 1, 2008 Try this: foreach($_POST['checkboxes'] as $key=>$val) { $data_name .= $key . ","; $data_value = $val; } echo trim($data_name, ','); Quote Link to comment https://forums.phpfreaks.com/topic/126622-solved-stripping-last-char-problem/#findComment-654860 Share on other sites More sharing options...
dennismonsewicz Posted October 1, 2008 Author Share Posted October 1, 2008 the code you provided worked! Here's a new problem lol foreach($_POST['checkboxes'] as $key=>$val) { $data_name .= $key . ","; $data_value .= $val . ","; } $trimmed_name = trim($data_name, ','); $trimmed_value = trim($data_value, ','); $qry = mysql_query("INSERT INTO fake ($trimmed_name) VALUES ('$trimmed_value')") or die(mysql_error()); I am receiving the following mysql error: Column count doesn't match value count at row 1 any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/126622-solved-stripping-last-char-problem/#findComment-654869 Share on other sites More sharing options...
Zhadus Posted October 1, 2008 Share Posted October 1, 2008 Lots of ideas What does your table setup in your DB look like? According to the code you have there, you should have one column that is the same as your $trimmed_name. Quote Link to comment https://forums.phpfreaks.com/topic/126622-solved-stripping-last-char-problem/#findComment-654887 Share on other sites More sharing options...
sasa Posted October 1, 2008 Share Posted October 1, 2008 try <?php $data_name = '`'.implode('`,`',array_flip($_POST['checkboxes'])).'`'; $data_value = "'".implode("','",$_POST['checkboxes'])."'"; $qry = mysql_query("INSERT INTO fake ($data_name) VALUES ($data_value)") or die(mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/126622-solved-stripping-last-char-problem/#findComment-654893 Share on other sites More sharing options...
dennismonsewicz Posted October 1, 2008 Author Share Posted October 1, 2008 The DB is setup with id being auto incremented and the primary key then the column names are the same names that match the checkbox names in the array. Each table cell holds a 0 until the information is passed to the DB that changes the 0 to a 1, essentially turning it "on". sasa I tried your code and it only uses the last value in the checkbox array, it deletes the rest. Quote Link to comment https://forums.phpfreaks.com/topic/126622-solved-stripping-last-char-problem/#findComment-654899 Share on other sites More sharing options...
dennismonsewicz Posted October 1, 2008 Author Share Posted October 1, 2008 I was doing some debugging and found out what my problem was Code: <?php foreach($_POST['checkboxes'] as $key=>$val) { $data_name .= $key . ","; $data_value .= $val . ","; } $trimmed_name = trim($data_name, ','); $trimmed_value = trim($data_value, ','); $qry = "INSERT INTO fake ($trimmed_name) VALUES ('$trimmed_value')"; echo $qry; /*$result = mysql_query($qry) or die(mysql_error());*/ ?> Is spitting out: INSERT INTO fake (pm1,designer1) VALUES ('1,1') So its not treating the ones as individual answers but as one... anyway to fix this? Quote Link to comment https://forums.phpfreaks.com/topic/126622-solved-stripping-last-char-problem/#findComment-654957 Share on other sites More sharing options...
dennismonsewicz Posted October 1, 2008 Author Share Posted October 1, 2008 problem solved! Updated Code for anyone wanting to know how this works: foreach($_POST['checkboxes'] as $key=>$val) { $data_name .= $key . ","; $data_value .= "'" . $val . "'" . ","; } $trimmed_name = trim($data_name, ','); $trimmed_value = trim($data_value, ','); $qry = mysql_query("INSERT INTO fake ($trimmed_name) VALUES ($trimmed_value)")or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/126622-solved-stripping-last-char-problem/#findComment-654975 Share on other sites More sharing options...
sasa Posted October 1, 2008 Share Posted October 1, 2008 remove ' from query Quote Link to comment https://forums.phpfreaks.com/topic/126622-solved-stripping-last-char-problem/#findComment-654979 Share on other sites More sharing options...
The Little Guy Posted October 1, 2008 Share Posted October 1, 2008 I would do this instead: foreach($_POST['checkboxes'] as $key=>$val) { $data_name .= $key . ","; $data_value .= "'" . $val . "'" . ","; } $trimmed_name = trim($data_name, ','); $trimmed_value = trim($data_value, ','); $qry = mysql_query(sprintf("INSERT INTO fake ('%s') VALUES ('%s')", mysql_real_escape_string($trimmed_name), mysql_real_escape_string($trimmed_value)))or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/126622-solved-stripping-last-char-problem/#findComment-655017 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.