Skip to content
Advertisement

Is there a better way to handle array data from POST than what I’m doing?

First off let me say, I’m sure there is a better way, I’m not a coder by profession, just a nerd trying to solve a small personal business problem with an app built entirely with html and php.

I’m not sure if I’ve done this as efficiently as possible and would like to know if there is a better way to do this. My app is strictly HTML and PHP.

In an HTML form, I’m building a list of items based on a search. This list is dynamic, in that it could be any given number of items based on the search. The end result is that any of the line items that have a sales price entered by the user will end up being added to an order. Those left blank will be left alone. There are three variablesvalues for each line that need to be sent to POST. The item id, the item description and the sales price of the selected items.

Here is the php and html setting up the form (go easy on me, I’m not a developer)

echo "<form action='actionAddItemToOrder.php' method='post'>";
    while($row = mysqli_fetch_assoc($result)){
    $itm = $row["lotitem_description"];
    $itmid = $row ["lotitem_id"];
    $itmcost = $row['lotitem_cost'];
    echo "<p style='margin-left:50;'><b>";
        echo "<input type='hidden' name='itmid[]' value='$itmid'/>Item # ".$itmid." - ".$itm."(cost$".$itmcost.")";
        echo ":  $<input type='text' name='sellprc[]' size='5'>";
        echo "<input type='hidden' name='itm[]' value='$itm'>";
        }

       echo "<p class='form-button-center'><input type='submit' value='Add item'></p>";
       echo "</form>";}

Right now, when the user clicks the submit button, three arrays are submitted to POST, one for each value set that will be needed in order to add the item to the order. Below is what those arrays look like from POST

Array
(
    [itmid] => Array
        (
            [0] => 1825
            [1] => 1824
            [2] => 1823
            [3] => 1822
            [4] => 1821
            [5] => 1820
            [6] => 1819
            [7] => 1818
        )

    [sellprc] => Array
        (
            [0] => 15
            [1] => 
            [2] => 
            [3] => 
            [4] => 17
            [5] => 
            [6] => 
            [7] => 21
        )

    [itm] => Array
        (
            [0] => Widget A
            [1] => Widget B
            [2] => Widget C
            [3] => Widget D
            [4] => Widget E
            [5] => Widget F
            [6] => Widget G
            [7] => Widget H
        )

Some assumptions can be made here. Since there are only three keys in the sellprc array with values assigned to them, that means those are the items being added to the order and the values are the sales price. The keys with no values are not being added to this order. The key numbers from the sellprc array can be used to match up the itm and itmid values.

From here, all I’ve been able to figure out is to create yet another set of arrays in order to align the correct item ID and description to the sellprc keys that have values. I’m doing that with this bit of PHP

$prcarray = array();
$itmarray = array();
$itmidarray = array();
foreach ($_POST['sellprc'] as $key2=>$val2) {
    if ($val2) {
        $val2 = preg_replace('/[^a-zA-Z0-9-. ]/s','',$val2);
        array_push ($prcarray,$val2);
        array_push ($itmarray,$_POST['itm'][$key2]);
        array_push ($itmidarray,$_POST['itmid'][$key2]);
        }
    }

I end up with these arrays

Array
(
    [0] => 15
    [1] => 17
    [2] => 21
)

Array
(
    [0] => Widget A
    [1] => Widget E
    [2] => Widget H
)

Array
(
    [0] => 1825
    [1] => 1821
    [2] => 1818
)

Then based on these three arrays I set the item id, description and sales price of each item with this bit of php

foreach($itmidarray as $key=>$val) {
    $itmid = $val;
    $sellprc = $prcarray[$key];
    $itm = $itmarray[$key];

        <do some sql inserts and updates based on these values then go to the next set>

}


This is working, but I feel like this is just really inefficient and am wondering if I can do this better.

Thanks!

Advertisement

Answer

First of all, put things together if they belong/relate to each other. So, you do not need to define 3 separate arrays in your form, of which elements you have to match after submitting ´the form.

Another thing is how to compose HTML content. If you do not use template engines like Smarty, you can still use variables to build your HTML content first. Then at the very end you can print it. Here is edited version of your HTML form:

BUILD HTML FORM

$myform = "<form action='actionAddItemToOrder.php' method='post'>";
$counter = 0;
while($row = mysqli_fetch_assoc($result))
{
    $id = $row["lotitem_id"];
    $description = $row["lotitem_description"];
    $cost = $row['lotitem_cost'];

    $myform .= "<p style='margin-left:50;'><b>
                    <input type='hidden' name='items[$counter][id]' value='$id'/>
                    <input type='hidden' name='items[$counter][description]' value='$description'/>
                    Item #$id - $description (cost ${$cost}): $<input type='text' name='items[$counter][sellPrice]' size='5'>
                </p>";
    $counter++;
}

$myform .= "<p class='form-button-center'>
                <input type='submit' value='Add item'>
            </p>
        </form>";

echo $myform;

As you see, I use $myform to build and store the HTML form first and when all rows added to it, it is printed. This method may be not the most proper way if you have many thousand items.

POST AND ADD ITEMS TO THE ORDER

After submitting the form, you can add the items with sell price to the order like this (I assume you have a function addItemToOrder(), which expects 3 parameters):

foreach($_POST['items'] as $item)
{
    $sellPrice = trim($item['sellPrice']);
    if(!empty($sellPrice) and is_numeric($sellPrice))
    {
        // add this item to the order...
        addItemToOrder($item['id'], $item['description'], $item['sellPrice']);
    }
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement