The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Picking up product id from a string
Discuss Picking up product id from a string in the PHP Development forum on Dev Shed. Picking up product id from a string PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 5th, 2013, 09:34 AM
|
|
Contributing User
|
|
Join Date: Jan 2010
Posts: 75
Time spent in forums: 15 h 7 m 39 sec
Reputation Power: 4
|
|
|
Picking up product id from a string
I am trying to pick up a product ID from a string.
For some reason, the developer of a plugin decided to store it all as one jumbled text string in the db.
So, for an order I get:
Code:
<metavalue>a:1:{i:0;a:10:{s:2:"id";s:3:"377";s:12:"variation_id";s:0:"";s:4:"name";s:10:"VPN 7 days";s:3:"qty";i:1;s:9:"item_meta";a:0:{}s:13:"line_subtotal";s:4:"1.99";s:17:"line_subtotal_tax";s:1:"0";s:10:"line_total";s:4:"1.99";s:8:"line_tax";s:1:"0";s:9:"tax_class";s:0:"";}}</metavalue>
My current SQL is:
SELECT SUBSTRING( meta.meta_value, LOCATE('\"id\";i:', meta.meta_value )+7, 3)AS prodID
This picks up the start of :"id";s:3:"377"
However, to make things confusing, sometimes s:3 appears after, sometimes not. So, how can I pick out the 377 part of the code. The only consistent thing is that id is enclosed by "" and so is the product ID "377"
How do I pick the 3rd occurrence of "
|

February 5th, 2013, 09:47 AM
|
 |
Contributing User
|
|
Join Date: Dec 2012
Location: Chicago
Posts: 51
Time spent in forums: 17 h 43 m 32 sec
Reputation Power: 1
|
|
|
At first I thought that this could be a JSON string, but if it is then it is not a well formed one. It does look as if it should follow some kind of encoding standard. I suggest that you try to find out how the string is parsed by the plugin instead of trying to reverse engineer it yourself.
Is the plugin open source? If so look at its source code.
Is there documentation of the plugin's internals? If so look for a reference to the encoding standard (if any) that it uses.
|

February 5th, 2013, 09:54 AM
|
|
Contributing User
|
|
Join Date: Jan 2010
Posts: 75
Time spent in forums: 15 h 7 m 39 sec
Reputation Power: 4
|
|
Quote: | Originally Posted by richpri At first I thought that this could be a JSON string, but if it is then it is not a well formed one. It does look as if it should follow some kind of encoding standard. I suggest that you try to find out how the string is parsed by the plugin instead of trying to reverse engineer it yourself.
Is the plugin open source? If so look at its source code.
Is there documentation of the plugin's internals? If so look for a reference to the encoding standard (if any) that it uses. |
Woocommerce, let's just say these plugin developers don't document stuff well 
This is all I have to go on in the data unfortunately.
I know for sure it starts with "id" and then I have to pick up the next bit of string enclosed in "" that is the product ID but it could be 7 chars or 10 or more.
|

February 5th, 2013, 09:55 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
That is a serialized string of an array.
Is there any way you can narrow down the search and check the product ID in PHP? Or perhaps rework the poorly-designed code so that the ID at the very least is stored in a proper column?
|

February 5th, 2013, 10:05 AM
|
|
Contributing User
|
|
Join Date: Jan 2010
Posts: 75
Time spent in forums: 15 h 7 m 39 sec
Reputation Power: 4
|
|
Quote: | Originally Posted by requinix That is a serialized string of an array.
Is there any way you can narrow down the search and check the product ID in PHP? Or perhaps rework the poorly-designed code so that the ID at the very least is stored in a proper column? |
I don;t think I should rework the code as although it's opensource, it's a plugin so any updates and I'd have to redo it.
I could take that string and put it into an array perhaps only taking anything enclosed in ""
Then I would get
"id"
"377"
"variation_id"
""
"name"
"VPN 7 days"
"qty"
"item_meta"
"line_subtotal"
"1.99"
"line_subtotal_tax"
"0"
"line_total"
"1.99"
"line_tax"
"0"
"tax_class"
""
Not sure how to parse the string into an array like that though..unserialize?
Last edited by qwertyjjj : February 5th, 2013 at 10:13 AM.
|

February 5th, 2013, 10:19 AM
|
 |
Sarcky
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
Code:
php > print_r(unserialize('a:1:{i:0;a:10:{s:2:"id";s:3:"377";s:12:"variation_id";s:0:"";s:4:"name";s:10:"VPN 7 days";s:3:"qty";i:1;s:9:"item_meta";a:0:{}s:13:"line_subtotal";s:4:"1.99";s:17:"line_subtotal_tax";s:1:"0";s:10:"line_total";s:4:"1.99";s:8:"line_tax";s:1:"0";s:9:"tax_class";s:0:"";}}'));
Array
(
[0] => Array
(
[id] => 377
[variation_id] =>
[name] => VPN 7 days
[qty] => 1
[item_meta] => Array
(
)
[line_subtotal] => 1.99
[line_subtotal_tax] => 0
[line_total] => 1.99
[line_tax] => 0
[tax_class] =>
)
)
__________________
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|