1

Hi I have an array of Services ordered by user like below $_POST values printed

Array
(
[compaignID] => 4
[totalChecked] => 3
[hear_about_us] => Google
[videolink] => 
[LinkedIn] => Array
    (
        [0] => 1
        [1] => 1
        [2] => http://developer.comoj.com
    )

[Facebook] => Array
    (
        [0] => 
        [1] => 
    )

[Twitter] => Array
    (
        [0] => 
        [1] => 
    )

[YouTube] => Array
    (
        [0] => 2
        [1] => 4
        [2] => http://developer.comoj.com
    )

[Coupon_Area] => Array
    (
        [0] => 
        [1] => 
    )

[Website] => Array
    (
        [0] => 
        [1] => 
    )

[Google_Map] => Array
    (
        [0] => 
        [1] => 
    )

[Email] => Array
    (
        [0] => 3
        [1] => 8
        [2] => http://developer.comoj.com
    )

[Share_To_Social_Media] => Array
    (
        [0] => 
        [1] => 
    )

[btnSubmit] => Submit
)

I'm confused how to store these. I made these arrays in php form to make their groups like below

for Orders Input field

<input name="<?php echo $service; ?>[]" type="text" id="order<?php echo $service; ?>" size="4">

for Services Check boxes to check which services to take

<input name="<?php echo $service; ?>[]" type="checkbox" id="show<?php echo $service; ?>" value="<?php echo $serviceID; ?>" onclick="countChecked();">

And for Services WebURL the input field below like

<input name="<?php echo $service; ?>[]" type="text" id="<?php echo $service; ?>" size="40">

I'm confused how to get only arrays which have values and store only these services their order number like for showing ascending or descending and their webrurls against each checked checkbox.

I'm confused how to loop through and store in mysql.

Please help me.

Many Thanks

Jaguar
  • 403
  • 2
  • 10
  • 22
  • It depends entirely on the structure of your tables. Post some schemas and maybe we can help you. – Marc B Aug 09 '11 at 17:03
  • http://php.net/serialize – Buddy Aug 09 '11 at 17:03
  • My Table is like below SQL `CREATE TABLE `tbl_page_links` ( `pageLinkID` bigint(20) NOT NULL AUTO_INCREMENT, `compaignID` bigint(20) NOT NULL, `serviceID` tinyint(4) NOT NULL, `webaddress` text NOT NULL, `sorder` tinyint(4) NOT NULL DEFAULT '0', `isActive` tinyint(4) NOT NULL DEFAULT '0', PRIMARY KEY (`pageLinkID`) ) ENGINE=MyISAM AUTO_INCREMENT=19 DEFAULT CHARSET=latin1` – Jaguar Aug 09 '11 at 17:06

4 Answers4

10

You can take help of json_encode() and json_decode() functions of PHP solve these things.

To store and array into mysql, you should use json_encode($yourArray); and you should store the returned string into mysql.

Similarly for retrieving, you should use json_decode($yourMySqlStoredString) and this will return the array back to you, which you can use for your further manupulations!

json_encode php function

json_decode php function

