hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Programming Tutorials : [PHP] Storing arrays in a database or file.
Reply

Programming Tutorials How-Tos related to programming, databases, and the like.
Forum Jump

[PHP] Storing arrays in a database or file.

Reply Post New Thread In Programming Tutorials Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 09-06-2005, 01:56 AM
hofan41 hofan41 is offline
Junior Guru Wannabe
 
Join Date: Jun 2002
Posts: 35

[PHP] Storing arrays in a database or file.


I barely see anybody use this technique anymore, so maybe it's completely outdated, but I still find it to be fairly useful.

Code:
$array; 
// the array you wish to store

$save_string = implode("`|`", $array); 
// saves the array as a string where each element is separated by `|`. 
//This can be changed to anything else.
use that $save_string to store into a file or database.

to get the array back:

Code:
$save_string; 
// the string you read from either the file or database.

$array = explode("`|`", $save_string); 
// splits $save_string into array elements by removing `|`.
Hope you found this useful. Please note that in this example if the original array contained a string with "`|`" in it, it would not work properly, so you need to escape the characters you use if there is the possibility of that happening.


Last edited by hofan41; 09-06-2005 at 02:00 AM.
Reply With Quote


Sponsored Links
  #2  
Old 09-06-2005, 02:57 PM
opera.mp3 opera.mp3 is offline
Junior Guru Wannabe
 
Join Date: Jul 2005
Posts: 56

Reply With Quote
  #3  
Old 09-26-2005, 01:36 PM
LimpBagel LimpBagel is offline
Junior Guru Wannabe
 
Join Date: Sep 2005
Location: Glendale, AZ
Posts: 84
I came onto my current job a few months ago and everything is exploded/imploded so I probably won't rewrite all of it but this serialize function looks very tempting for any future projects.

Reply With Quote
Sponsored Links
  #4  
Old 12-26-2005, 10:27 PM
JustinH JustinH is offline
Web Hosting Master
 
Join Date: Nov 2000
Posts: 3,042
Nobody uses it because it's less than great programming. Fields in a database should only have a single value. If you want to store an array, store it in multiple fields.

Reply With Quote
  #5  
Old 12-26-2005, 10:56 PM
hofan41 hofan41 is offline
Junior Guru Wannabe
 
Join Date: Jun 2002
Posts: 35
Quote:
Originally Posted by JustinH
Nobody uses it because it's less than great programming. Fields in a database should only have a single value. If you want to store an array, store it in multiple fields.
im not sure how exactly you plan on storing an array in multiple fields if the length of the array is uncertain or the array contents are of the same type of data, say a shopping cart's items, or maybe a friends list filled with ID values.

Reply With Quote
  #6  
Old 12-27-2005, 11:33 AM
Korvan Korvan is offline
Web Hosting Guru
 
Join Date: Nov 2005
Posts: 268
thats very easy to do hofan. The way justin would set it up would be

Code:
CREATE TABLE cart (
cart_id INT UNSIGNED NOT NULL AUTO_INCREMENT,


PRIMARY KEY (cart_id)
);
and for your dynamic data, where you could have an unlimited amount of fields you would put it in another table like this one
Code:
CREATE TABLE cart_data (
cart_data_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
cart_id INT UNSIGNED,
cart_data_value varchar(100),
PRIMARY KEY(cart_data_id)
);
so as your array gets bigger you can keep adding more and more values into the cart_data table, when you want to get all the rows of the array back from the database you would query with a left join statement like this

Code:
SELECT * FROM cart LEFT JOIN cart_data USING(cart_id) WHERE cart_id = ?
Then you can loop through and restore your array.

The first table would be for static data or data related to the current user, while the second table is only there to fulfill the unknown number of values in your array. If all you are storing is an array, then you can forgo the first table and just use the second one.

Reply With Quote
  #7  
Old 12-27-2005, 04:25 PM
hofan41 hofan41 is offline
Junior Guru Wannabe
 
Join Date: Jun 2002
Posts: 35
oh, I see. I misread justin's post and took fields to be actual database fields instead of completely new entries in a database. thanks

Reply With Quote
  #8  
Old 12-27-2005, 09:20 PM
JustinH JustinH is offline
Web Hosting Master
 
Join Date: Nov 2000
Posts: 3,042
Quote:
Originally Posted by hofan41
oh, I see. I misread justin's post and took fields to be actual database fields instead of completely new entries in a database. thanks
Korvan was far more clear than me .

Reply With Quote
  #9  
Old 02-06-2006, 11:57 PM
trail_blazer trail_blazer is offline
New Member
 
Join Date: Feb 2006
Posts: 0
can anyone show me the INSERT statement ?
because i'm not sure how to relate cart id to cart_data_id to identify the array belong to same user

Reply With Quote
  #10  
Old 02-07-2006, 05:07 PM
Korvan Korvan is offline
Web Hosting Guru
 
Join Date: Nov 2005
Posts: 268
no you just compare cart_id, cart_data_id is really moot unless you want to delete a specific cart_data_id field

The first table should contain the information you need to determine who owns the cart, so in your script you should have a variable with the current users cart_id.

So when you insert data you will use "INSERT INTO cart_data (cart_id, cart_data_value) VALUES ({cartid}, '{value}');"

Reply With Quote
  #11  
Old 02-07-2006, 11:04 PM
trail_blazer trail_blazer is offline
New Member
 
Join Date: Feb 2006
Posts: 0
thanx for reply

Reply With Quote
  #12  
Old 03-19-2006, 02:36 PM
Jamez226 Jamez226 is offline
Newbie
 
Join Date: Nov 2005
Posts: 10
The whole point in databases is that you do not use arrays. You would create a new table with the array info in.

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Samsung Ventures Backs Cloudant Database as a Service Web Hosting News 2013-02-08 14:10:26
File Sharing Service RapidShare Changes Traffic Model to Deter Piracy Web Hosting News 2012-11-09 12:16:35
Cloud Host Savvis Extends Cloud-Based Database Platform to European Customers Web Hosting News 2012-10-12 11:06:37
Web Host webHOSTING.net Improves Data Backup Using Nimble Storage Array Web Hosting News 2012-03-28 13:14:22
Jelastic Adds Apache CloudDB to Java PaaS Cloud Platform Web Hosting News 2011-12-22 19:47:44


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?