SELECT for an array field -- Magento - php

One of the fields in my seller db has following data:
{"delivery area":"delivery amount"}
An example of this data in the db is as follows:
{"1":"0","2":"0","3":"0","4":"200","5":"1"}
Now I want to write a query that picks all sellers that deliver to a particular delivery area.
How can I do it using SQL? What do I specify in the WHERE clause?
I am working on Magento so an equivalent in addFieldToFilter will also help.

I just realized that this value is nothing but a json encoded value. All I had to do was to decode it upon reading and extract the values.

Related

Search query containing a variable

HI I am working on a php mysql project and need some help.
On one of my fields I use a check box to enter a value. The possible options are 9001 14001 and 18001.
If I tick 14001 and 18001 the result that gets stored is 14001,18001.
When I set up a search I have had to set up an if equals for each possible combination. ..not too bad in this case as only 7.
Bit what would I use in an sql query if I wanted to say if (field) contains?
The following query would give you all line where field contains 14001.
SELECT * FROM table WHERE field LIKE '%14001%'
But you might want to reconsider the way you store your data to make it faster.
One way to do it is nicely described here:
http://www.phpknowhow.com/mysql/many-to-many-relationships/

how to save json string in a mysql field

I have a table with a text field. I received string as below:
"value": "{\"airwayBills\":[\"84498761\"],\"dockets\":[\"1652395\"],\"error\":false,\"muditaError\":[{\"actionType\":\"docket\",\"docketNo\":\"1652395\",\"errorMessage\":\"No thing error\",\"pieceList\":null,\"quantity\":\"1\"}],\"muditaManifestExtra\":\"Unknown\",\"thingDetails\":[{\"airline\":\"Unknown\",\"bookingDate\":\"19-05-2014 13:03:15+0530\",\"currentPiecesDelivered\":\"0\",\"currentPiecesManifested\":\"1\",\"destination\":\"GOI\",\"flight\":\"Unknown\",\"inScannedPieces\":[],\"locationId\":\"MUDDELHUB1\",\"mode\":\"Air\",\"number\":\"1652395\",\"ordinality\":\"0\",\"origin\":\"DEL\",\"outScannedPieces\":[],\"quantity\":\"1\",\"recipientEmailId\":\"Unknown\",\"serviceProvider\":\"Unknown\",\"shipper\":\"Vector E Commerce Pvt. Ltd.\",\"thingType\":\"docket\",\"totPiecesManifested\":\"1\",\"totalPiecesDelivered\":\"0\",\"weight\":\"11\"}]}",
This value get stored correctly inside a field in the table, but when I try to fetch this value and save it in another field of another table then some of the values like "airwayBills\":[\"84498761\"] are stored as "airwayBills\":["0"], this is causing the loss of data. There is one more strange behaviour that this is happening randomly with only some of the records. Some records are added correctly. For saving into another field I am just enclosing the string in a single quote to save the string as is.
Please help how to save it in a single field in Mysql. Please note that the above values are coming after a GROUP_CONCAT query on the first table.
Thanks in advance
Consider the variable name $json_string as the one that contains your json in the give state (in your post), try stripslashes before trying to insert it into the database.
Here's an extra question, are you building the json array manually using string concatenation or using php array and them json_encode ? (2nd one would make things easier for you, since when you need to add the json obtained after the first mentioned query ran you can just do json_encode and your done)

Dynamically build AJAX request based onChange event in form