linuxeasy
  • 6,269
  • 7
  • 33
  • 40
  • +1 for awesome creative solution. Would have given you another 4 if I could. Well done. – Madara's Ghost Aug 09 '11 at 17:09
  • 4
    -1 Terrible idea. Instead of shoving serialized data into a RDBMS you should adjust your schema to properly fit the data. – NullUserException Aug 09 '11 at 17:14
  • What if I try the PHP Serialize() & Unserialize(); Which one is better and easy to handle. also Fast enough. – Jaguar Aug 09 '11 at 17:14
  • @NullUserException but if you could read the question, probably that's not what user wants :) – linuxeasy Aug 09 '11 at 17:15
  • I'll adjust it to store json or Serialize() & Unserialize() data. – Jaguar Aug 09 '11 at 17:16
  • @junjua serialize would be faster, but `json_encode` and `json_decode` could be used for several purposes, even the data could be used for different systems or communication. so its tradeoffs, performance or easy maintenance! – linuxeasy Aug 09 '11 at 17:17
  • @NullUserException as you can see junjua concludes storing the data into json or serialize formats only, so thats what he indended by the question!, anyway thanks for your negative mark, though it didn't make any sense! – linuxeasy Aug 09 '11 at 17:18
  • 1
    I've read the question. It sounds like the OP wants to have multiple one-to-many relationships; which shouldn't be handled by using serialization (eg: using json or php's built-in `serialize()`) – NullUserException Aug 09 '11 at 17:18
  • you could refer [this](http://stackoverflow.com/questions/1306740/json-vs-serialized-array-in-database) for your json vs serialize stuff :) – linuxeasy Aug 09 '11 at 17:20
  • 1
    And regardless of whether the OP feel that it "will work," we should advise him or her against bad coding practices (eg: inserting serialized data into RDBMS) – NullUserException Aug 09 '11 at 17:21
  • more than a practice, it's a matter of requirement :), for now, if I am not concerned with the relationship but just storing snapshots, why should I choose unwanted complexities? is over-engineering stuffs really necessary? anyways, the discussions will have no end, so you are the winner! – linuxeasy Aug 09 '11 at 17:24
6

I believe the use of serialize() and unserialize() functions is better than using json_encode() and json_decode().

The reason is simple: Serialize encodes multidimensional (string)indexed array into string and unserialize is able to return the same array (in original array format) unlike the combination of json_encode and json_decode function which returns object with array properties.

Well and that is simply not very "programmer friendly".

Original array:

Array(
[prvni] => Array
    (
        [druhy] => Array
            (
                [0] => a
                [1] => b
                [2] => c
                [3] => d
                [4] => e
                [5] => f
            )
    )

[prvnidruhy] => Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
        [3] => 4
        [4] => 5
    )
)

json_encode:

{"prvni":{"druhy":["a","b","c","d","e","f"]},"prvnidruhy":[1,2,3,4,5]}

json_decode:

object(stdClass)[1]
 public 'prvni' => 
  object(stdClass)[2]
   public 'druhy' => 
    array (size=6)
      0 => string 'a' (length=1)
      1 => string 'b' (length=1)
      2 => string 'c' (length=1)
      3 => string 'd' (length=1)
      4 => string 'e' (length=1)
      5 => string 'f' (length=1)
   public 'prvnidruhy' => 
    array (size=5)
      0 => int 1
      1 => int 2
      2 => int 3
      3 => int 4
      4 => int 5

serialize:

a:2:{s:5:"prvni";a:1:{s:5:"druhy";a:6:{i:0;s:1:"a";i:1;s:1:"b";i:2;s:1:"c";i:3;s:1:"d";i:4;s:1:"e";i:5;s:1:"f";}}s:10:"prvnidruhy";a:5:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;i:4;i:5;}}

unserialize:

array (size=2)
 'prvni' => 
    array (size=1)
 'druhy' => 
    array (size=6)
      0 => string 'a' (length=1)
      1 => string 'b' (length=1)
      2 => string 'c' (length=1)
      3 => string 'd' (length=1)
      4 => string 'e' (length=1)
      5 => string 'f' (length=1)
 'prvnidruhy' => 
    array (size=5)
      0 => int 1
      1 => int 2
      2 => int 3
      3 => int 4
      4 => int 5
Dejv
  • 944
  • 2
  • 14
  • 31
1

But you could use json_decode($data, true) obtaining a simple associative array. From the documentation:

When TRUE, returned objects will be converted into associative arrays.

vaultah
  • 44,105
  • 12
  • 114
  • 143
decadenza
  • 2,380
  • 3
  • 18
  • 31
0

This answer is based on your last comment. You need to loop through the array and insert each value. Tentative...

foreach ($array AS $v) {
$sql = "INSERT INTO `tbl_page_link` (serviceID, sorder, webaddress) VALUES ($v[0], $v[1], '$v[2]')";
mysql_query($sql);
}
Jason Fuller
  • 137
  • 1
  • 10