Jump to content

[SOLVED] Pleas Help with implode and comma


work_it_work

Recommended Posts

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!

 

 

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 :(

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

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 (')

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.