Page 1 of 1

problem insert values into table using tokenizer..

Posted: Wed Sep 16, 2009 5:51 am
by chennaibala
hi frds...
in my hiden textbox.i have following values...
robert|true|true|false|arun|true|false|true|anu|true|true|false|

i want to splits in to token and insert in mysql table in following manner

namefield writefiled readfield speakfield
robert true true false
arun true false true
anu true true false

thanks in advance...

Re: problem insert values into table using tokenizer..

Posted: Wed Sep 16, 2009 6:30 am
by amargharat
You can't split with this kind of pattern
robert|true|true|false|arun|true|false|true|anu|true|true|false

Instead of above, use following pattern in your hidden field.
robert;true,true,false|arun;true,false,true|anu;true,true,false

using above pattern, we can split easily

here is the code

Code: Select all

  $pattern = "robert;true,true,false|arun;true,false,true|anu;true,true,false";
 
    $expPattern = explode("|", $pattern); // after spliting in this manner, you'll get each user and his/her properties.
    
    //loop it to store each namefield and his/ her properties
    foreach ($expPattern as $param)
    {
          $expParam = explode(";", $param);
          $namefield = $expParam[0];
          $properties = $expParam[1];
 
          //split $properties to get each property
          $expProperties = explode(",", $properties);
          $writefield = $expProperties[0];
          $readfield = $expProperties[1];
          $speakfield = $expProperties[2];
          
          $sql = "INSERT INTO table(namefield, writefield, readfield, speakfield) VALUES('{$namefield}', '{$writefield}', '{$readfield}', '{$speakfield}')"; 
          mysql_query($sql); 
    }