duanmu3049 2015-07-31 10:55
浏览 52
已采纳

检查并在for循环中显示多个值

I have for loop in which I have to check that the array that I am retriving should not empty. And if empty then don't display that field.

        $checkboxes = array();
    $text = array();
    $Enhanced = array();
    $Search = array();
    $Landing = array();
    $spotlights="";
    for ($i = 1; $i <= 20; $i++) {
         $checkboxes[$i] = $_POST[$i];
         $text[$i] = $_POST[$i.'t'];
    }

    for ($p = 1; $p <= 20; $p++) {
         $checkboxes[$p] = $_POST[$p];
         $Enhanced[$p] = $_POST['Enhanced'.$p];
    }
    for ($q = 1; $q <= 20; $q++) {
         $checkboxes[$q] = $_POST[$q];
         $Search[$q] = $_POST['Search'.$q];
    }
    for ($r = 1; $r <= 20; $r++) {
         $checkboxes[$r] = $_POST[$r];
         $Landing[$r] = $_POST['Landing'.$r];
    }
 for ($j = 1; $j <= 20; $j++) {

            if($checkboxes[$j]!="")
                {   

                $spotlights=$spotlights."<strong>".$checkboxes[$j]."</strong><br>".$Enhanced[$j]."<br>".$Search[$j]."<br>".$Landing[$j]."<br>".$text[$j]."<br><br>";

                }

            }
       echo $spotlights;

In above code, $Enhanced[$j], $Search[$j], $Landing[$j] may or may not be empty as these are checkboxes. If any of them is empty i don't want to print that. In above case if any of three field empty then it display <br/> which i don't want.

If I check them one by one or combination of them then my code will be longer. How do I check in smarter way? How should I write the line of $spotlights=... so that it will display only non empty variables($Enhanced[$j], $Search[$j], $Landing[$j])?

I have tried bolow which is not working. using functions:

    for ($j = 1; $j <= 20; $j++) {

    if($checkboxes[$j]!="")
        {   

        $spotlights=$spotlights."<strong>".$checkboxes[$j]."</strong>".enhanced($j).search($j).landing($j)."<br>".$text[$j]."<br><br>";

        }


    }
    function enhanced($j) {

    if($Enhanced[$j]!="")
        {   
        return "<br/>".$Enhanced[$j];
        }
}
    function search($j) {

    if($Search[$j]!="")
        {   
        return "<br/>".$Search[$j];
        }
}
    function landing($j) {

    if($Landing[$j]!="")
        {   
        return "<br/>".$Landing[$j];
        }
}

above code not displaying values of $Enhanced[$j], $Search[$j], $Landing[$j]

checking values independently

for ($j = 1; $j <= 20; $j++) {

if($checkboxes[$j]!="")
    {   

    $spotlights .= $spotlights."<strong>".$checkboxes[$j]."</strong>";

    if($Enhanced[$j]!="")
    {   
       $spotlights .= "<br>".$Enhanced[$j];

    }
    if($Search[$j]!="")
    {   
       $spotlights .= "<br>".$Search[$j];

    }
    if($Landing[$j]!="")
    {   
       $spotlights .= "<br>".$Landing[$j];

    }

     $spotlights .= "<br>".$text[$j]."<br><br>";

}

values are getting repeated by using above code.

  • 写回答

2条回答 默认 最新

  • drno94939847 2015-07-31 11:12
    关注

    I don't think checking them individually will be a lot of trouble, as there are only three of them, but if you want to keep it one line, the line where you define $spotlights with this:

        $spotlights=$spotlights."<strong>".$checkboxes[$j]."</strong>"
    .($Enhanced[$j] != "" ? "<br>".$Enhanced[$j] : "")
    .($Search[$j] != "" ? "<br>".$Search[$j] : "")
    .($Landing[$j] != "" ? "<br>".$Landing[$j] : "")
    ."<br>".$text[$j]."<br><br>";
    

    Hope this helped.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog