1. 程式人生 > >使用文字處理php的欄位代替資料庫進行增刪改查

使用文字處理php的欄位代替資料庫進行增刪改查

第一次就是比較慢 寫個模版 以後有小型的東西 可以考慮不用資料庫的 那麼就用字串操作

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);
}