php - 在 while 中用 If 语句排序

标签 php mysql while-loop

我有一个带有单词的mysql数据库。我用 while 语句打印所有单词。所以我觉得:

马铃薯 番茄 生菜

一切正常,但我想按长度对单词进行排序。我试过:

if(strlen($go['words']) == 4 ){ echo "this are the 4 letter words:"; }

但这将在每 4 个字母的单词之前打印句子。然而,我希望它只打印 1 次作为所有 4 个字母单词的标题。当然,我也想对 5、6、7 个字母的单词执行此操作。

我考虑过进行多个 sql 查询,但是服务器无法处理大量访问者。

最佳答案

你可以像这样使用一个临时变量:

$tempLen = 0;
while(...)
{
    if(strlen($go['words']) == 1 && $tempLen < 1){ 
        echo "this are the 1 letter words:"; 
        $tempLen = 1;
    }
    ...
    if(strlen($go['words']) == 4 && $tempLen < 4){ 
        echo "this are the 4 letter words:"; 
        $tempLen = 4;
    }
    ...
}

关于php - 在 while 中用 If 语句排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25342317/

相关文章:

php - Symfony2:调用未定义的方法 Doctrine\ORM\QueryBuilder::getResult()

php - SQLSTATE[HY093] : Invalid parameter number INSERT

php - 为什么 PDO 在 wamp 中不起作用?

php - 如何迭代 mysqli_fetch_all() 方法

java - 将 Java Applet 连接到 MySQL DB - 通信链路故障

php - 带有请求正文的 Laravel DELETE 方法

php - facebook graph api在提要中只获取一张图片

php - 如何在没有 FETCHALL 的情况下在 MYSQL PDO 中获取 2 次

c++ - 如何使用 do/while、While 语句或 For 语句查找用户输入的多个整数之和

c++ - While循环什么时候使用OR或AND?