Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

copying array into db - PHP MYSQL

  • 20-09-2007 01:30PM
    #1
    Registered Users, Registered Users 2 Posts: 648 ✭✭✭


    Hi

    i need to copy the $_POST array into a db so i can use it later

    what im doing not is im assigning $post=print_r($_POST, true) into a db field.

    however the problem is that when i pull the $post out of the db it is only a string now - anyone know how i convert the following string into an array :


    Array ( [option] => com_profile [Itemid] => 3 [task] => searchprofile [choice] => 0 [sport] => 0 [gender] => 0 [country] => AU [city] => [radius] => [username] => [photo] => 0 [body] => 0 [agefrom] => [ageto] => [heightfrom] => 0 [heightto] => 0 [life] => 1 [status] => 0 [children] => 0 )

    ttnx


Comments

  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    $_POST is already an associative array. So all you really need to do is try format the input to the database, so it can be more easily extracted later on.

    The best way to do it would be to have a separate table for the data - three columns; RefID (whatever the related reference in another table is), varname, value.

    Then when you need to get the POST data again, you just call
    SELECT varname, value from postdata where refid = $id
    and pull the data from the result into an array.

    If you want to dump all of the post data into a single varchar column, then perhaps just format it the way you want it.

    Before you input the data, choose a character which separates each key/val pair, say#. Then choose a character which separates the key from the val, say ~.

    Then format a string like that, e.g.

    [php]
    $data_string = "";

    foreach($_POST as $key => $val) {
    if($data_string == "") { //This checks to make sure we don't put a # at the start or end of the string.
    $data_string .= "#"
    }
    $data_string .= "$key~$val";
    }
    [/php]

    The end result is that $data_string will look something like

    #option~com_profile#Itemid~3#task~searchprofile#choice~0#sport~0#gender~0

    This looks odd, but it's a predicatable format taht you can use.

    Then when you extract this string from the database:

    [php]

    $postdata = array();
    $pairlist = split("#", $datastring); //This creates an array of strings, each string being a key/val pair.
    foreach($pairlist as $pair) {
    list($key, $val) = split("~", $pair);
    $postdata[$key] = $val;
    }

    [/php]

    I would still recommend going with my original suggestion though. :) The second option can be messy and troublesome.


  • Registered Users, Registered Users 2 Posts: 3,594 ✭✭✭forbairt


    This sounds like a job for superted ....


    er I mean ... Serialize ...

    Investigate the following ...

    http://www.php.net/manual/en/function.serialize.php

    then .. investigate unserialize :)


Advertisement