donglu7816 2013-10-09 15:00
浏览 51
已采纳

PHP - foreach循环WITH条件没有额外的内部IF语句

I understand that pdo/mysqli is the required standard, for the time being I have no choice other than to use msql

Currently I have this setup to loop through results and display them accordingly

foreach($Items as $Item)
{
    if($Item["Col1"] == "valueFoo"){
        echo "<li>display relevant results</li>";                   
    }
} 

Is there way I can do this without looping through the whole array if Items and instead combining the foreach loop with a condition. Alternatively filtering the array prior to the foreachloop? What is the most efficient?

Currently I have 60 items and display them in lists like as below

$SQL = "SELECT * FROM tableName WHERE Col1 IS NOT NULL";
$Data = mysql_query($SQL, $db);

$Items = array();
    while ($NewRow = mysql_fetch_assoc($Data)) {
    // Append all rows to an array
    $Items[] = $NewRow;
}

echo "<ul>";
foreach($Items as $Item)
    {
        if($Item["Col1"] == "valueFoo"){
            echo "<li>display relevant results</li>";                   
        }
    } 
echo "</ul>";
echo "<ul>";
foreach($Items as $Item)
    {
        if($Item["Col1"] == "valueDoo"){
            echo "<li>display relevant results</li>";                   
        }
    } 
echo "</ul>";
echo "<ul>";
foreach($Items as $Item)
    {
        if($Item["Col1"] == "valueHoo"){
            echo "<li>display relevant results</li>";                   
        }
    } 
echo "</ul>";

Is there a more efficient way? I'm still a newbie when it comes to php

  • 写回答

4条回答 默认 最新

  • doutuo8800 2013-10-09 15:05
    关注

    What I'd do is combine each check into one foreach, assigning the text to variables until the end, then echoing them as needed. Inside the foreach use a switch statement to avoid the multiple ifs.

    foreach($Items as $Item)
        {
          switch($Item["Col1"])
          {
             case 'valuefoo':
             $result_foo="<li>display relevant results</li>";  
             break;
             case 'valuedoo':
             $result_doo="<li>display relevant results</li>";  
             break;
             case 'valuehoo':
             $result_hoo="<li>display relevant results</li>";  
             break;
             default:
             // handle error if there is one
          }                
            }
        } 
    echo "<ul>";
    echo $result_foo;
    echo "</ul>";
    
    echo "<ul>";
    echo $result_doo;
    echo "</ul>";
    
    echo "<ul>";
    echo $result_hoo;
    echo "</ul>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