In this tutorial we learn the dynamic PHP script of the XML sitemap generator. A sitemap is described for navigating the website URLs. With the sitemap, the user can easily search any URL, it is very easy. Search to navigate users on the site by sitemap. you are creating in XML encoding in any project.

Here is an example of some code of how to create a dynamic XML sitemap using Codeigniter. Below is some code with which you can create a dynamic sitemap in your project via PHP.

So you can follow below steps.

Step 1. Create A Sitemap Controller File In Your Project Folder.

First of all, Add the below code in your project(controller) file and Get the data in the database you want to create sitemap URL and pass it in the view file.

defined('BASEPATH') OR exit('No direct script access allowed');`

class Sitemap extends CI_Controller {

    function __construct() {

    public function index()
		$que = 'SELECT * FROM product';
        $arrData['product_detail'] = $this->db->query($que)->result_array();
		$this->load->view('sitemap', $arrData);

Step 2. Create Sitemap View File In Your Project Folder.

This step you can add in this bellow code in product name-wise create a dynamic XML sitemap and when this code runs at that time sitemap.xml file automatically uploaded on your root directory of the project folder.


$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
foreach ($product_detail as $pd) {
    $xmlString .=   '<url>';
	$xmlString .=  '<loc>'.base_url('product/'.$pd['name'].'</loc>';
    $xmlString .=  '</url>';

$xmlString .= '</urlset>';

$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
    echo "<h2>Site Map Created SuccessFully</h2>";
    echo "<h2>Site Map Created Failed</h2>";

I hope it can help you...