Have an old project that someone did in PHP and am attempting to convert it over to Rails. Am encountering an issue when trying to iterate through a multiple array in rails.
The array as stored in the DB looks like this:
a:5:{i:0;s:8:"Director";i:1;s:11:"Shareholder";i:2;s:14:"Vice President";i:3;s:9:"Secretary";i:4;s:9:"Treasurer";}
Am trying to display the String values such as "Director" and "Shareholder".
Does the DB field need to be changed to a different format to work?
How would this be done in rails?
Thank you in advance
That looks like the output of PHP's serialize function to me.
If you have PHP around or don't mind installing it to help with the data migration, then I'd write a short PHP script to convert those columns to JSON using PHP's unserialize and json_encode functions: read a value, unserialize it, json_encode the result, write it back into the database (all in PHP).
After that, you should be able to use ActiveRecord's serialize with JSON as the storage format:
class Model < ActiveRecord::Base
serialize :whatever_it_is_called, JSON
end
You could also use YAML (the default for ActiveRecord's serialize) if you had YAML support in PHP-land.
If you don't want to touch PHP at all, then you'll have to write Ruby parser for PHP's serialize format (which seems to be well documented in the PHP docs) and hook that up to ActiveRecord's serialize.
You will of course be working with a copy of the real database for all this work.
Related
I'm interested in scrapping data from a website, and pass the data for use it in PHP. I have had a look around, and the best suggestion I have been able to find is to first serialise the Python data and then pass it along.
The issue I have is that I'm unsure how to serialise the Python data.
I'm using Selenium, and I have the following code.
test = browser.find_elements_by_css_selector("table#resultstable td")
I can see the data I want to use by running the variable through a loop and printing it.
for val in test:
print(val.text)
However when I try to serialise the object, I receive the following error:
json.dumps(test)
....
TypeError: Object of type 'WebElement' is not JSON serializable!
I Hope somebody can point me in the right direction, I'm happy in PHP but I have only begun to look Python recently.
JSON the data format is limited in what it can store (numbers, strings, booleans, and then arrays or maps (what python calls dictionaries) of these elements (or other arrays or maps which at some point end in one of those formats). json the python library for manipulating json data can therefore only dump to a json string something that fits those rules. In your case, I'd suggest as a simple starting point:
columns = [val.text for val in test] # convert to a list of strings, where each string is the td.text
json.dumps(columns) should then give you something useful
The goal is to unserialize a PHP serialized string and get sutable object in C#
Is there any way to make this possible in C#(.Net)?
To be more specific:
We need to make an application which comunicates (Via HTTP) to specific website which returns the needed information. Fortunately/unfortunately we dont have permission to website so the data (array mostly) that is returned from website is PHP serialized.
I suppose using JSON as an intermediary step could be useful.
You should probably write it to XML or JSON. You can construct your C# object back from the XML
Edit: Looks like there is already a XML serializer for PHP
i'm manually reading out database entries from a plugin (eventscalendar wordpress). there are user defined fields / custom fields stored in the database. excuse my ignorance - but i'm having trouble associating the format the data is stored in:
a:1:{s:3:"key";s:6:"extern";}
so in the end all i would need is the value of s:6:"" in a simple variable.
lets say $key = "extern"
any quick solutions?
thanks,
mark
Take a look at unserialize to get it back into an array: http://php.net/manual/en/function.unserialize.php
That is a serialized array-- note the a at the beginning. It also works on Objects.
It is created with PHP's serialize and converted back to proper array/object with unserialize.
It serves a similar purpose to the JSON format.
using php, if possible.
What does it mean? I was reading one of my old questions ( How do you pass values between PHP pages for MVC?)
and in one of the answers it says:
The part responsible for transferring the data between the controller and the view is the View engine (or class) internal to CodeIgniter. It takes that array from the controller and deserializes it for the view.
I don't know what that means (I read the comments). I put CodeIgniter as the example and tag, but I guess it could be a general question.
Thanks.
To serialize data is to generate a storable representation of a value as a string, for example:
json_encode is a type of serialization but PHP has native support also of serialize that can serialize almost any type of data except resources types, you can find a small guide to serialization here:
http://www.devshed.com/c/a/PHP/The-Basics-of-Serializing-Objects-in-PHP/
For the full manual that corresponds to PHP you can find it in the link that Cédric Belin posted in the post below :D
Serializing usually means converting object (or complex object structure) into text/binary form, suitable for storing or transmitting over network.
Deserialization is a reverse process.
See this link
Serialization is the process of converting an object or an object graph in linear sequence of bytes for storage or transmission to another location.
If you want to store your data in memory or transmit over computer network and reconstruct later in different environment, first you need to convert this data into understandable/usable format by computers which is binary format (00011010101011...).
SERIALization is a convertion of data structure into SERIES of bits.
I want to convert an Object into a String in PHP. Specifically, I'm trying to take a mysql query response, and I'm trying to convert it into something I can write to a file and use later.
Of course, when you try to write an Object to a file, PHP appropriately yells: Catchable fatal error: Object of class DB_result could not be converted to string in .....
Alternatively, if there is some other way of writing the result of a mysql query to a file, that works too. I'm playing around with a home-brewed caching project :)
Maybe serializing? It will take an object/array and convert it to a string (which can then be un-serialized back later)
json_encode and json_decode will also accomplish many of the properties you are looking for via serialize. The advantage is that you can send JSON-encoded data to a web browser and JavaScript can view and modify properties like a native JavaScript object. In addition, JSON is lighter weight than serialized data because its syntax is a lot more compact.