【问题标题】:Removing blank lines from PDO array从 PDO 数组中删除空行
【发布时间】:2017-09-05 01:05:43
【问题描述】:

我对 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);

任何帮助将不胜感激。

【问题讨论】:

  • ifelse 声明的目的是什么?它们在逻辑上或句法上毫无意义。
  • if 和 else 语句为可用植物生成植物名称和订购数量的数组。有一个月度目录,可用植物在 Admin 中从种植的植物的总数据库中处理。脚本循环显示可用植物的详细信息,客户在表格中输入所需的数量。发送电子邮件前有订单预览显示 4 个类别(尺寸),植物有一种或多种尺寸可供选择,价格不同。并非所有植物在任何给定时间都提供各种尺寸。希望你能关注这个!
  • hohoho else ($plantname1 = ''. $quantity1 = '') ; 这个代码真有味道。
  • 我不认为hohoho很有建设性。请描述气味。

标签: php arrays pdo


【解决方案1】:

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

【讨论】:

  • 我试过 array_filter($message);但它没有过滤掉空白。感谢您提供示例代码。订单的数量默认为 0。我发布的代码工作正常并达到了所需的结果,只是它在数组中为表单中为“0”的每个项目生成一个空行。
  • 您应该尝试array_filter($message);$message= array_map(function($v){return trim($v);}, $message); 或先修剪,然后array_filter.. 或发布数组以尝试它
  • 数组过滤器仍然没有成功。这是在结果页面上收到的数组。第二个结果显示为空行,并且对于数量为 0 的每个项目 1 都会这样做: [plantname1] => Array ( [0]=> Acacia acinacea Ruby Tips [1]=> Acacia adunca [2]=> Acacia amblygona prostrate ) [quantity1] => Array ( [0]=> 1 [1]=> 0 [2]=> 3 )
  • 我在顶部编辑了答案,请尝试,似乎在我的示例中有效。 0 不为空,数字 0 应该以不同的方式处理。
猜你喜欢
  • 2013-01-02
  • 2021-07-20
  • 1970-01-01
  • 2017-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多