Help With Creating An Auto Gen Sitemap

Discussion in 'PHP' started by mrhulk, Jun 11, 2009.

  1. #1
    Hi, I am trying to create an automated Sitemap that will be run through a cronjob, however am having trouble with php on how to use fopen to read a file and edit it from a specific point, in my case writing after <!-- URLS Below -->

    Php Code
    =================================
    <?php
    require_once('config.php');
    mysql_connect($server,$user,$pass);
    mysql_select_db($database);

    $sql="SELECT * FROM sitemap";
    $result=mysql_query($sql);
    $row = mysql_fetch_array($result);

    $lastid=$row[0]; //This is the value of the last URL id that was entered successfully to sitemap in my case its on 1 as its starting from begining

    $sql_url=mysql_query("SELECT * FROM books ORDER BY id ASC LIMIT $lastid, 50");

    //Using the loop to store the urls
    while($row_url = mysql_fetch_array($sql_url))
    {
    $pattern = "/[^A-Za-z0-9\-]/";
    $url_row2=str_replace(' ','-',$row_url[2]);
    $url_row3=str_replace(' ','-',$row_url[3]);
    $url_row2= preg_replace($pattern,'',$url_row2);
    $url_row3= preg_replace($pattern,'',$url_row3);
    $xml_url.='<url>';
    $xml_url.='<loc>http://www.site.com/textbook/'.$row_url[11].'-'.$url_row2.'-by-'.$url_row3.'-ISBN:'.$row[1].'</loc>';
    $xml_url.='<changefreq>never</changefreq>';
    $xml_url.='</url>';
    }

    // I am stuck here
    // This is how my .xml file is right now, I somehow need to read the <!-- URLS Below --> and add the xml urls below that, not sure how to do that in php
    +++++++++++++++++++++++++++++++++++++++++++++++
    <?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <!-- URLS Below -->
    <url>
    <loc>http://www.example.com/</loc>
    <changefreq>never</changefreq>
    </url>

    </urlset>
    +++++++++++++++++++++++++++++++++++++++++++++++++

    $fp = fopen ("/site/sitemap.xml", "w");
    //help needed here

    //update the last done field
    $lastidone=$lastid+50;
    mysql_query("UPDATE sitemap SET last_done ='.$lastdone.'");
    ?>
     
    mrhulk, Jun 11, 2009 IP
  2. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #2
    if youre parsing data from .xml into php , you might want to build your php script around it.
     
    ezprint2008, Jun 11, 2009 IP