So I have several form elements which are currently acting as filters - I plan to use this as a basis for a search feature on my website.
I was thinking about linking a set of on change events to dyanmically build an AJAX query that selects fields from a set of tables but I'm not too sure how I can go about doing it.
On page load I have the following: http://jsfiddle.net/uVhzZ/. The pseudo SQL for this setup would be: SELECT * from ts_rooms WHERE capacity >= 50 (Any means no WHERE condition has been set)
However when the user starts to make changes, I would like this additions to be dynamically added to the SQL query - for instance if Lecture Style is set to Lab and capacity changes to 75 then pseudo SQL would change to SELECT * from ts_rooms WHERE capacity >= 75 AND lectureStyle="Lab";
How do I go about doing this?
First you gotta prepare your AJAX request, it can be done by creating an array with all selects you wanna use (you can look on ther .val() and if it has any value you add this value to that array using the select id as array key) then converting it to JSON.
PHP will receive this JSON string and convert it to array. Then you can look thru this array, and for each element you go building your where statement.
Then it's just a matter of taking query resultset and build another array from it, converting to JSON and responding to JS.
Put all your elements in a form, post it to your php search file. Created a sql query with the relevant constraints and return data in json and display it.

Importing Data into Wordpress-Magic Fields using MySQL queries

PART 01: INTRO
Hey Stackoverflow
Greetings from the Snowy alps of Norway
I have now tried for two full consecutive days to reverse engineer Magic Fields. Why? Because I am a simple man, and want to import data to my wordpress from an exotic database using CSV (and not programming a php routine, which I don't know nor understand).
This has worked for all vanilla Wordpress fields, i.e. post-data, categories and so on. (I used LibreOffice Calc to fine-tune the CSV data, Quest Toad for MySQL import and to create MySQL queries).
My designer has designed this entire thing extensively using the Magic Fields plugin. Therefore I have to deal with Magic Fields for the rest of the data that I need to have imported. It's a movie database, so it's the typical meta-data for movies like "productionyear" "producer" "imdblink" "youtubetrailerlink" and so on.
My question is that I am hoping to get some insights/ideas/support on how to import these data into the magic fields. I have tried and given up the "magic fields importer", it is not documented and does not give any error messages when I try to import various csv formats.
PART 02: Understanding Magic Fields
My question is how can I populate(insert) a magic field with data using a mysql query?
I have tried to understand what it is Magic Fields is doing when I put some data into a magic field and press save, in the admin-edit-post-frontend. It does a lot of things that I can't seem to re-create. For one it is not enough to insert data into the two places where the data is referenced. wp_postmeta and wp_mf_post_meta.
This example post is not sufficient to get the data to "work" i.e. display themselves in wordpress neither in the admin-post-editor nor the front-end for the user:
INSERT INTO `wp_postmeta` (`post_id`,`meta_key`,`meta_value`) VALUES ('474','originaltitle','Die Hard 3');
INSERT INTO wp_mf_post_meta ( meta_id, field_name, field_count, group_count, post_id ) VALUES ( 1100083, 'originaltitle' , 1,1 ,474 );
Also with that meta_id number, Magic Fields creates a number series of 10 for each post, like 7000-7010 and then 8000-8010. I don't understand where it takes those numbers from, and whether they have to be sequential/consecutive. I have found that once entered by the admin-post-editor frontend, I can change the meta-id's in wp_postmeta and wp_mf_post_meta and it still works. But again when I try to create them myself with a SQL query, "it just doesn't work (TM)".
After enabling MySQL querylogging and examining the logfile from the MySQL db engine gives and then trying to insert the queries manually (that I can make out from the logs), I still can't make Wordpress "find" the data and display them properly on the page.
I can modify the data with mysql queries after I have populated them using the frontend with no problems.
Also using the query logs, I found that after I push "SAVE" in the Wordpress frontend-post-editor it does no less than ~780 mysql query lines (regardless if I populate 1 or 10 of my magic fields)!
The other thing I have tried is to take a snapshot of the database before and after I have done the post of the magic fields, and then used a diff-tool (various, WinMerge and Notepad++ mainly). I can't understand what it's doing but I THINK it is doing some hidden trickery in wp_term_taxonomy. This is just speculation.
Part 03: Conclusion I don't know programming so any practical solution, would be deeply apprechiated.
the full query I want to do per post (I have about 800 of these) is here: >http://pastebin.com/5cZT3AjA
The FULL list of queries that Magic Fields is doing after I push save in the admin-front-end is listed here (which I call the 'robot-at-work'). http://pastebin.com/c2c6qUQt To be sure I have checked three times that it wasn't something extraordinary that it is doing ~780 lines after I push save.
I guess if I could find a way to bulk-edit all of my posts, and then have the "robot" i.e. admin-post-edit frontend do the work to create all these lines in the database. I could then change the fields afterwards... But the bulk-editor doesn't show the magic-field editor either.
I have twisted my head around this problem 10 times now, and can't twist anymore!
SOLVED!!!!
Meta_id 's must be CHRONOLOGICAL and CANNOT be arbitrary (i.e. random).
data must be inserted into wp_mf_postmeta AND wp_postmeta
these records relate to post_id, meta_id and the key-value stores of
the data itself such where key would be "movietitle" and value would be "Die Hard".
I have uploaded the csv I created to do this and create the mysql queries.
I used LibreOffice Calc to finetune the data and Toad for MySQL (by Quest, free)
to create the MySQL queries...
These two records need to be modified for the data to be properly inserted:
NOTE THAT the META_ID MUST be chronological i.e. 11001, 11002 11003 per field per post
so if meta_key is Movietitle is first then that has meta_id of 11001, if productionyear is second then that is 11002 and if country is third then that is 11003
Also note that for the wp_mf_postmeta the fieldname is the SAME VALUE as meta_key
from wp_postmeta , I.e. the meta key.
INSERT INTO wp_postmeta (post_id,meta_key,meta_value) VALUES (346,'produksjonsar','18001');
INSERT INTO wp_mf_post_meta ( meta_id, field_name, field_count, group_count, post_id ) VALUES ( 18001, 'produksjonsar' , 1,1 ,346 );
The file I have used for this import is available here, if anyone
needs to see an example of how to do this import... Again I used
Toad for MySQL to create the SQL Queries by using the IMPORT function.
http://ge.tt/9Q5h4zC
This also solves this question which I posted on the Wordpress Forums, Stackoverflow and Stackexchange.
http://wordpress.org/support/topic/importing-data-to-custom-fields?replies=2#post-2592976
https://wordpress.stackexchange.com/questions/37691/inserting-data-into-magicfields-using-mysql-queries
Importing Data into Wordpress-Magic Fields using MySQL queries
Massive props to Hameedullah Khan Stackexchange, profile here https://wordpress.stackexchange.com/users/5337/hameedullah-khan , for basically providing the clue needed to find the solution.

problems with finalizing the sales invoice form

i know this would be probably too much to ask for . i am sorry. and i would really appreciate if someone can guide me in the right direction.
i have made the sales invoice posting form.
the fields are:-
customer id, segment id, date of invoice, invoice no(streamed from mysql max value), items,uom's prices,quantities.
since the no of items is variable. as in the no of rows changes so i have made it dynamic as per user's requirement.
i am able to display this data on the next page.
now when i have say 5 itemcodes, their prices and quantities, i want to compare all of these with their corresponding mysql values. for ex. for quantities, i would like to compare the quantities avl in mysql and only then proceed with posting, same is the case with prices. i want to compare the prices with the selling prices listed for those particular items.
i also need help with how to post these arrays to the mysql and then check what all has been posted correctly.
i would greatly appreciate any information,ideas,links, etc. where i can learn or may be if someone can post up a sample code or something.
thanks again.keep smiling :-)
From what I understand, you are trying to INSERT invoice data after validating from MySQL. A generic answer would:
Receive the data at the server.
Do the logical validation.
Use SELECT to get respective values from the database
Compare those values with the received one
Execute an INSERT query if successful.
There are many ways to solve your problem of posting arrays in MySQL, in certain cases, I send client side arrays as CSV strings and use something like PHP-explode("," $str) and then insert individual values, however it will differ based on your data-formatting and table design.

Categories