使用文本处理php的字段代替数据库进行增删改查

本文介绍了一个使用PHP实现的搜索历史记录管理系统,该系统能够读取、显示、删除和更新用户的搜索历史记录,同时提供了针对管理员和普通用户的差异化视图。系统通过文本文件存储数据,避免了使用数据库的复杂性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一次就是比较慢 写个模版 以后有小型的东西 可以考虑不用数据库的 那么就用字符串操作

search.php

<?php
/**
 * Created by PhpStorm.
 * User: liuan
 * Date: 2018/11/29
 * Time: 23:46
 */
header('content-type:text/html;charset=utf-8');
include_once 'lib/fun.php';
include_once 'config.php';
if ($login = checkLogin()) {
    $user = $_SESSION['user'];
}
$admin = $user['username'] == ADMIN_USER;

date_default_timezone_set('PRC');
$filename = "static/file/text/search_txt.txt";
$content = file_get_contents($filename, FILE_USE_INCLUDE_PATH);
//$content = str_replace("\n", "<br/>", $content);
$time = isset($_POST['time']) ? $_POST['time'] : "";
$search = isset($_POST['search']) ? $_POST['search'] : "";
//删除包含$time and $search的内容
//显示txt所有数据
$data = initData($content);
var_dump($data);
if (!empty($time) && !empty($search)) {
//    从0开始 所以此处写-1
    $currentIndex = -1;
    $replaceStr = '';
    foreach ($data as $v) {
        $currentIndex++;
        if ($v['time'] == $time && $v['search'] == $search) {
            //删除这个索引的内容 我们使用replcae 删除
            $title = "------------------------\n";
            $userInfo = "";
            if ($v['username']) {
                $userInfo .= "用户#" . $v['username'] . "#_OVER\n";
            }
            if ($v['email']) {
                $userInfo .= "邮箱#" . $v['email'] . "#_OVER\n";
            }
            $currentTime = "时间#" . $v['time'] . "#_OVER\n";
            $replaceStr = $title . $userInfo . $currentTime . "搜索内容#" . $v['search'] . "#_OVER\n";
            break;
        }
    }

    $fp = fopen($filename, "w");
    $len = fwrite($fp, str_replace($replaceStr, '', $content));
//    删除了数据 再次刷新一次
    $data = initData($content);

}

/**
 * @param $content
 * @param $data2
 * @return array
 */
function initData($content)
{
    $splite = '------------------------';
    $data1 = array_filter(explode($splite, $content));
    $data = array();
    foreach ($data1 as $value) {
        $data2 = array();
        $res = $value . explode("\n", $value)[0];
        $data2['username'] = getNeedText("用户", $res);
        $data2['email'] = getNeedText("邮箱", $res);
        $data2['time'] = getNeedText("时间", $res);
        $data2['search'] = getNeedText("搜索内容", $res);
        $data[] = $data2;
//    $data['']=
    }
    return $data;
}


function getNeedText($str, $allText)
{
    $res = '';
    if (strstr($allText, $str)) {
        $date = explode($str . '#', $allText);
        $date = explode('#_OVER', $date[1]);
        $res = $date[0];
    }
    return $res;
}


?>
<!DOCTYPE html>
<html lang="en">
<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/index.css"/>
    <link rel="stylesheet" type="text/css" href="static/css/detail.css"/>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<!--导航栏-->
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
    <div class="container-fluid">
        <div class="navbar-header">
            <button class="navbar-toggle" data-toggle="collapse" data-target="#example-navbar-collapse">
                <span class="sr-only">切换导航</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <div class="logo f1">
                <img src="static/image/logo.png" onclick="javascript:location.href='index.php'">
            </div>

        </div>

        <div class="collapse navbar-collapse" id="example-navbar-collapse">

            <ul class="nav navbar-nav pull-right">
                <?php if ($login): ?>

                    <li class="dropdown">
                        <a href="" class="dropdown-toggle"
                           data-toggle="dropdown"
                           <?php if ($user['vip'] == 1): ?>style="color: red"<?php endif; ?>><span>管理员:<?php echo $user['username'] ?>
                                <b
                                        class="caret"></b></span></a>

                        <ul class="dropdown-menu">
                            <?php if ($user['vip'] == -1): ?>
                                <li><a href="open_vip.php">开通vip</a></li>
                            <?php endif; ?>
                            <li><a href="bought.php">已购买</a></li>
                            <li><a href="https://shop107973033.taobao.com">淘宝店铺1</a></li>
                            <li><a href="https://shop405007575.taobao.com/">淘宝店铺2</a></li>
                            <li><a href="collected.php">已收藏</a></li>
                            <li class="divider"></li>

                            <li><a href="login_out.php">退出</a></li>

                        </ul>
                    </li>
                    <li><a href="help.php" target="_self">帮助</a></li>


                <?php else: ?>
                    <li><a href="login.php">登录</a></li>
                    <li><a href="register.php">注册</a></li>
                    <li><a href="help.php" target="_self">帮助</a></li>
                <?php endif; ?>
            </ul>
        </div>
    </div>
