Intelligent search?
-
Hi folks,
I’m working on a proof of concept for a client and one of the functions I need is a search box for customers to discover if deliveries are made to certain postcodes. The search just needs to return a certain page (or post) for yes or a different page for no. I have a csv of all the postcodes (there are about 14,000) but I’m really struggling to get my head around how to do this – ideally with a plugin but I’m not adverse to a bit of coding.
Any ideas gratefully received.
-
Hi Grassmedia,
Could you please post a few first lines of your csv files? I’ll probably help you but I need to know the csv structure.
A.
Hi Adam,
The format of the .csv is basically
Postcode District Delivery
CT1 1AA Canterbury Monday morning
CT1 1AD Canterbury Monday morning
CT1 1AF Canterbury Monday morning
CT1 1AG Canterbury Monday morning
CT1 1AH Canterbury Monday morning
CT1 1AJ Canterbury Monday afternoon
CT1 1AL Canterbury Monday afternoon
CT1 1AN Canterbury Monday afternoon
CT1 1AP Canterbury Monday afternoonSo if a user enters a postcode of CT1 1AA (with or without a space) something appears which says “Yes we deliver to your area on a Monday morning between 10.00 and 13.00 kind of thing. Does that make sense?
Thanks.
Sure it does make sense, however it would be better if CSV was separated with some other character than space. I mean if it was for example
CT1 1AA, Canterbury, Monday, morning
Is it possible?
Oh sorry, I misunderstood, it can be any separator I like. Typically a comma.
All right, so let’s assume that your CSV file is separated by commas like that
ZIP, City, Day, time of day
eg:
CT1 1AA, Canterbury, Monday, morning
I’ll provide you with a simple function to put in functions.php or even a simple plugin today. It’s 1:40pm now for me so take a look here sometime around 10-11pm (that’ll be in about 8-9 hours from now).
Cheers
A.Hey Adam, if you could do that, you’d be a hero!
For a quick and simple “proof of concept” please check out this (it’s a temporary “draft”):
https://spythemes.com/grass/s.php
For now it’s just a php script without any WordPress integration. If this is more or less what you’re looking for (in terms of features) please let me know and I’ll rewrite it and post it here as a code for you so you may use it in your WordPress .
For testing purposes you have following database (CSV) available at the moment:
CT1 1AA, Canterbury, Monday, morning
CT1 1AD, Canterbury, Monday, morning
CT1 1AF, Canterbury, Wednesday, morning
CT1 1AG, Canterbury, Monday, morning
CT1 1AH, Canterbury, Monday, morning
CT1 1AJ, Canterbury, Sunday, afternoon
CT1 1AL, Canterbury, Friday, afternoon
CT1 1AN, Canterbury, Monday, afternoon
CT1 1AP, Canterbury, Monday, afternoonSearch is done only by zip code, however the code might be entered in search form as lower or uppercase, spaces don’t matter and even parts of the code may be separated (in search form, not in CSV file) by ‘-‘ or ‘_’.
Anyway, check it out please and let me know. If it’s ok I’ll make WP version and post it here for you.
a.
Hi Adam,
Brilliant, yes that works exactly as required!
Thank you so much for this.
All right, so I’ll make a WordPress version for you, however I’m not sure if I’ll be able to do it for today – since my first posts some things came-up related to my daily job. If you’re all right with it I’ll make it for tomorrow afternoon.
That’s cool Adam, it’s not screaming urgent.
Ok ??
Hello again Grassmedia!
I’m sorry that I wasn’t able to get back to you sooner but due to some personal stuff I had to take care of other things. Anyway as I promised I’ve got you a little plugin for your proof-of-concept. It works quite well within my WP 4.0.1 testing site and with small amount of data.
I should warn you though, that I’m not quite sure how will it behave with thousands of lines of data inside csv file. I guess it should be ok but don’t be suprised if the search will be a little slower (although it shouldn’t).
Since the code itself is bigger than a few lines I’ve put it here for you (and anyone else who’d like to take a look):
A small note: this plugin is indeed a kind of, as you say, “proof of concept” with quite quick and dirty code. I wouldn’t use it on real (commercial) site as there’s no input data sanitization nor decent code optimization. In a word: it’s not a good coding. If you’d like to use it “for real” I think the code shoud be rewritten to match coding, security and efficiency standards. Also, the database from csv file should be imported into the WP mySQL database instead of simple file upload.
Despite that, the plugin does what you were expecting, so feel free to do with it whatever you want. There’s not much explanation inside the code but you should get the idea ??
Usage instruction is built-in and you’ll find it after installing plugin in “Settings” -> “ZIP Search” in your WP Dashboard.
Here’s another link for you, where you can download complete plugin, working and tested. Install it just like any other WP Plugin.
https://www.dropbox.com/s/yu8u9nxojnm0xel/spy-zip-search.zip?dl=0
Good luck!
A.Hi Adam, wow, thank you do much for your efforts. I’d like to say I made it work but so far, I’ve failed ??
Using the plugin from Dropbox.
My csv is simplified with no header row and just 4 rows of data (just in case) like so …
1234567,canterbury,Friday,PM
ct11aa,Canterbury,Monday,14:00 to 16:00
ct11ad,Canterbury,Tuesday,14:00 to 16:01
CT1 1AF,Canterbury,Wednesday,14:00 to 16:02And all looks good in the set-up. I added some simple debugging to your code and I can see that on the input side the file name is being picked-up ok and the zip code parsed as expected – reduced to lower case, with spaces stripped.
Where it seems to fail is that it never enters the foreach loop
‘
echo “Lets go in to the foreach loop now…”; //This echoes
foreach ($csv as $csvline) {
echo “in the loop”; //this never does
$line = explode(“,”,$csvline);
$zipcheck = trim(strtolower(str_replace(‘ ‘,”,$line[0]))); …etc.
‘
So it’s never getting to the comparators. I figured then that perhaps the $csv array was empty so tried using $csv = fgetcsv($zipfile) to fill the array but, alas, no go. Sorry, I know just enough PHP to be really, really dangerous!You can view/use my dev site at https://skunkworx.agency/sample-page/
and the csv is at https://skunkworx.agency/wp-content/uploads/2015/01/postcode-test.csvAny help greatly appreciated!
Hi Grassmonkey!
IT’s a little strange, see it here: https://spythemes.com/blog/delivery – it’s the same plugin from dropbox and your file (this time run in a real environment on WP. 4.1). There it clearly works.
It has to work, but as I said earlier it’s rather quick and dirty code meant to be a “skeleton” rather than the final product. Anyway I’ll go through the code once again and look for any glitches that may prevent it from working.
And could you perhaps let me look at the code modified by you?
A.
“My” code is now same as your original, except for an echo or two. I’m wondering if its a hosting thing? I know some hosts have an issue with things like str_getcsv …
If you wouldn’t mind dropping me an email to [moderated] I’ll give you admin access and, if you tell me your ip address, I’ll give you FTP access too. Sorry to be a pain!
- The topic ‘Intelligent search?’ is closed to new replies.