Why are my brackets turning into part of my print statement?

时间:2019-03-06 11:34:28

标签: php html

Trying to display the results of my post array. For line

print "<tr><td>$prodqty</td>......" 

I need my {$gear{prodnum}[1]} to have that last bracket close out the array element. But for some reason the final closing bracket becomes part of my print statement (for anyone familiar with NetBeans, that bracket turns orange instead of black like it's supposed to be) and since I have a few if/else statements, the closing bracket gets moved to further down the lines (particularly after a ; which I don't understand) and I can't figure out how to get that array element to close.

I have tried both {$gear{$prodnum}[1]} and {$gear{$prodnum[1]}} and either way it will not work. I also tried () instead of brackets for $prodnum.

My professor was the one who helped me write that section and it was working until I tried to add more if statements and then it stopped.

if(array_key_exists('submit',$_POST))
{
    echo "<table><table align='center'><th colspan='3'>Total</th><tr>
<td>Quantity</td><td>Item</td><td>Extended Price</td></tr>";

    foreach($_POST['qty'] as $prodnum=>$prodqty){

        if($prodqty>0){

            print "<tr><td> $prodqty </td> <td>{$gear{$prodnum}[1]**}**</td>";
            print "<td>".($prodqty*$gear{$prodnum}[3])."</td></tr>";
        }
    };

elseif($prodqty==null)
{;
}

else {
    echo "Please enter a quantity.";
}
};

Ideally I would like the prodqty to go after the prodnum, but it would not work that way. Also any suggestions on how to print the results of the $POST that have quantities added, and forgetting those that do not, will be greatly appreciated. I'm trying to write the if statements to require the submitter to enter a number (no letters or words) and leave the other array elements out if they did not enter a quantity for them. Thank you!

1 个答案:

答案 0 :(得分:1)

您的ifelse位置似乎未对齐。试图以最佳方式重构您的陈述,我认为您正在努力实现

if(array_key_exists('submit',$_POST))
{
    echo "<table><table align='center'><th colspan='3'>Total</th><tr>
    <td>Quantity</td><td>Item</td><td>Extended Price</td></tr>";

    foreach($_POST['qty'] as $prodnum=>$prodqty){

       if($prodqty>0){

          print "<tr><td> $prodqty </td> <td>".$gear[$prodnum][1]."</td>";
                print "<td>".($prodqty*$gear[$prodnum][3])."</td></tr>";

       } elseif($prodqty==null)
       {
       }
    };
} else {
    echo "Please enter a quantity.";
}