</nav>


<div style="margin-top: 80px">
    <!-- <p class="mg-b20 text-center font16"><a href="register.php">注册</a></p>-->


    <h2 class="text-center font32"><a href="commit.php">课程需求登记</a></h2>
    <?php if (count($data) <= 0): ?>
        <p style="font-size: 16px" class="text-center">暂时还没有搜索历史 <a
                    href="index.php">去搜索</a></p>
    <?php else: ?>
        <div class="mg-t20">
            <h3>用户搜索历史</h3>
            <table border="1">
                <tr class="row">


                    <?php if ($admin): ?>
                        <th class="col-1 col-lg-1 col-md-1 col-xs-1">个数</th>
                        <th class="col-2 col-lg-2 col-md-2 col-xs-2">用户</th>
                        <th class="col-4 col-lg-4 col-md-4 col-xs-4">搜索内容</th>
                        <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">联系方式</th>
                        <th class="col-1 col-lg-1 col-md-1 col-xs-1"></th>
                    <?php else: ?>
                        <th class="col-3 col-lg-3 col-md-3 col-xs-3">个数</th>
                        <th class="col-6 col-lg-6 col-md-6 col-xs-6">搜索内容</th>
                        <th class="col-3 col-lg-3 col-md-3 col-xs-3">时间</th>
                    <?php endif; ?>
                </tr>
                <?php foreach ($data as $v):
                    $i++;
                    ?>

                    <tr class="row">

                        <?php if ($admin): ?>
                            <td class="col-1 col-lg-1 col-md-1 col-xs-1"><?php echo $i ?></td>
                            <td class="col-2 col-lg-2 col-md-2 col-xs-2"><?php echo $v['username'] ?></td>
                            <td class="col-4 col-lg-4 col-md-4 col-xs-4"><?php echo $v['search'] ?></td>
                            <td class="col-2 col-lg-2 col-md-2 col-xs-2"><?php echo $v['time'] ?></td>
                            <td class="col-2 col-lg-2 col-md-2 col-xs-2"><?php echo $v['email'] ?></td>
                            <td class="col-1 col-lg-1 col-md-1 col-xs-1">
                                <form action="search.php" method="post">
                                    <input type="hidden" name="time" value="<?php echo $v['time'] ?>">
                                    <input type="hidden" name="search" value="<?php echo $v['search'] ?>">
                                    <button type="submit" class="btn btn-danger delete">删除</button>
                                </form>
                            </td>
                        <?php else: ?>
                            <td class="col-3 col-lg-3 col-md-3 col-xs-3"><?php echo $i ?></td>
                            <td class="col-6 col-lg-6 col-md-6 col-xs-6"><?php echo $v['search'] ?></td>
                            <td class="col-3 col-lg-3 col-md-3 col-xs-3"><?php echo $v['time'] ?></td>
                        <?php endif; ?>
                    </tr>
                <?php endforeach; ?>
            </table>
        </div>
    <?php endif; ?>

</div><!--row end-->


</body>
<script>
    //设置购买类型
    function setType(val) {
        document.getElementById('type').value = val;
    }

    //    开通逻辑
    $(function () {
        $('#open').click(function () {
            <?php if($login):?>
            $("#div_pay").show();
            $("#open").hide();
            <?php else:?>
            window.location.href = "login.php"
            <?php endif;?>
        });
    })

    /*搜索逻辑*/
    function onSearch() {
        var text = $('#search').val();

        if (text.length > 0) {
            //如果搜索框不是空的 就刷新goods 的数值
            window.location.href = "index.php?search=" + text
        } else {
            window.location.href = "index.php"
        }
    }

    /* 搜索框  */
    function onKeyDown(event) {
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if (e && e.keyCode == 27) { // 按 Esc 
            //要做的事情
            $('#search').val("");
        }
        if (e && e.keyCode == 113) { // 按 F2 
            //要做的事情
            window.location.href = "index.php"
        }
        if (e && e.keyCode == 13) { // enter 键
            onSearch();
        }

    }

    $(function () {
        $('.del').on('click', function () {
            if (confirm('确认删除该商品么?')) {
                window.location = $(this).attr('href');
            }
            return false;
        });

    })
</script>


</html>

这些只是 删除 和修改 查询

那么问题来了 插入呢

在这里呢

if ($total == 0 && $search_type == 1 && !empty($search)) {
    $uploadFile = "./static/file/text/search_txt.txt";
    $title = "------------------------\n";
    $userInfo = "";
    if ($login) {
        $userInfo .= "用户#" . $user['username'] . "#_OVER\n";
        $userInfo .= "邮箱#" . $user['email'] . "#_OVER\n";
    }
    $currentTime = "时间#" . date("Y-m-d H:i:s", $_SERVER['REQUEST_TIME']) . "#_OVER\n";
    $content = $title . $userInfo . $currentTime . "搜索内容#" . $search . "#_OVER\n";
    file_put_contents($uploadFile, urldecode($content), FILE_APPEND);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值