I have generated a JSON file and it is formatted correctly, the same way as I have export it from my MySQL database. Now I need to put it back:-)
I would like to read up on how to do this, so if there is a good link, I welcome it. I have used the phrase
php - script to import JSON file into MySQL database
in Google and others like it, but I having no luck.
Also if I import a file and the record already exists, how do I deal with duplicate issues? Can I make it so that it overwrites automatically?
I am uploading it from an iOS app, so I do not see the php file at work.
You can use json_decode
json_decode($json_from_ios_app, true)
json_decode will return associated array. Based from this structure, construct SQL queries.
Related
I am new to Laravel and on a time crunch for a project. Looking for any help on getting started with this project.
I need to import this table from this url http://ftp.ebi.ac.uk/pub/databases/genenames/hgnc/json/locus_groups/protein-coding_gene.json into laravel. First question is where to put this in the file structure.
Then, I need to create a search that can access just 2 parts of this table (gene symbol and location) and display the results. What are some basic setup tips and/or code to help me access that data from my search bar?
Thank you so much!
I know this code will get me started:
public function index()
$results = file_get_contents("http://ftp.ebi.ac.uk/pub/databases/genenames/hgnc/json/locus_groups/protein-coding_gene.json");
$data = json_decode($results, true);
But not sure where to put that and where to go from there.
First question is where to put this in the file structure.
Up to you, also depends on if you're downloading it on the fly each time your code runs, or if you're uploading it to your Laravel app as a static asset.
If you're downloading on the fly, I'm not sure that you do need to store it on your filesystem really. If it's an asset which you need to manually update, upload it to public/assets or similar.
If you want to use it for searching, you'll probably need to store the file as an asset and load it into your app.
Then, I need to create a search that can access just 2 parts of this table (gene symbol and location) and display the results. What are some basic setup tips and/or code to help me access that data from my search bar?
I'd suggest using a JSON parser like https://jsonformatter.org/json-parser to format the file and explore its structure. Using your IDE's autoformatter is also fine.
$data is just an associative array so you can access its contents as normal. For searching, you can load up your JSON file and loop through the array, check if the current object's name/location matches the search query according to your searching method (equality, startsWith, etc). It looks like your JSON file is pretty large though, and while looping probably won't take too long, loading that all into memory might not be nice. So you probably want to maintain some sort of cache, or parse the datafile into a local database, and write up an SQL query for it.
If you don't want to spin up a database etc, a more lightweight approach might be to process the JSON file once, create a new smaller JSON file which just contains the search criteria and an array index reference to where the object lies in the bigger JSON file.
I have been wondering whether or not it is possible to write VBA to an excel file using php? I ask this question for this reason -> I want to generate an excel report every monday when our users log in. The reports will pull data from our sql db, and write the data to an excel file.
My question is multi-part.
1) Is it possible to write data to an excel file and save that data without the user seeing it?
If I can, then I know how to have it attached using php mailer.
2) If it has to open as a result of using headers, can i write a vba script to minimize or make active workbook close. (I'm highly doubtful as it is because I think excel docs open up as csv when writing sql results to them.
i have been reading around looking for a way to get graphs and so on into my spread sheet i export out of my database and to make it look better for my employee who wants reports of everything in a certain way and so on. I have found PhpExcel.. which seems really good it can do everything i want it to plus more.. but i haven't been able to work out how to use data from my mysql table to fill in the cells since it seems like they all have to be manually set to a value...
i have read over this post How to set cell value Dynamically
i have attempted to use the code provided in this page to try and help me out but i havent been able to get it to work for some reason i am always receiving an error.
i am hoping someone may be able to help me out with trying to get this MySQL table data into a spreadsheet using php excel or even if there are any other links i may have missed i would really appreciate it...
If you install phpmyadmin to connect to your database.
Then you can write SQL queries for whatever reports you need.
And Export (see image 1) the results to CSV or CSV for MS Excel (see image 2).
Export option below the query results
Choose export data format
I have done this using the PEAR classes for PHP. You can read the data from the MYSQL database and then write the script using the PHP and PEAR classes to create an excel file for the same.
Here is link to the SpreadSheet Writer
This tutorial will help you to install PEAR and write the data to the excel sheet.
Hope this will solve the problem you are facing.
I'm doing a lot of work with PHPExcel... I wrote a little class to simplify exporting, you can download it here.
You just need to get your data from database into array, pass it to this class and specify the output format. I hope it helps.
I have a .txt file on our webserver which gets updated and replaced by the client's third party property software.
Does anyone have a script (PHP/MySQL) where I can read this file and import it into a table in my database? Ideally something using codeigniter, but standard PHP will work just fine too.
It is in this format:
"BranchID","PropertyID","PropertyName","Street","DisplayStreet","Postcode","PricePrefix","Price","Bedrooms","Receptions","Bathrooms","ParkingSpaces","Numeric5","Numeric6","Numeric7","Numeric8","Numeric9","AREA","TYPE","FURNISHED","CHILDREN","SMOKING","PETS","GARDEN","DSS","PARKING","cFacility1","cFacility2","cFacility3","cFacility4","cFacility5","cFacility6","cFacility7","cFacility8","cFacility9","cFacility10","Tenure","ShortDescription","MainDescription","AvailabilityCode","AvailabilityDate","FullAddress","PricePrefixPos"
My field names match these headers exactly.
You can use the MYSQL LOAD DATA INFILE directly, see MySQL Reference
This will save come scripting time and will be much faster than importing it via a PHP script.
You may also parse it with PHP.
It looks like a csv.
I would use fgetcsv() to parse the file.
I'm using the National Weather Service's REST web service to return forecast data for locations. I can get the data back and display it onscreen by calling an XSLT, and I can use XSLT to transform the returned XML to a file of SQL insert statements that I can then import manually. However, I'm a bit confused as to how I can do this automatically.
Some clarification: I need to have a cron job run on a scheduled basis to pull in data from the web service. I then need to somehow parse that data out into database records. This all needs to happen automatically, via the single php file that I'm allowed to call in the cron job.
Can anyone give me an idea of how I'd go about this? Do I need to save the XML response to an actual file on my server, and then transform that file into a sql file, and then somehow (again automatically) run an import on the SQL file? Ideally, I wouldn't have to save anything; I'd just be able to do a direct insert via a database connection in my php file. Would that work if I looped through the response XML using a DOM parser rather than an XSLt file?
I'm open to any alternative; I've never done this before, have no idea of how to go about it, and have been unable to find any kind of articles or tutorials about parsing XML data into a database directly.
You need to parse the xml data instead of using xslt to transform it.
You can use xml_parse_into_struct to turn it into a php array and work from it there.
It is probably easier to use SimpleXml and walk the xml tree though.
Considering you already have an xslt transformation, you can also write out the sql to a file, and pipe it to mysql directly.
exec("echo xml_sql.txt| mysql -uusername -ppassword database")
Good Luck!