duanmu3049
2015-07-31 10:55 阅读 49
已采纳

检查并在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 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.

    点赞 评论 复制链接分享
  • duanqinjiao5244 duanqinjiao5244 2015-07-31 11:18

    Use array_filter.
    Something like that:

    $checkboxes = array_filter($checkboxes);
        foreach($checkboxes as $checkbox)
        {
            $j = array_search($checkbox, $checkboxes)
                $spotlights=$spotlights."<strong>".$checkboxes[$j]."</strong><br>".$Enhanced[$j]."<br>".$Search[$j]."<br>".$Landing[$j]."<br>".$text[$j]."<br><br>";
            echo $spotlights;
        }
    

    array_filter will remove all NULL, false or '' from array, but will keep the position, so if you have in checkbox[3] something different, in checkboxes after array_filter will be on position 3. doesn't matter if on position 2 was NULL.

    点赞 评论 复制链接分享

相关推荐