当前位置:网站首页>php 生成sitemap
php 生成sitemap
2022-07-20 08:29:00 【安果移不动】
<?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>
边栏推荐
猜你喜欢
tp5.1 打开网址 输出的是网页代码输出 没有转变过 没有渲染(模板 return $this-&gt;fetch() return view();)
Node JS processes pictures asynchronously and JSON text asynchronously
工业4.0数字孪生下的应用案例
字符串函数的模拟实现(上)
UML图系列之序列图
File upload and download
Scala变量和数据类型(01)
Iptables firewall experiment
[El upload realizes a function of modifying avatar]
6.0 保存银联参数出错:Unable to open file
随机推荐
点播 构造自己的播放器 用户调用获取视频播放地址接口
Comparative evaluation of reverse analysis tool IDA and open source tools ghidra and cutter
Aruba学习笔记02-Web UI --Dashbord面板介绍
The best method of converting string to date in R language
[hbuilder runs to the problem that Mumu simulator cannot install the base, and it has been stuck in the installation base...]
js失去焦点获取值
R语言世界价值观调查作业
6.0 error saving UnionPay parameters: unable to open file
【C语言刷LeetCode】731. 我的日程安排表 II(M)
Alibaba cloud CDN domain name configuration method
tp5.1 打开网址 输出的是网页代码输出 没有转变过 没有渲染(模板 return $this-&gt;fetch() return view();)
Tp5.1 controller initialization initialize (inherit public file initialization)
Vs2017 modify the default include directory and Library Directory
你了解实时3D渲染吗?实时渲染软件和应用场景科普来了
Go runtime package
Regular expression matching URL
Routing of upper layer loops in VRRP
tp5.1 include 包含文件(引用公共文件)
JUC advanced -no.1 review of basic knowledge of threading
长沙大屏开发