Make Dynamic XML sitemap using PHP
What is a sitemap?
Types of sitemaps
DATABASE TEST TABLE NAMED AS pages
CREATE TABLE `pages` (
`page_id` int(11) NOT NULL,
`page_title` text NOT NULL,
`page_url` text NOT NULL,
`date` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;
ALTER TABLE `pages`
ADD PRIMARY KEY (`page_id`);
ALTER TABLE `pages`
MODIFY `page_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11;
INSERT INTO `pages` (`page_id`, `page_title`, `page_url`, `date`) VALUES
(1, 'Comment System using PHP, Ajax and MySQLi', '7/comment-system-using-php-ajax-and-mysqli', '2025-10-23'),
(2, 'Chat Script Similar to Facebook using Ajax and PHP', '32/chat-script-similar-to-facebook-using-ajax-and-php', '2025-10-23'),
(3, 'Create Treeview with Bootstrap Treeview Ajax JQuery in PHP\r\n', 'create-treeview-with-bootstrap-treeview-ajax-jquery-in-php', '2025-10-23'),
(4, 'Bootstrap Multiselect Dropdown with Checkboxes using Jquery in PHP\r\n', 'bootstrap-multiselect-dropdown-with-checkboxes-using-jquery-in-php', '2025-10-23'),
(5, 'Facebook Style Popup Notification using PHP Ajax Bootstrap\r\n', 'facebook-style-popup-notification-using-php-ajax-bootstrap', '2025-10-23'),
(6, 'Modal with Dynamic Previous & Next Data Button by Ajax PHP\r\n', 'modal-with-dynamic-previous-next-data-button-by-ajax-php', '2025-10-23'),
(7, 'How to Use Bootstrap Select Plugin with Ajax Jquery PHP\r\n', 'how-to-use-bootstrap-select-plugin-with-ajax-jquery-php', '2025-10-23'),
(8, 'How to Load CSV File data into HTML Table Using AJAX jQuery\r\n', 'how-to-load-csv-file-data-into-html-table-using-ajax-jquery', '2025-10-23'),
(9, 'Autocomplete Textbox using Typeahead with Ajax PHP Bootstrap\r\n', 'autocomplete-textbox-using-typeahead-with-ajax-php-bootstrap', '2025-10-23'),
(10, 'Export Data to Excel in Codeigniter using PHPExcel\r\n', 'export-data-to-excel-in-codeigniter-using-phpexcel', '2025-10-23');
Create a dot php file and name it as sitemap.php, copy and paste the following code in this file, save the file and view it via your web browser to see your sitemap in action.
Code for sitemap.php
<?php // Database Connection Details $connect = mysqli_connect("localhost", "root", "", "vasplus"); // Input your hostname, username, password and database name $data = mysqli_query($connect, "select page_url,date from pages"); // Query the pages table to get all URL and dates posted if( mysqli_num_rows($data) > 0 ) // Check if we have some data in the database pages table { $base_url = "http://localhost/tutorial/"; header("Content-Type: application/xml; charset=utf-8"); echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL; 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">' . PHP_EOL; // Fetch all needed data and display in the sitemap while($row = mysqli_fetch_array($data)) { ob_start(); echo '<url>'.PHP_EOL; echo '<loc>'.$base_url. $row["page_url"] .'</loc>'.PHP_EOL; echo '<lastmod>'.$row["date"].'</lastmod>'.PHP_EOL; echo '<changefreq>daily</changefreq>'.PHP_EOL; echo '</url>'.PHP_EOL; ob_end_flush(); } echo '</urlset>'.PHP_EOL; } else { // There is no data to display in our sitemap at the moment. } ?>
Screen Shots
Click on a photo below to scroll through the screen shots of the application!
Submit your Job or Project Today!
We can help you turn your idea into reality, take over your existing project, or extend your current development team.
Submit your idea job or project below and we will follow up with you shortly.
OUR OBJECTIVE
Our objective is to reach a place where our services will be highly regarded by businesses from various industrial domains for building their innovative busines solutions with our cutting-edge technological expertise, interactive designs and uncompromised quality.
OUR MISSION
We aspire to help businesses ranging from startups to enterprises, who reach out to us with their requirements, in achieving great lengths, expanding their reach, upscaling their products, and generate a large user-base with our outstanding and cost-effective services.
