一段逻辑判断,实现不了目的,求助
想只用一个页面,显示新闻系统的三种功能:"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>

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!
