首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用串行逗号/array_pop时不满足if条件

使用串行逗号/array_pop时不满足if条件
EN

Stack Overflow用户
提问于 2014-04-17 05:36:46
回答 1查看 32关注 0票数 0

新手在做一个简单的回帖表单(实际上对我来说很复杂)。我的目标是,如果只选择了一项,'and‘这个词就不会打印出来。当选择了三个或更多的内容时,文件工作得很好,因为它去掉了最后一个逗号,但如果它只是最后一个元素,那么我想去掉'and...‘。所以只打印最后一个元素。

我试着计算出一个if else语句,但是我没有得到它。谢谢你的帮助--我只是想把我的小实验做得更好一点。

代码语言:javascript
复制
<?php
    define('THIS_PAGE', basename($_SERVER['PHP_SELF']));//shows webroot

    if(isset($_POST['submit3'])){ //proces form - catches the post info when submit clicked

    $arr = $_POST['sundae'];
    $lastElement = array_pop($arr);

    if ($arr == ""){$myReturn = "stuff"; echo $myReturn;}
    else
    {$myReturn = "<span style=\"color:red; font-weight: bold\">You have selected: " . implode(', ',$arr)." and ".$lastElement;
    $myReturn .= "</span> <br />";
    $myReturn .= '<input type="button" value="Reload page" onclick="reloadPage()" >';
    echo $myReturn;}
    }else{// show the form if no posties
        echo
        '
        <form action="myTest.php" method ="post">
            Select a size:<br />
            <input type="checkbox" name="sundae[]" value="One Scoop"> One Scoop<br />
            <input type="checkbox" name="sundae[]" value="Two Scoops"> Two Scoops<br />
            <input type="checkbox" name="sundae[]" value="Three Scoops"> Three Scoops<br />

            <hr ><br />

            Select a flavor:<br />
            <input type="checkbox" name="sundae[]" value="Chocolate">Chocolate<br />
            <input type="checkbox" name="sundae[]" value="Coffee">Coffee<br />
            <input type="checkbox" name="sundae[]" value="Double Fudge">Double Fudge<br />

            <hr ><br />

            Select your Toppings:<br />
            <input type="checkbox" name="sundae[]" value="Applesauce">Applesauce<br />
            <input type="checkbox" name="sundae[]" value="Blueberries">Blueberries<br />


            <input type="checkbox" name="sundae[]" value="Bubblegum">Bubblegum<br />

            <input type="checkbox" name="sundae[]" value="Caramel">Caramel<br />
            <input type="checkbox" name="sundae[]" value="Hot Fudge">Hot Fudge<br />
            <input type="checkbox" name="sundae[]" value="Gummy Bears">Gummy Bears<br />

            Select a Whip:<br />
            <input type="checkbox" name="sundae[]" value="Double Whipped Cream">Double Whipped Cream<br />
            <input type="checkbox" name="sundae[]" value="Marishino Cherry">Vanilla Whipped Cream<br />
            <input type="checkbox" name="sundae[]" value="Whipped Cream">Whipped Cream<br />

            <hr ><br />

            Select a Cherry:<br />
            <input type="checkbox" name="sundae[]" value="Black Cherry">Black Cherry<br />
            <input type="checkbox" name="sundae[]" value="Frozen Lake Cherry">Frozen Lake Cherry<br />
            <input type="checkbox" name="sundae[]" value="Marishino Cherry">Marishino Cherry<br />
            <br />
            <hr ><br />

            Wopuld you like to add one "waffer-thin" mint? (No charge)<br />
            <input type="checkbox" name="sundae[]" value="Wafer-Thin Mint">Yes<br />
            <br />
            <input type="submit" name="submit3" >
        </form>
        ';
    }
?>
EN

回答 1

Stack Overflow用户

发布于 2014-04-17 05:48:25

我想你是在说,如果只有两个元素你不想要and?如果是这样的话:

代码语言:javascript
复制
else
{
  //> 1 because we already popped the last element
  if (count($arr) > 1) $lastElement = " and " . $lastElement;
  $myReturn = "<span style=\"color:red; font-weight: bold\">You have selected: " . implode(', ',$arr).$lastElement;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23120601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档