The following describes a solution which works perfectly to echo results as HTML table. I need to apply same php rules to create XML output for a sitemap.
Please suggest how to implement my code to give XML output or point me to any other solution.
Scenario
sitemap.csv
1,https://example.com/page1,monthly,1
7,https://example.com/page2,monthly,0.5
14,https://example.com/page3,monthly,0.5
21,https://example.com/page4,monthly,0.5
This is a static website where all pages are already created but should only appear in sitemap at rate of 1 per week ie 10 days after 09 Feb 2020 $launchdate only first two rows should be visible in sitemap.
1st column should not be echoed - it is used to determine if that row should be echoed at load time.
I have looked at many stripts to convert csv to XML but all of them display all columns. I have tried for hours to come up with a solution to output results in XML format.
I know final solution will need to include something like:
header(βContent-Type: application/xml; charset=utf-8β);
echo β<?xml version="1.0" encoding="UTF-8"?>β.PHP_EOL;
echo ββ .PHP_EOL;
I know final solution will need to exclude the echo of date data at top - currently used for testing
I know final solution will need to replace the column descriptors
eg Loc will become <loc> URL </loc> then <lastmod> <changefreq> <priority>
======
Working code to echo HTML table
<?php
//Sources for this solution
// https://thisinterestsme.com/reading-csv-file-with-php/
// and & https://stackoverflow.com/questions/40285479/filtering-and-displaying-a-csv-file-with-php
// Final solution should be xml not HTML table
$date = date("Y-m-d"); // Todays Date
$date_number = strtotime($date); // Todays date as Integer
$launchdate_number = strtotime('2020-02-09'); //Launch Date as Integer 1581206400
$days_since_launch = ($date_number - $launchdate_number) / 86400; // Days since Launch date
// Echo data for testing, will not be in the final XML file
echo "date ";
echo $date;
echo " ";
echo "date_number ";
echo $date_number;
echo " ";
echo "launchdate ";
echo $launchdate_number;
echo " ";
echo "days_since_launch ";
echo " ";
echo $days_since_launch;
echo "<br>";
$fileHandle = fopen("sitemap.csv", "r");
//Loop through the CSV rows.
while (($row = fgetcsv($fileHandle, 0, ",")) !== FALSE) {
if ($row[0] > $days_since_launch) continue; // display only pages were value in 1st column[0]less that $days_since_launch
//Print out my column data - exclude 1st column[0]
echo 'Loc: ' . $row[1] . '<br>';
echo 'Lastmod: ' . $date . '<br>';
echo 'changefreq: ' . $row[2] . '<br>';
echo 'priority: ' . $row[3] . '<br>';
echo '<br>';
}
fclose($file_handle);
?>