CSV to XML - filter column & rows v3

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);
?>

you can use basic control structures for whatever conditions you want

https://www.php.net/manual/en/control-structures.if.php

you can generate XML as you do with HTML, because they are very similar, or you use some wrapper:

https://www.php.net/manual/en/domdocument.createelement.php

Thanks for the links. My skills are insufficient to be able to use the links you gave to fix my code.

I have managed to find a solution with other resources + trial and error. I will add my solution to this thread.

I have managed to resolve this with additional research plus trial and error.

Code below. Hopefully it will help someone else.

Any comments or corrections welcome.

    <?php   
    //Sources for this solution
        // https://thisinterestsme.com/reading-csv-file-with-php/
        // https://stackoverflow.com/questions/40285479/filtering-and-displaying-a-csv-file-with-php 
        // https://www.webslesson.info/2017/06/make-dynamic-xml-sitemap-in-php-script.html

        header("Content-Type: application/xml; charset=utf-8");

        echo '<?xml version="1.0" encoding="UTF-8"?>'; 

        $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


        $fileHandle = fopen("sitemap.csv", "r");
        echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
        //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 '<url>' . PHP_EOL;
         echo '<loc>'. $row[1] .'</loc>' . PHP_EOL;
         echo '<lastmod>'.$date.'</lastmod>' . PHP_EOL;
         echo '<changefreq>'. $row[3] .'</changefreq>' . PHP_EOL;
         echo '<priority>'. $row[4] .'</priority>' . PHP_EOL;
         echo '</url>' . PHP_EOL;  

        }
        echo '</urlset>';
        ?>
1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service