Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Location
    Tokyo, Japan
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts

    <textarea> csv to DB

    Hey Folks,

    I have a fairly large table generated with JavaScript...users can enter/delete/change data at thier descretion. Once the user has completed any modifications I would like to give them the ability to save to a DB.
    So far I have Included a "Export to CSV" button that converts all the table data into CSV format and sends the output to a <textarea>.

    What I would like to do next is take the CSV data inside the <textarea> and send it to my DB...
    (i.e. after the data is converted to CSV the user then has the option of clicking the "Save to DB" button)

    I'm not sure where to start in terms of getting things into thier respective cols...

    Anyone have a suggestion on how to accomplish this?


    Thanks,

    Yakisoba

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,628
    Thanks
    0
    Thanked 648 Times in 638 Posts
    They need to submit the form with the CSV data in the text area to a server side script and that script then does the database update. This can be done either by reloading a whole new page or via Ajax.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Location
    Tokyo, Japan
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the quick response felgall.

    They need to submit the form with the CSV data in the text area to a server side script and that script then does the database update. This can be done either by reloading a whole new page or via Ajax.
    This much is already understood...(Sorry, I should have been clearer in my initial post).

    Ideally I would like the "save" feature to function asynchronously...as does the rest of my page...

    The idea I am working with now involves this order of operation:
    1. Convert/display CSV data into <textarea> (this is complete)
    2. Submit via <form> CSV data contained in <textarea> to .php script (no problem)
    3. .php script to split the CSV data into an array (this is somewhat working, but I can't get it to play nice with step 4)
    4. .php script to build a mySQL INSERT query
    5. INSERT values in TABLE (this should not be a problem, must complete the above steps first though)
    6. Refresh browser page to reflect changes (this is complete. if I manually enter data into my DB the changes are reflected when the page loads)

    As you can see steps 3 and 4 are my biggest challenges at the moment.

    Any suggestions?

    Thanks,

    Yakisoba

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,628
    Thanks
    0
    Thanked 648 Times in 638 Posts
    Well to insert them into the database you need a comma separated list of values in the same order as the fields you are loading into so a single database call something like this should handle 3 and 4.

    INSERT INTO tablename ($field1, $field2, $field3) VALUES ($csvlist);
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Location
    Tokyo, Japan
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well to insert them into the database you need a comma separated list of values in the same order as the fields you are loading
    Got that...

    something like this should handle 3 and 4.

    INSERT INTO tablename ($field1, $field2, $field3) VALUES ($csvlist);
    I think the above is more what I was thinking for number 5 on my list...

    from my last post
    1. Convert/display CSV data into <textarea> (this is complete)
    so I have a <textarea> that contains CSV output (actually SCSV ";" output, but at the moment that doesn't matter)

    example data displayed in the <textarea name=csv> would look like this:
    ;1;2;3;4;5
    ;1;2;3;4;5
    ;1;2;3;4;5
    ;1;2;3;4;5

    each col is seperated by ";" and each row is seperated by "\r\n"....

    so $_POST["csv"] =;1;2;3;4;5 ;1;2;3;4;5 ;1;2;3;4;5 ;1;2;3;4;5

    now I would like to take that and split them into thier respective rows/cols and put them into an array...is this possible?

    Could I first split the data one row at a time, then place that row in an array, then insert the data into my db?


    This is what I've been trying with no luck: (not the prettiest so go easy;)
    PHP Code:
    <?php

    include 'db_connect_item.php';

    function 
    data_split($buff)
    {
        
    $length strlen($buff);
        
    $i 0;
        while (
    $length >= 1)
        {
            
    $pos2 strpos($buff';');
            if (!
    $pos2)
            {
                
    $fields[$i] = $buff;
                
    $length = - 1;
            }
            else
            {
                
    $fields[$i] = substr($buff0$pos2);
                
    $buff substr($buff, ($pos2 1));
                
    $length strlen($buff);
            }
            
    $i++;
        }
        return 
    $fields;
    }


        if (
    $_POST["csv"] == null)
        {
            echo 
    "Export to CSV first.";
        }
        else
        {
        
            
    $csv $_POST["csv"];
            
    $pos1 0;
            
    $pos2 1;
            while (
    $pos2)
            {
                
    $pos2 strpos($csv"\r\n");
                
    $buffer substr($csv$pos1$pos2);
                
    $csv substr($csv, ($pos2 1));
                if (!
    $buff$buff $csv;
                
    $fields data_split($buff);
                
    $name $fields[4];
                
    $sql "insert into item (player)
                    values('$name')"
    ;
                
    $result mysql_query($sql) or die ("CRASH!!!!!".mysql_error());
            }
            echo 
    "<p>Data Stored! <br>";
        }

    ?>
    With the above code i was trying to INSERT $name into col player...the player name would be in the 4th position in the array...

    So as it stands now ... I can send the data of $csv into all of that but, not getting anything useful in return...currently all my field values are null...

    Any suggestions are appreciated.

    Thanks

    Yakisoba

  • #6
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    take a look at fgetcsv and explode for getting what you want.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •