Jump to content

kosaks17

New Members
  • Posts

    9
  • Joined

  • Last visited

    Never

Posts posted by kosaks17

  1. This is the code that i've been using.. Please see below

     

    You should "save up" all the data and do one INSERT into the database. Where is the array of data coming from: a form POST, a file import or what? It appears that each 'record' in the array contains more data than you actually want to save to the database. Ideally, each element in the sub-array would have named keys (e.g. 'name', 'age, etc.) If not, you would need a process to translate what field is what

     

    Here is a general solution:

    $insertValues = array(); //Temp array to store values
    foreach($array as $id => $record)
    {
        $name  = $record[0];
        $age    = $record[1];
        $addr   = $record[2];
        $weight = $record[3];
        $height = $record[4];
    
        $insertValues[] = "('{$name}', '{$age}', '{$addr}', '{$weight}', '{$height}')";
    }
    
    //Create/run insert quer
    $query = "INSERT INTO table_name
                  (`name`, `age`, `address`, `weight`, `height`)
              VALUES " . implode(', ', $insertValues);
    $result = mysql_query($query);

     

    The code works fine but have problems when a field contain quotes, double quotes and commas.

    Ex : when $addr = "address 1, sample's street."

     

    This would cause an error when i try to save it into the database. the query string would give me this. Please see below

     

    <?php
    /* for example the implode results would be ('thomas','17','5 feet','60 kilograms','address 1, sample's street.') */
    $query = "INSERT INTO table_name
                  (`name`, `age`, `height`, `weight`, `address`)
              VALUES ('thomas','17','5 feet','60 kilograms','address 1, sample's street.');
      ?>

     

    as you can see the string on Values has an error since the bold information has a wrong format..

     

    VALUES ('thomas','17','5 feet','60 kilograms','address 1, sample's street.');

     

    can someone help me modify the code so that i could save my information whether or not it has a comma, quote and double quote?

     

    thanks guys

  2. Im trying to parse this string and put it in the array.. However i dont know how..

     

    11111<>your mobile<>the receiver<>keyword<>2012-01-15<>12:09:08<>wow "this", is great!

     

    I want to remove this sign <> and put the elements on the array.. Also i would not want to loose the quotes and commas

     

    Can someone help me?

     

    thanks

  3. Hello

     

    This is my array..

     

    Array
    (
        [0] => Array
            (
                [1] => Mike
                [2] => 17
                [3] => camboia
                [4] => 50kgs
                [5] => 5 feet
            )
    
        [1] => Array
            (
                [1] => anna
                [2] => 21
                [3] => peru
                [4] => 45kgs
                [5] => 6 feet
            )
    
    )

     

    Im using foreach loop. This is the code

     

    foreach($array as $h => $value) {
    foreach($value as $elements) {
    	echo $elements; // i want to divide the data into 5 elements then save it to database
    }
           echo '<br/><b> --> Break <-- </b>';
    }
    

     

    What i want is that to break the data everytime the second loop reach 5 turns then save it to database.. I know how to save it into the database but i dont know how to divide the data on the foreach loop..

     

    Could you help me? Im stuck on this problem..

     

    thanks in advance

  4. Hello to all

     

    I have problem a with my foreach loop..

     

    First : Is it possible to rename each element that's been outputted  everytime the loop execute?

    <?php
    $arr = array(mikel, 17, cambodia, 50kgs, 5 feet, anna, 21, peru, 45kgs, 6 feet);
    foreach ($arr as &$value) {
        $value = $value;
    }
    ?>
    

    What i would like on the result of the foreach loop is

    name : mikel

    Age : 17

    address : cambodia

    weight : 50kgs

    height : 5 feet

    is this possible?

     

    Second : i want to echo the result of the for each loop in every 5 turns.

    result 1 = mikel, 17, cambodia, 50kgs, 5 feet

    result 2 = anna, 21, peru, 45kgs, 6 feet

     

    Can anyone help me on this? thanks

  5. Hello guys

     

    I have here my array result

    Array ( [0] => Array ( [0] => 1 [1] => [2] => 123456789 [3] => [4] => 2012-02-02 [5] => 09:12:21 [6] => Test Array ) )

     

    I would like to view this result into a string so that it will remove the unnecessary array elements like brackets etc. Can some tell me how to handle this properly?

     

    The result would be : 1 123456789 2012-02-02 09:12:21

     

    thanks

×
×
  • 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.