0

I'm very new to PDO/PHP and I'm converting a very old VBScript order system to PDO/MYSQL. All is going well until now.

I have a simple order form for products and quantities, no calculations or payments. It works fine except that all of the products that are not selected are shown as blank lines on the next HTML page, which is to preview the order.

I've tried a number of ways to fix the problem, but no success yet. This is the code:

$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);

Any help would be much appreciated.

FreddoB
  • 11
  • 3
  • 1
    What is the purpose of those `if` and `else` statements? They make no sense logically or syntactically. – Enstage Sep 05 '17 at 01:09
  • The if and else statements produce arrays of the plantname and quantity ordered for the available plants. There is a monthly catalog and the available plants are processed in Admin from the total database of plants grown. The script loops through and displays details of the available plants and the customer enters the quantity required in the form. There is an order preview before emailing There are 4 categories(sizes) displayed, with plants being available in one or more sizes with differing prices . Not all plants are available in all sizes at any given time. Hope you can follow this! – FreddoB Sep 05 '17 at 08:18
  • hohoho `else ($plantname1 = ''. $quantity1 = '') ;` This code really smell. – degr Sep 05 '17 at 10:44
  • I don't think hohoho is very constructive. Please describe the smell. – FreddoB Sep 05 '17 at 23:40

1 Answers1

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

removing empty values is just array_filter($message);
or actually remove the else ($plantname1 = ''. $quantity1 = '') ; line from the code


maybe something like this:

<?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

Output is

Your order is: 
Ageratum houstonianum (11)
Aconitum (8)
African Daisy (7)
Only 3 of Allium roseum is available
No Alchemilla is available
Leo Tahk
  • 420
  • 1
  • 6
  • 15
  • I tried array_filter($message); but it didn't filter out the blanks. Thanks for the sample code.The order form has a default 0 in Quantity. The code I posted works fine and achieves the required result except that it produces a blank line in the array for every item that is "0" in the form. – FreddoB Sep 05 '17 at 23:44
  • you should try `array_filter($message);` or `$message= array_map(function($v){return trim($v);}, $message);` or first trim, then array_filter.. or post the array to try it – Leo Tahk Sep 05 '17 at 23:47
  • Still no success with the array filter. This is the arrays received on the results page. The second result shows as a blank line and will do so for every item that has a 0 in quantity1: [plantname1] => Array ( [0]=> Acacia acinacea Ruby Tips [1]=> Acacia adunca [2]=> Acacia amblygona prostrate ) [quantity1] => Array ( [0]=> 1 [1]=> 0 [2]=> 3 ) – FreddoB Sep 06 '17 at 01:23
  • i edited the answer on the top, please try, seemed to work in my example. 0 is not empty, the number 0 should be dealt in a different way. – Leo Tahk Sep 06 '17 at 01:39
  • [to remove zeroes](https://stackoverflow.com/questions/2287404/remove-zero-values-from-a-php-array) – Leo Tahk Sep 06 '17 at 01:46
  • Leo, thank you so much for the link. This worked perfectly. foreach($message as $array_key=>$array_item) { if($message[$array_key] == 0) { unset($message[$array_key]); } } – FreddoB Sep 06 '17 at 07:35