imarockstar Posted October 12, 2009 Share Posted October 12, 2009 I have a script that echos out sample code .. and it works fine .. but when it prints each block of code out ... there is a comma at the end of each array .. i know why it there .. but i dont know a trick to get it off there .. here is an example ... if there are 5 form fields, each asking for a variable name and the user enters this : name email phone city state then the array is : name,email,city,state, you can see that there is a COMMA after state, and you can see below that I am putting it there so the code output it valid. However i dont need the code on the last post entry. is there a way i can take that out ? here is my code : $tablename = $_POST['tablename']; $header = $_POST['header']; if ( isset($_POST['v']) ) { $vary = implode(',', $_POST['v']); } else { $vary = ""; } ?> <code clss="getcode"> <p> <?php $a = explode(',', $vary); foreach ($a as $v) { echo "$".$v."=\$_POST['" . $v . "']; <br>"; } ?> </p> </code> <h4 class="codehead">Insert Code</h4> <code clss="getcode"> <p> $sql="INSERT INTO <?php echo $_POST['tablename'];?> ( <?php $a = explode(',', $vary); foreach ($a as $v) { echo "".$v.","; } ?> ) <br> VALUES ( <?php $a = explode(',', $vary); foreach ($a as $v) { echo "'".$v."',"; } ?> ); </p> </code> <h4 class="codehead">Update Code</h4> <code clss="getcode"> <p> mysql_query("UPDATE merch SET <?php $a = explode(',', $vary); foreach ($a as $v) { echo "".$v."='".$v."',"; } ?> "); </p> </code> <code clss="getcode"> <p> $result = mysql_query($sql) or die("ERROR: " . mysql_error() . "SQL: " . $sql); <br> header("Location: <?php echo $_POST['header'];?>"); </p> <br> </code> Link to comment https://forums.phpfreaks.com/topic/177437-solved-i-am-having-a-comma-issue/ Share on other sites More sharing options...
marcus Posted October 12, 2009 Share Posted October 12, 2009 an example: <?php $input = "value1,value2,value3,value4"; $ex = explode(",",$input); $c = count($ex); $x = 1; foreach($ex AS $cat){ $comma = ($c == $x) ? "" : ","; echo "'".$cat."'".$comma; $x++; } ?> Link to comment https://forums.phpfreaks.com/topic/177437-solved-i-am-having-a-comma-issue/#findComment-935542 Share on other sites More sharing options...
Mark Baker Posted October 12, 2009 Share Posted October 12, 2009 $a = explode(',', $vary); array_pop($a); Link to comment https://forums.phpfreaks.com/topic/177437-solved-i-am-having-a-comma-issue/#findComment-935553 Share on other sites More sharing options...
imarockstar Posted October 12, 2009 Author Share Posted October 12, 2009 this is what i got .. but it not working .. <?php $a = explode(',', $vary); $c = count($a); $x = 1; foreach ($a as $v) { $comma = ($c == $x) ? "" : ","; echo "".$v."".$comma; } ?> ) <br> Link to comment https://forums.phpfreaks.com/topic/177437-solved-i-am-having-a-comma-issue/#findComment-935609 Share on other sites More sharing options...
lemmin Posted October 12, 2009 Share Posted October 12, 2009 I would do it like this so that you aren't making a comparison at every iteration: <?php $string = ""; $a = explode(',', $vary); foreach ($a as $v) { $string .= "'".$v."',"; } echo substr_replace($string, null, -1); ?> Though I don't understand why you are taking an array, turning it into a string, turning it back into an array and then, finally, turning it back into a string again. Link to comment https://forums.phpfreaks.com/topic/177437-solved-i-am-having-a-comma-issue/#findComment-935614 Share on other sites More sharing options...
mikesta707 Posted October 12, 2009 Share Posted October 12, 2009 just use implode? $arr = (1, 2, 3); echo implode(',', $arr); //output: 1,2,3 Link to comment https://forums.phpfreaks.com/topic/177437-solved-i-am-having-a-comma-issue/#findComment-935616 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.