硬汉工作室

搜索
热搜: 活动 交友 discuz

[代码] PHP文件保存数据输出的例子

[复制链接]
发表于 2023-6-16 15:04:49 | 显示全部楼层 |阅读模式
这个是一个动态导航的demo,后台可以添加标题,链接,图片,前台显示导航,没有用到数据库,利用文件函数实现,有时候还是很有用的。
假如这个文件是index.php,那么通过index.php?admin=123456就可以进入管理后台,密码可以自行修改



前台界面




后台界面


代码如下:

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

  18.     if (isset($_POST['dosubmit'])) {
  19.         $titles = $_POST['titles'];
  20.         $urls = $_POST['urls'];
  21.         $imgs = $_POST['imgs'];

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

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

  42. }
  43. ?>
  44. <?php
  45. if ($isAdmin):
  46.     ?>
  47.     <style>
  48.         .adminbox {
  49.             font-size: 14px;
  50.             padding: 10px;
  51.             border: solid 1px #ccc;
  52.             width: 800px;
  53.             margin: 10px auto;
  54.         }

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

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

  61.         .f-input {
  62.             padding: 5px;
  63.         }

  64.         .dosubmit {
  65.             width: 100px;
  66.             background: #F00;
  67.             color: #FFF;
  68.             line-height: 30px;
  69.         }
  70.     </style>
  71.     <div class="adminbox">
  72.         <form method="post" action="">

  73.             <p><b>菜单设置</b></p>
  74.             <p style="color:red;">请注意,如果想要删除某个菜单,那么就删掉标题即可</p>
  75.             <div class="f-box">
  76.                 <?= $navHtml; ?>
  77.                 <p class="f-item">
  78.                     <input class="f-input" type="text" name="titles[]" value="" placeholder="标题"/>
  79.                     <input style="width: 250px;" class="f-input" type="text" name="urls[]" value="" placeholder="链接"/>
  80.                     <input style="width: 250px;" class="f-input" type="text" name="imgs[]" value="" placeholder="图片"/>
  81.                 </p>
  82.             </div>
  83.             <p>
  84.                 <input type="submit" class="dosubmit" name="dosubmit" value="提交"/>
  85.                   
  86.                 <a href="javascript:" onclick="addrow();">新增一行</a>
  87.             </p>
  88.         </form>
  89.     </div>
  90.     <script>
  91.         function addrow() {
  92.             $('.f-box').append(`
  93.     <p class = "f-item">
  94.     <input class = "f-input" type = "text" name = "titles[]" value = "" placeholder="标题"/>
  95.     <input style = "width: 250px;" class = "f-input" type = "text" name = "urls[]" value = "" placeholder="链接"/>
  96.     <input style = "width: 250px;" class = "f-input" type = "text" name = "imgs[]" value = "" placeholder="图片"/>
  97.     </p>
  98.     `);

  99.         }
  100.     </script>


  101. <?php
  102. else:
  103. $navs = file('data.txt');
  104. $navHtml = '';
  105. foreach ($navs as $v) {
  106.     list($t, $u, $i) = explode('|', trim($v));
  107.     $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>";
  108. }
  109. ?>

  110. <div class = "main">
  111. <?=$navHtml;?>
  112. </div>

  113. <?php
  114. endif;
  115. ?>
  116. </body>
  117. </html>
复制代码




有关PHP系统、Discuz或网站等各种问题,可以联系QQ1069971363寻求付费支持

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
Discuz插件商店:http://addon.dismall.com/?@56030.developer
回复

使用道具 举报

QQ|Archiver|手机版|小黑屋|硬汉工作室 ( 冀ICP备13021567号-9 )

GMT+8, 2024-5-17 11:07 , Processed in 0.075463 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表