如果使用串行逗号/ array_pop时未满足条件

时间:2014-04-16 21:36:46

标签: php arrays forms postback

新手在这里处理一个简单的回发表格(实际上我很复杂)。我的目标是,如果只选择了一个项目,则单词'和'不打印。当选择三个或更多内容时,该文件很有效,因为它删除了最后一个逗号,但如果它只是最后一个元素,那么我想放弃'和...'这样只打印最后一个元素。

我正在尝试制作一个if else if else声明,但我没有得到它。 感谢您的帮助 - 我只是想让我的小实验更好一点。

<?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>
        ';
    }
?>

1 个答案:

答案 0 :(得分:1)

我想你是说如果只有2个元素你不想要和?如果是这样的话:

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;