work_it_work Posted September 19, 2009 Share Posted September 19, 2009 here's the scenario: i have form filled in steps, at some step the user have to check some boxes and proceed further. at the end of the steps the form is submitted and the variables placed into db because the check boxes are generated to form a table and balance the columns i used: <?php //set 3 to 4 of you want 4 columns. Set it to 5 if you want 5, etc $numcols = 4; // how many columns to display $numcolsprinted = 0; // no of columns so far /******* Other options ********/ $options = "SELECT * FROM `options` WHERE object_id != '104' ORDER BY value"; $ores = mysql_query($options) or die(mysql_error()); while($or = mysql_fetch_assoc($ores)){ if ($numcolsprinted == $numcols) { print "</tr>\n<tr>\n"; $numcolsprinted = 0; } echo "<td width=25%><input type=checkbox name=opt[] value=".$or['object_id']." /> ".$or['value']."</td>\n"; // bump up row counter $numcolsprinted++; // end while loop } $colstobalance = $numcols - $numcolsprinted; for ($i=1; $i<=$colstobalance; $i++) { } print "<td></td>\n"; /******* End Options **********/ } ?> </table> this works perfect for columns then i have to pass the values to other file which stores them until the submission step which is like this: <input type="hidden" name="opt[]" value="<? if(isset($_POST['opt'])) { $options = implode(",",$_POST['opt']); }?><?=$options;?>" > then it process the php form and then another file inserts the rows in db $sql_insert_item = $this->query("UPDATE " . DB_PREFIX . "usertable SET a='" . implode(",", $_POST['opt']) . " WHERE auction_id='" . $user_details['user_id'] . "' AND owner_id='" . $owner_id . "'"); all this thing work, but not as i want it inserts into a field in db values like this ",4,21,23,26,30,2,53" instead of "4,21,23,26,30,2,53" it puts a comma before anything!! how can i avoid that? Please help! Link to comment https://forums.phpfreaks.com/topic/174793-solved-pleas-help-with-implode-and-comma/ Share on other sites More sharing options...
ozestretch Posted September 19, 2009 Share Posted September 19, 2009 Could try to remove empty values before imploding. Is like it's first value is empty!? Can you view source of the hidden fields to check? Link to comment https://forums.phpfreaks.com/topic/174793-solved-pleas-help-with-implode-and-comma/#findComment-921170 Share on other sites More sharing options...
work_it_work Posted September 19, 2009 Author Share Posted September 19, 2009 yes, i just change this <input type="hidden" name="opt[]" value="<? if(isset($_POST['opt'])) { $options = implode(",",$_POST['opt']); }?><?=$options;?>" > to <input type="text" name="opt[]" value="<? if(isset($_POST['opt'])) { $options = implode(",",$_POST['opt']); }?><?=$options;?>" > this way i preview the imploded options until they are submitted... there is no space before any option Link to comment https://forums.phpfreaks.com/topic/174793-solved-pleas-help-with-implode-and-comma/#findComment-921172 Share on other sites More sharing options...
ozestretch Posted September 19, 2009 Share Posted September 19, 2009 I am not sure, but this could be a ... dare i say it, short cut!? $getthecommaoutinanastyquickway = implode(",", $_POST['opt']); if ($getthecommaoutinanastyquickway[0] == ",") $getthecommaoutinanastyquickway= substr($getthecommaoutinanastyquickway, 1); $sql_insert_item = $this->query("UPDATE " . DB_PREFIX . "usertable SET a='" . $getthecommaoutinanastyquickway . " WHERE auction_id='" . $user_details['user_id'] . "' AND owner_id='" . $owner_id . "'"); EDIT: REMOVED TYPO Link to comment https://forums.phpfreaks.com/topic/174793-solved-pleas-help-with-implode-and-comma/#findComment-921185 Share on other sites More sharing options...
work_it_work Posted September 19, 2009 Author Share Posted September 19, 2009 don't work, it stops inserting values in db in column "a" .... Link to comment https://forums.phpfreaks.com/topic/174793-solved-pleas-help-with-implode-and-comma/#findComment-921205 Share on other sites More sharing options...
work_it_work Posted September 19, 2009 Author Share Posted September 19, 2009 anyone?! any suggestion? Link to comment https://forums.phpfreaks.com/topic/174793-solved-pleas-help-with-implode-and-comma/#findComment-921224 Share on other sites More sharing options...
ozestretch Posted September 19, 2009 Share Posted September 19, 2009 I missed a comma , take a looksie... sorry. $sql_insert_item = $this->query("UPDATE " . DB_PREFIX . "usertable SET a='" . $getthecommaoutinanastyquickway . "' WHERE auction_id='" . $user_details['user_id'] . "' AND owner_id='" . $owner_id . "'"); my typos are bad tonight (night here in australia) not a comma (,) an apost (') Link to comment https://forums.phpfreaks.com/topic/174793-solved-pleas-help-with-implode-and-comma/#findComment-921227 Share on other sites More sharing options...
work_it_work Posted September 19, 2009 Author Share Posted September 19, 2009 u're a little genius!! i never saw that missing character works now thanks! Link to comment https://forums.phpfreaks.com/topic/174793-solved-pleas-help-with-implode-and-comma/#findComment-921228 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.