php 生成sitemap
Posted 安果移不动
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 生成sitemap相关的知识,希望对你有一定的参考价值。
<?php
/**
* Created by PhpStorm.
* User: liuan
* Date: 2018/11/29
* Time: 23:46
*/
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
include_once 'lib/fun.php';
if ($login = checkLogin())
$user = $_SESSION['user'];
$admin = $user['username'] == ADMIN_USER;
$search = isset($_GET['search']) ? $_GET['search'] : "";
//搜索类型 1 是全匹配 2是仅仅搜索分类
$search_type = isset($_GET['search_type']) ? intval($_GET['search_type']) : 1;
$orderby = isset($_GET['orderby']) ? $_GET['orderby'] : "id";
$sort = isset($_GET['sort']) ? $_GET['sort'] : "desc";
//排序规则描述
$desc = isset($_GET['desc']) ? $_GET['desc'] : "默认";
$con = mysqlinit();
$C_TIME = date("Y-m-d", $_SERVER['REQUEST_TIME']);
if (empty($search))
$sql = "SELECT COUNT(`id`) as total from `la_goods` where ( `status`='1' $vipStr) ";
else
if ($search_type == 1)
$sql = "SELECT COUNT(`id`) as total from `la_goods` where ( `status`='1' $vipStr)and (`name` like '%$search%' or `cid` like '%$search%' or `content` like '%$search%')";
else if ($search_type == 2)
$sql = "SELECT COUNT(`id`) as total from `la_goods` where ( `status`='1' $vipStr) and `cid`='$search'";
$total = 0;
if (!$obj = mysqli_query($con, $sql))
die(mysqli_error($con));
$result = mysqli_fetch_assoc($obj);
$total = isset($result['total']) ? intval($result['total']) : 0;
$vipStr = "or `status`='2'";
$needField = '`name`,`id`,`cid`';
if (empty($search))
$sql = "SELECT $needField FROM `la_goods` where ( `status`='1' $vipStr) ORDER BY `$orderby` $sort ";
else
if ($search_type == 1)
$sql = "SELECT $needField FROM `la_goods` where ( `status`='1' $vipStr)and (`name` like '%$search%' or `cid` like '%$search%' or `content` like '%$search%') ORDER BY $orderby $sort ";
else if ($search_type == 2)
$sql = "SELECT $needField FROM `la_goods` where ( `status`='1' $vipStr) and `cid`='$search' ORDER BY $orderby $sort ";
if (!$obj = mysqli_query($con, $sql))
die("暂时还没有人发布商品");
$goods = array();
while ($result = mysqli_fetch_assoc($obj))
$goods[] = $result;
$category = getCid($vipStr, $con);
if (!$obj = mysqli_query($con, $sql))
msg(2, "数据库查询操作" . mysqli_error($con));
if (mysqli_affected_rows($con) <= 0)
echo "暂时没有数据";
die;
function getCid($vipStr, $con)
$sql = "SELECT `cid` FROM `la_goods` where( `status`='1' $vipStr)";
if (!$obj = mysqli_query($con, $sql))
die("得到所有cid出错" . mysqli_error($con));
$category = array();
$category_cid = array();
$category_status = array();
while ($result = mysqli_fetch_assoc($obj))
if (!in_array($result['cid'], $category_cid))
$category_cid[] = $result['cid'];
$category_status[] = $result['status'];
$category[] = $result;
return $category;
$goods = array_values($goods);
$key = array_column($goods, 'cid');
array_multisort($key, SORT_DESC, $goods);
$cidArray = array();
$g_xml = <<<G_XML
<?xml version="1.0" encoding="UTF-8"?>
<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">
<url>
<loc>https://www.yzdzy.com/</loc>
<lastmod>$C_TIME</lastmod>
<changefreq>always</changefreq>
</url>
G_XML;
$g_txt = "https://www.yzdzy.com\\n"
?>
<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<meta charset="UTF-8">
<title>课程列表</title>
<link rel="stylesheet" type="text/css" href="static/css/common.css"/>
<link rel="stylesheet" type="text/css" href="static/css/detail.css"/>
<link rel="stylesheet" type="text/css" href="static/lib/bootstrap/3.3.7/css/bootstrap.min.css"/>
<script src="static/lib/jquery/2.1.1/js/jquery.min.js"></script>
<script src="static/lib/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<?php getHeader(); ?>
</head>
<body>
<!--导航栏-->
<?php nav($login, $user, $admin) ?>
<!--管理员才可以看到-->
<div class="admin">
<br>
<button class="btn btn-primary" id="g_xml">生成sitemap.xml</button> <a href="sitemap.xml">查看: sitemap.xml</a>
<br>
<button class="btn btn-primary" id="g_txt">生成sitemap.txt</button> <a href="sitemap.txt">查看: sitemap.txt</a>
</div>
<div class="center-block">
<?php if (count($goods) > 0): ?>
<h3 class="so">商品列表</h3>
<table border="1">
<tr class="row">
<th class="col-2 col-lg-2 col-md-2 col-xs-2">分类名称</th>
<th class="col-2 col-lg-2 col-md-2 col-xs-2">商品id</th>
<th class="col-10 col-lg-10 col-md-10 col-xs-10">商品名称</th>
</tr>
<?php foreach ($goods as $v):
?>
<tr class="row">
<?php
if (!in_array($v['cid'], $cidArray))
$cidArray[] = $v['cid'];
$g_xml .= <<<G_XML
<url>
<loc>https://www.yzdzy.com/index.php?search=$v['cid']&search_type=2</loc>
<lastmod>$C_TIME</lastmod>
<changefreq>yearly</changefreq>
<priority>1.0</priority>
</url>
G_XML;
$g_txt .= "https://www.yzdzy.com/index.php?search=$v['cid']&search_type=2\\n";
echo "<td class='col-2 col-lg-2 col-md-2 col-xs-2'><a href='https://www.yzdzy.com/index.php?search=$v['cid']&search_type=2'>$v['cid']</a></td>";
else
echo "<td class='col-2 col-lg-2 col-md-2 col-xs-2'></td>";
?>
<td class="col-2 col-lg-2 col-md-2 col-xs-2"><?php echo $v['id'] ?></td>
<?php
$g_xml .= <<<G_XML
<url>
<loc>https://www.yzdzy.com/detail.php?id=$v['id']</loc>
<lastmod>$C_TIME</lastmod>
<changefreq>yearly</changefreq>
<priority>0.8</priority>
</url>
G_XML;
$g_txt .= "https://www.yzdzy.com/detail.php?id=$v['id']\\n";
?>
<td class="col-10 col-lg-10 col-md-10 col-xs-10"><a
href="detail.php?id=<?php echo $v['id'] ?>"><?php echo $v['name'] ?></a></td>
</tr>
<?php endforeach;
$g_xml .= "</urlset>"
?>
</table>
<?php else: ?>
<h2 class="text-center text-justify">您好像还没有购买过什么东西呢 <a href="course_list.php">去购买</a></h2>
<?php endif; ?>
</div>
<?php echo js_push() ?>
</body>
<script>
/*设置文字到上面*/
function setText(obj)
var text = $(obj).text();
var href = $(obj).attr('href');
$(obj).attr('href', href + "&desc=" + text);
// $('#order_top').text("排序:" + );
$('#g_xml').click(function ()
//生成xml
if (confirm("请确保网页加载完毕,重新生成xml 将覆盖之前的xml,确定继续么?"))
<?php file_put_contents("./sitemap.xml", urldecode($g_xml));?>
)
$('#g_txt').click(function ()
//生成xml
if (confirm("请确保网页加载完毕,重新生成txt 将覆盖之前的txt,确定继续么?"))
<?php file_put_contents("./sitemap.txt", $g_txt);?>
)
</script>
</html>
以上是关于php 生成sitemap的主要内容,如果未能解决你的问题,请参考以下文章