开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 186|回复: 2
收起左侧

[已解决] PHP求改代码

 关闭 [复制链接]
结帖率:83% (55/66)
发表于 2025-9-3 12:31:49 | 显示全部楼层 |阅读模式   重庆市重庆市
28精币
<?php
    $filename= 'text.txt';
    if (file_exists($filename)&&filesize($filename)>0) {
    $str= file_get_contents($filename);
    $userinfo= unserialize($str);
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>查看页面</title>
</head>
<body>
    <h3 style="text-align: center;">列表页面-<a href="index.php">添加页面</a></h3>
    <table style="margin: 0 auto;" border="1" width="60%"aligh="center" cellpadding="5" cellsacing="0" bgcolor="" >
    <tr style="text-align: center;" bgcolor="E1E1E1">
    <td>序列</td>
    <td>姓名</td>
    <td>标题</td>
    <td>内容</td>
    <td>TIME</td>
    <td>类型</td>
    <td>操作</td>
    </tr>
    <?php foreach ($userinfo as $key =>$val){
        ?>
    <tr style="text-align: center;">
    <td><?php echo $key;?></td>
    <td><?php echo $val['username']?></td>
    <td><?php echo $val['title']?></td>
    <td><?php echo $val['content']?></td>
    <td><?php echo $val['time']?></td>
    <td><img width="30" height="20" src="img/<?php echo $val['dengj'];?>" alt=""/></td>
        <form action="del.php"method="get">
        <input type="hidden" name="Linenumber" value= <?php echo $key;?> />
                   <td colspan="1"><input type="submit" value="删除" /></td>
           </tr>
    </form>

    </tr>
        <?php
    }?>
    </table>
</body>
</html>

711c127c-0bf8-477c-9659-33eb87068a7d.png
求把代码改写成 可以cha询指定内容的形式显示
不是全部显示出来 有这一条内容才显示
就是一个搜索功能

最佳答案

查看完整内容

[mw_shl_code=php,true] 查看页面 列表页面-添加页面

回答提醒:如果本帖被关闭无法回复,您有更好的答案帮助楼主解决,请发表至 源码区 可获得加分喔。
友情提醒:本版被采纳的主题可在 申请荣誉值 页面申请荣誉值,获得 1点 荣誉值,荣誉值可兑换荣誉会员、终身vip用户组。
快捷通道:申请荣誉值

结帖率:100% (109/109)

签到天数: 11 天

发表于 2025-9-3 12:31:50 | 显示全部楼层   浙江省温州市
[PHP] 纯文本查看 复制代码
<?php
$filename = 'text.txt';
$searchKeyword = isset($_GET['search']) ? trim($_GET['search']) : '';

if (file_exists($filename) && filesize($filename) > 0) {
    $str = file_get_contents($filename);
    $userinfo = unserialize($str);

    // 根据搜索关键词过滤数据
    if (!empty($searchKeyword)) {
        $filteredUserinfo = [];
        foreach ($userinfo as $key => $val) {
            // 检查用户姓名、标题、内容是否包含关键词,只要有一项包含就保留
            if (stripos($val['username'], $searchKeyword) !== false || stripos($val['title'], $searchKeyword) !== false || stripos($val['content'], $searchKeyword) !== false) {
                $filteredUserinfo[$key] = $val;
            }
        }
        $userinfo = $filteredUserinfo;
    }
} else {
    $userinfo = [];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>查看页面</title>
</head>
<body>
    <h3 style="text-align: center;">列表页面-<a href="index.php">添加页面</a></h3>
    
    <!-- 搜索框 -->
    <form action="" method="get" style="text-align: center; margin: 20px 0;">
        <input type="text" name="search" placeholder="输入关键词搜索" value="<?php echo htmlspecialchars($searchKeyword); ?>" />
        <input type="submit" value="搜索" />
    </form>

    <table style="margin: 0 auto;" border="1" width="60%" align="center" cellpadding="5" cellspacing="0" bgcolor="">
    <tr style="text-align: center;" bgcolor="E1E1E1">
        <td>序列</td>
        <td>姓名</td>
        <td>标题</td>
        <td>内容</td>
        <td>TIME</td>
        <td>类型</td>
        <td>操作</td>
    </tr>
    <?php foreach ($userinfo as $key => $val) { ?>
    <tr style="text-align: center;">
        <td><?php echo $key; ?></td>
        <td><?php echo htmlspecialchars($val['username']); ?></td>
        <td><?php echo htmlspecialchars($val['title']); ?></td>
        <td><?php echo htmlspecialchars($val['content']); ?></td>
        <td><?php echo $val['time']; ?></td>
        <td><img width="30" height="20" src="img/<?php echo $val['dengj']; ?>" alt=""/></td>
        <form action="del.php" method="get">
            <input type="hidden" name="Linenumber" value="<?php echo $key; ?>" />
            <td colspan="1"><input type="submit" value="删除" /></td>
        </form>
    </tr>
    <?php } ?>
    </table>
</body>
</html>

评分

参与人数 1荣誉 +1 收起 理由
笨潴 + 1 热心帮助他人,荣誉+1,希望继续努力(*^__^*) 嘻嘻!

查看全部评分

回复

使用道具 举报

结帖率:0% (0/2)

签到天数: 11 天

发表于 2025-9-3 12:51:34 | 显示全部楼层   浙江省杭州市
<?php   
$filename= 'text.txt';   
if (file_exists($filename)&&filesize($filename)>0) {   
    $str= file_get_contents($filename);   
    $userinfo= unserialize($str);   
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
    <title>查看页面</title>
    <style>
        .search-box {
            text-align: center;
            margin: 20px 0;
        }
        .search-box input {
            padding: 8px;
            font-size: 14px;
            width: 300px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        .search-box select {
            padding: 8px;
            font-size: 14px;
            border: 1px solid #ccc;
            border-radius: 4px;
            margin-left: 10px;
        }
        .no-results {
            text-align: center;
            padding: 20px;
            color: #666;
            display: none;
        }
    </style>
</head>
<body>
    <h3 style="text-align: center;">列表页面-<a href="index.php">添加页面</a></h3>
   
    <!-- 搜索框 -->
    <div class="search-box">
        <input type="text" id="searchInput" placeholder="输入关键字搜索姓名、标题或内容..." />
        <select id="searchType">
            <option value="all">全部字段</option>
            <option value="username">姓名</option>
            <option value="title">标题</option>
            <option value="content">内容</option>
        </select>
        <button type="button" onclick="clearSearch()">清空</button>
    </div>
   
    <table style="margin: 0 auto;" border="1" width="60%" align="center" cellpadding="5" cellspacing="0" bgcolor="" id="dataTable">
        <thead>
            <tr style="text-align: center;" bgcolor="E1E1E1">
                <td>序列</td>
                <td>姓名</td>
                <td>标题</td>
                <td>内容</td>
                <td>TIME</td>
                <td>类型</td>
                <td>操作</td>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($userinfo as $key => $val){ ?>
            <tr style="text-align: center;" class="data-row">
                <td><?php echo $key;?></td>
                <td class="username"><?php echo $val['username']?></td>
                <td class="title"><?php echo $val['title']?></td>
                <td class="content"><?php echo $val['content']?></td>
                <td><?php echo $val['time']?></td>
                <td><img width="30" height="20" src="img/<?php echo $val['dengj'];?>" alt=""/></td>
                <td>
                    <form action="del.php" method="get" style="margin: 0;">
                        <input type="hidden" name="Linenumber" value="<?php echo $key;?>" />                  
                        <input type="submit" value="删除" />
                    </form>
                </td>
            </tr>
            <?php } ?>
        </tbody>
    </table>
   
    <div class="no-results" id="noResults">
        没有找到匹配的记录
    </div>

    <script>
        // 搜索功能
        function performSearch() {
            const searchInput = document.getElementById('searchInput');
            const searchType = document.getElementById('searchType');
            const searchTerm = searchInput.value.toLowerCase().trim();
            const searchField = searchType.value;
            const rows = document.querySelectorAll('.data-row');
            const noResults = document.getElementById('noResults');
            
            let visibleCount = 0;
            
            rows.forEach(function(row) {
                let shouldShow = false;
               
                if (searchTerm === '') {
                    shouldShow = true;
                } else {
                    if (searchField === 'all') {
                        // 搜索所有字段
                        const username = row.querySelector('.username').textContent.toLowerCase();
                        const title = row.querySelector('.title').textContent.toLowerCase();
                        const content = row.querySelector('.content').textContent.toLowerCase();
                        
                        shouldShow = username.includes(searchTerm) ||
                                   title.includes(searchTerm) ||
                                   content.includes(searchTerm);
                    } else {
                        // 搜索指定字段
                        const fieldContent = row.querySelector('.' + searchField).textContent.toLowerCase();
                        shouldShow = fieldContent.includes(searchTerm);
                    }
                }
               
                if (shouldShow) {
                    row.style.display = '';
                    visibleCount++;
                } else {
                    row.style.display = 'none';
                }
            });
            
            // 显示/隐藏"没有结果"提示
            if (visibleCount === 0 && searchTerm !== '') {
                noResults.style.display = 'block';
            } else {
                noResults.style.display = 'none';
            }
        }
        
        // 清空搜索
        function clearSearch() {
            document.getElementById('searchInput').value = '';
            document.getElementById('searchType').value = 'all';
            performSearch();
        }
        
        // 绑定事件
        document.getElementById('searchInput').addEventListener('input', performSearch);
        document.getElementById('searchType').addEventListener('change', performSearch);
        
        // 支持回车键搜索
        document.getElementById('searchInput').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                performSearch();
            }
        });
    </script>
</body>
</html>
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报QQ: 793400750,邮箱:wp@125.la
网站简介:精易论坛成立于2009年,是一个程序设计学习交流技术论坛,隶属于揭阳市揭东区精易科技有限公司所有。
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备2025452707号) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

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