Storing page data in php - php

I'm trying to figure out the best way to store webpage data for a website build in PHP so that I can access it for components like navigation and breadcrumbs.
At the moment I've got an array in my config file which has each page's name, path and parent data, but I realise this is almost certainly wrong.
How should this data be optimally stored? I thought about a MySQL table but it seems like overkill for a small site.

Related

Load JSON file from URL into laravel, then create a search that can access that data

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.

Have a PHP script detect if a new file has been uploaded and log information about that file to MySql?

I am doing something rather complex here (for me at least), and I'm not sure where to look in terms of resources. I don't need anyone to write this for me, maybe just a link where I can find a tutorial?
I have a folder on my website that holds all the articles on that site. I also have a MySql database that holds a table of contents for the articles. Right now I have to manually edit the MySql database every time I upload a new directory. I was wondering if I am able to automate this with PHP.
Each of the articles is a separate subdirectory with an index.html file inside of it, along with any subpages and images. How do I detect when a new article has been uploaded to the main folder, find out the location of said article, as well as read an XML (or JSON? I'm not sure yet, which do you suggest?) file inside the article directory that defines the Name of the article, the data it was created, and other information and log all that to the Mysql Database?
This code must not log an article twice, as the information within the database will be picked up by another script that will display a list of all my articles.
i am wondering if the idea I have is even do-able, or does it need to be thrown away? If it is possible, are there any resources that offer tutorials on things related to this?

copy current HTML page state to MySQL table so users can access later the saved file

Ok so my problem here is that I have created a page with a table. this table can be cloned infinite times and data is inserted or manually, or from data fetched from a sql table. This means each table created is different.
The point of this page is for users to create their own personal tables, and then be able to save the current page on a specific sql table so they can head back to it whenever they like and therefor load it back to its saved state.
This means each user will have used the same table, but each users tables will have different values in it!
I have very little knowledge in php and have just recently started to use jquery. My question is how can I work out a way to copy the current html page state, and save it to a specific sql table?
This php grabs everything inside <html> and </html>.
<?PHP
echo "<button class=\"Button\" onclick=\"console.log(getPageHTML());\">Print all html</button>";
?>
so far so good. How can i now save this to a sql table? Php will be needed but how?
Hope all this was clear enough.
Help would be great on this one. Thanks
The getPageHTML() is executed on the client's side, so you have to upload it to the server again, where you will have to save the state into your database.
Things you need:
a structure to save the table in the database
knowledge of GET (POST would be better) or AJAX (best) requests
minimal SQL knowledge
You could also save the data to a static file on the server (but don't forget to prevent them from overriding someone elses table).
My idea:
button uploads only table to server
server checks for content size (eg. <=1KB)
server creates hash (best would be SHA512)
server saves the file to .html (with a header and footer to surround the table)
send the user the link to the site
links:
https://en.wikipedia.org/wiki/Http#Request_methods
http://www.tizag.com/phpT/postget.php
https://en.wikipedia.org/wiki/Json
That project could take a while to realize ;)
EDIT:
You could give the table an id (<table id="someusefulid">…</table>) and then use JavaScripts document.getElementById() to retrieve its HTML.
Have a look at this site. It has plenty of examples. First of all look at the basics of JavaScript's DOM to be able to access the table, then try to upload it somehow (preferably AJAX, which is a bit more to study) and after that you can get to the PHP code.
But do always remember, if you read or write files using PHP, be extremely cautious.

Heatmap in PHP, using mysql database

I've currently got a database with just short of 2000 client locations in Australia. What I am trying to do is to display this data on a heatmap, to be embedded into an existing website.
I've done a heap of looking around, and can't seem to find exactly what I'm after.
http://www.heatmapapi.com/sample_googlev3.aspx
http://www.heatmaptool.com/documentation.php
These are along the right lines of what I want to achieve, however I cannot see these working with data from a mysql database (require the data to be hard-coded, or uploaded through CSV files).
Has anyone come across this sort of thing before, or managed to achieve it?
Both of the examples you provide would potentially work.
With the first you would need to use the data you have to dynamically generate the javascript, or at least the values that go into the javascript.
The second is probably the better option. You would provide a path to the script that would dynamically generate a CSV file.

Should I store php/mysql modules in database or files?

I have 10 different modules that are created to be used for the index page. I've created a jquery drop/sort function for the backend, so the admin of the site can change the layout of the front page without coding knowledge. However I'm having a problem finding a way to store the modules.
I have two types of modules:
1. An image, title, link and a dynamic php/mysql generated content (eg. last five reviews)
2. An image, title, link and plain text
What is the best way to store the modules that makes it possible to edit them easily in the backend? Should I store the php/mysql part in seperate php files or in mysql?
Any tips on making a good backend module solution would be appreciated.
Thanks
You should be storing your files in your filesystem, and your data in the database. That is how it is designed. You may slow things down if you put your files into the database.

Categories