• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

一段逻辑判断,实现不了目的

php 搞代码 4年前 (2022-01-24) 12次浏览 已收录 0个评论

一段逻辑判断,实现不了目的,求助
想只用一个页面,显示新闻系统的三种功能:”index.php?news_title=xxx?news_class=xxx”,需要达到以下目的:

1,当news_title和news_class元素均为空或者都有值时,该页面显示全部新闻的标题列表;

2,当news_title和news_class两个元素中,一个有值另一个无值时,该页面显示元素有值的内容。

——算法逻辑和代码实现:

PHP code

<!---ecms Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->//首先,取得浏览器的传值:news_title和news_classif(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/<em>8本文来源gao.dai.ma.com搞@代*码(网$</em><pre>搞代gaodaima码

/当news_class不为空,则该页面显示news_class的相关内容 { $sql=”SELECT * FROM news where news_class=’$news_class'”; $result = mysql_query($sql); while ($row=mysql_fetch_assoc($result)) {?>

<?php echo $row[‘news_title’]; ?> <?php echo $row[‘in_time’]; ?>

<?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)) {?>

<?php echo $row[‘news_title’]; ?> Author : <?php echo $row[‘news_author’]; ?> Date : <?php echo $row[‘in_time’]; ?> <?php echo $row[‘news_content’]; ?>

<?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)) {?>

<?php echo $row[‘news_title’]; ?> <?php echo $row[‘in_time’]; ?>

<?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) {    ;}
------解决方案--------------------
url 要写作 index.php?news_title=xxx&news_class=xxx

$news_title = $news_class = '';
if(isset($_GET['news_title'])) $news_title = $_GET['news_title'];
if(isset($_GET['news_class'])) $news_class = $_GET['news_class'];

if($news_title != '' && $news_class != '') {
//两个都有


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:一段逻辑判断,实现不了目的

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址