nciaer 发表于 2023-6-16 15:04:49

PHP文件保存数据输出的例子

这个是一个动态导航的demo,后台可以添加标题,链接,图片,前台显示导航,没有用到数据库,利用文件函数实现,有时候还是很有用的。
假如这个文件是index.php,那么通过index.php?admin=123456就可以进入管理后台,密码可以自行修改



前台界面



后台界面

代码如下:
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>文件保存数据</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <meta name="viewport" content="initial-scale=1,maximum-scale=1, minimum-scale=1">
    <script src="https://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
<?php
if(!file_exists('data.txt')) touch('data.txt');
$pwd = '123456'; // 这是密码
$isAdmin = 0;
if ($_REQUEST['admin'] == $pwd) {
    $isAdmin = 1;

    if (isset($_POST['dosubmit'])) {
      $titles = $_POST['titles'];
      $urls = $_POST['urls'];
      $imgs = $_POST['imgs'];

      $navStr = '';
      foreach ($titles as $k => $v) {
            if ($v) {
                $navStr .= $v . '|' . $urls[$k] . '|' . $imgs[$k] . "\n";
            }
      }

      file_put_contents('data.txt', $navStr);
    }
    $navs = file('data.txt');
    $navHtml = '';
    foreach($navs as $v) {
      list($t, $u, $i) = explode('|', trim($v));
      $navHtml .= "
    <p class = 'f-item'>
    <input class = 'f-input' type = 'text' name = 'titles[]' value = '{$t}' placeholder='标题'/>
    <input style = 'width: 250px;' class = 'f-input' type = 'text' name = 'urls[]' value = '{$u}' placeholder='链接'/>
    <input style = 'width: 250px;' class = 'f-input' type = 'text' name = 'imgs[]'' value = '{$i}' placeholder='图片'/>
    </p>         
      ";
    }

}
?>
<?php
if ($isAdmin):
    ?>
    <style>
      .adminbox {
            font-size: 14px;
            padding: 10px;
            border: solid 1px #ccc;
            width: 800px;
            margin: 10px auto;
      }

      .adminbox p {
            margin-bottom: 10px;
      }

      .adminbox .f-item {
            margin-bottom: 20px;
      }

      .f-input {
            padding: 5px;
      }

      .dosubmit {
            width: 100px;
            background: #F00;
            color: #FFF;
            line-height: 30px;
      }
    </style>
    <div class="adminbox">
      <form method="post" action="">

            <p><b>菜单设置</b></p>
            <p style="color:red;">请注意,如果想要删除某个菜单,那么就删掉标题即可</p>
            <div class="f-box">
                <?= $navHtml; ?>
                <p class="f-item">
                  <input class="f-input" type="text" name="titles[]" value="" placeholder="标题"/>
                  <input style="width: 250px;" class="f-input" type="text" name="urls[]" value="" placeholder="链接"/>
                  <input style="width: 250px;" class="f-input" type="text" name="imgs[]" value="" placeholder="图片"/>
                </p>
            </div>
            <p>
                <input type="submit" class="dosubmit" name="dosubmit" value="提交"/>
                  
                <a href="javascript:" onclick="addrow();">新增一行</a>
            </p>
      </form>
    </div>
    <script>
      function addrow() {
            $('.f-box').append(`
    <p class = "f-item">
    <input class = "f-input" type = "text" name = "titles[]" value = "" placeholder="标题"/>
    <input style = "width: 250px;" class = "f-input" type = "text" name = "urls[]" value = "" placeholder="链接"/>
    <input style = "width: 250px;" class = "f-input" type = "text" name = "imgs[]" value = "" placeholder="图片"/>
    </p>
    `);

      }
    </script>


<?php
else:
$navs = file('data.txt');
$navHtml = '';
foreach ($navs as $v) {
    list($t, $u, $i) = explode('|', trim($v));
    $navHtml .= "<div style = 'float: left;border: solid 1px #DCDCDC;width: 150px; text-align: center; margin-right: 20px;'><a href='{$u}'><img src='{$i}' style = 'width: 80px;height: 40px;'><p>{$t}</p></a></div>";
}
?>

<div class = "main">
<?=$navHtml;?>
</div>

<?php
endif;
?>
</body>
</html>



页: [1]
查看完整版本: PHP文件保存数据输出的例子