Generating sitemap.xml file by reading data from a directory.As for the required format of sitemap and to keep it simple we will be using the minimum required information i.e. the URL only to generate the sitemap.xml file. There is some optional information which can be added and that can be discussed separately. Here is a standard format of sitemap.xml file.
In the above code the required data is URL and other data are optional . So we will use this data only to generate our xml file. Like our previous example of storing data in mysql table , we will use z_db1.php ( read about z_db1.php file at connect to table ) to connect to MySQL database. We have used PDO query to get the records from table. Then we have used the file write functions to crate and write the content to our sitemap.xml file. Here is the complete code.
Why to store data in MySQL tableAs you can understand we can also create xml file by reading the files present inside the directory without storing them in a table. The main advantage of storing them in a table is we can set the priority , update schedule etc manually from an admin area for each file and then with that data generate a sitemap.xml file. Storing the file details in a table also helps us in planning further improvement to content by reading title, descriptions and keywords with H1 tags etc.This way it can be part of a complete content management script. Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
|