Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

copying array into db - PHP MYSQL

  • 20-09-2007 12: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,317 ✭✭✭✭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