Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
How To Create Dynamic Xml Sitemap In Codeigniter

How to Create Dynamic Xml Sitemap in Codeigniter

Posted on June 2, 2018December 17, 2022 By XpertPhp 11 Comments on How to Create Dynamic Xml Sitemap in Codeigniter

In this tutorial, we will explain to you how to create dynamic xml sitemap in codeigniter. It helps us to we can easily submit XML sitemap into the google console. so our website can easily rank on google.

A sitemap is described navigate of the website. users can easily search to navigate the site by sitemap. it is creating in the XML coding.

Read Also:
How to get last record from the mysql table in codeigniter

We have given below an example of how to generate a dynamic XML sitemap using Codeigniter. Add the below code in your controller file and Get the data from the database you want to create sitemap URL and pass it in the view file.
So you can follow the below steps.

Create Sitemap Controller

Add the below code in your controller file and Get the data from the database you want to create sitemap URL and pass it in the view file.
application/controllers/Sitemap.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
defined('BASEPATH') OR exit('No direct script access allowed');`
 
class Sitemap extends CI_Controller {
 
    function __construct() {
        parent::__construct();
    }
 
    public function index()
    {
$que = 'SELECT * FROM product';
        $arrData['product_detail'] = $this->db->query($que)->result_array();
$this->load->view('sitemap', $arrData);
    }
 
}
?>
See also  Generate a PDF file using Dompdf in Codeigniter

 

Create XML File.

Add the below code in this file. in this below code in the product name wise create a dynamic XML sitemap and when this code run at that time sitemap.xml file is automatically uploaded to your root directory of the project folder.
application/views/sitemap.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
 
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>
    <urlset
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
    <loc>http://www.example.com</loc>
</url>';
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;
$dom->loadXML($xmlString);
if($dom->save($_SERVER["DOCUMENT_ROOT"].'/sitemap.xml')){
    echo "<h2>Site Map Created SuccessFully</h2>";
}else{
    echo "<h2>Site Map Created Failed</h2>";
}
?>

you can run on following URL for submit XML sitemap into the google console.

1
http://your-project-dir/sitemap.xml

Codeigniter, MySql Tags:Codeigniter sitemap generator, codeigniter tutorial, create sitemap, how to create a sitemap, sitemap example, sitemap generator, sitemap maker, xml sitemap, xml sitemap generator

Post navigation

Previous Post: How to get last record from the mysql table in codeigniter
Next Post: How to Remove Special Character from String in PHP

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com