从PDO阵列中删除空行

时间:2017-09-05 01:05:43

标签: php arrays pdo

我是PDO / PHP的新手,我将一个非常旧的VBScript订单系统转换为PDO / MYSQL。到目前为止,一切进展顺利。

我有一个简单的产品和数量订单,没有计算或付款。它工作正常,但未选中的所有产品在下一个HTML页面上显示为空行,即预览订单。

我已经尝试了多种方法来解决问题,但还没有成功。这是代码:

$quantity1 = $_POST['quantity1'];
foreach($_POST['plantname1'] as $key => $plantname1)
{
$quantity1 = isset($_POST['quantity1'][$key])? $_POST['quantity1'] [$key]:'Not selected';
if($quantity1 != 0);    
else ($plantname1 = ''. $quantity1 = '') ;
$message[] = $quantity1.'  '. $plantname1  ;}
echo implode('<br>', $message);
unset($message);

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

eidt

Remove zero values from a PHP array

php remove "empty" values in array

$message= array_filter($message, function($v){return trim($v);}); 
$message= array_slice($message, 0 , count($message));

删除空值只是array_filter($message);
或者实际从代码中删除else ($plantname1 = ''. $quantity1 = '') ;

可能是这样的:

<?php
//buyer
$user_choice['Ageratum houstonianum']   = 11;
$user_choice['Aconitum']                = 8;
$user_choice['African Daisy']           = 7;
$user_choice['Allium roseum']           = 11;
$user_choice['Alchemilla']              = 1;
//==============================================
//the flower shop
$flower['Aconitum']                 = 11;
$flower['African Daisy']            = 7;
$flower['Agapanthus']               = 5;
$flower['Ageratum houstonianum']    = 21;
$flower['Alchemilla']               = 0;
$flower['Allium roseum']            = 3;
//post request
if(isset($_REQUEST['user_choice'])) $user_choice = $_REQUEST['user_choice'];
//message of choice
if(isset($user_choice))
{
    $message[] = 'Your order is: ';
    foreach($user_choice as  $key => $quantity)
    {
        if($quantity > $flower[$key])
        {//check if available
            if($flower[$key] > 0)
            {
                $message[] = 'Only '.$flower[$key].' of '.$key.' is available';
            }else
            {
                $message[] = 'No '.$key.' is available';
            };//end if
        }else
        {//show the choice
            $message[] = $key.' ('.$quantity.')';
        };//end if
    };//end foreach
    echo implode('<br>', $message);
};//end if

输出

Your order is: 
Ageratum houstonianum (11)
Aconitum (8)
African Daisy (7)
Only 3 of Allium roseum is available
No Alchemilla is available