一段逻辑判断,实现不了目的
class
news
php
title
一段逻辑判断,实现不了目的,求助
想只用一个页面,显示新闻系统的三种功能:"index.php?news_title=xxx?news_class=xxx",需要达到以下目的:
1,当news_title和news_class元素均为空或者都有值时,该页面显示全部新闻的标题列表;
2,当news_title和news_class两个元素中,一个有值另一个无值时,该页面显示元素有值的内容。
------算法逻辑和代码实现:
- PHP code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> //首先,取得浏览器的传值:news_title和news_class if(isset($_GET['news_class'])) { $news_class=$_GET['news_class']; } else { $news_class=''; } if(isset($_GET['news_title'])) { $news_title=$_GET['news_title']; } else { $news_title=''; } //然后开始判断,准备显示有关的内容 if ($news_title='')//当news_title元素为空时,开始判断news_class元素是否为空 { if ($news_class='')//news_class也为空,则该页面显示所有news表的news_title列表 { $sql="SELECT news_title,in_time FROM news"; $result = mysql_query($sql); while ($row=mysql_fetch_assoc($result)) { ?> <tr> <td> <?php echo $row['news_title']; ?> <?php echo $row['in_time']; ?> </td> </tr> <?php } mysql_free_result($result); } else//当news_class不为空,则该页面显示news_class的相关内容 { $sql="SELECT * FROM news where news_class='$news_class'"; $result = mysql_query($sql); while ($row=mysql_fetch_assoc($result)) { ?> <tr> <td> <?php echo $row['news_title']; ?> <?php echo $row['in_time']; ?> </td> </tr> <?php } mysql_free_result($result); } } else //当news_title不为空值时,开始判断news_class是否为空 { if ($news_class='')//news_class为空,则该页面show出news_title的相关数据 { $sql="SELECT * FROM news where news_title='$news_title'"; $result = mysql_query($sql); while ($row=mysql_fetch_assoc($result)) { ?> <tr> <td> <?php echo $row['news_title']; ?> Author : <?php echo $row['news_author']; ?> Date : <?php echo $row['in_time']; ?> <?php echo $row['news_content']; ?> </td> </tr> <?php } mysql_free_result($result); } else//当news_class也不为空值时,则该页面显示所有news表的数据(和两者都为空时一样) { $sql="SELECT news_title,in_time FROM news"; $result = mysql_query($sql); while ($row=mysql_fetch_assoc($result)) { ?> <tr> <td> <?php echo $row['news_title']; ?> <?php echo $row['in_time']; ?> </td> </tr> <?php } mysql_free_result($result); } }
登录后复制
以上代码在最终显示时,全部显示为新闻的标题列表了,即:new_title列表。
敢问各位大虾,我这逻辑错了,还是写法有问题?
------解决方案--------------------
你这样写不太好呢。
你请求数据与逻辑与表现的html代码都搞在一起,
最好是先逻辑判断好。再去请求数据。最后一个foreach出html。
看你代码很吃力不如从新写了。=_=zzz
------解决方案--------------------
index.php?news_title=xxx?news_class=xxx
中的参数串,不是常规的格式
常规的是 index.php?news_title=xxx&news_class=xxx
你个到的是 array( 'news_title' => 'xxx?news_class=xxx')
需加工后方可使用
------解决方案--------------------
- PHP code
if ($a && $b) { // 显示全部 } elseif ($a) { ; } elseif ($b) { ; } <br><font color="#e78608">------解决方案--------------------</font><br>url 要写作 index.php?news_title=xxx&news_class=xxx<br><br>$news_title = $news_class = '';<br>if(isset($_GET['news_title'])) $news_title = $_GET['news_title'];<br>if(isset($_GET['news_class'])) $news_class = $_GET['news_class'];<br><br>if($news_title != '' && $news_class != '') {<br> //两个都有 <div class="clear"> </div>
登录后复制
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前
By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
3 周前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保护它?
3 周前
By DDD

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写
