Hi all,
I am trying to compare data from $_POST['Net'] which has 6 elements to check to see if the data has changed in the form when submitted.
Below is the Post array from form.

array(16) { ["Submitted"]=> string(4) "true" ["NetValue"]=> string(1) "0" ["ProductID"]=> array(6) { [0]=> string(5) "76458" [1]=> string(5) "76459" [2]=> string(5) "76460" [3]=> string(5) "76461" [4]=> string(5) "76462" [5]=> string(5) "76463" } ["Quantity"]=> array(6) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "2" [3]=> string(1) "1" [4]=> string(1) "1" [5]=> string(1) "1" } ["Nominal"]=> array(6) { [0]=> string(4) "5001" [1]=> string(4) "5001" [2]=> string(4) "5001" [3]=> string(4) "5001" [4]=> string(4) "5001" [5]=> string(4) "5001" } ["Department"]=> array(6) { [0]=> string(1) "4" [1]=> string(1) "4" [2]=> string(1) "4" [3]=> string(1) "4" [4]=> string(1) "4" [5]=> string(1) "4" } ["Unit"]=> array(6) { [0]=> string(5) "25.00" [1]=> string(5) "25.00" [2]=> string(5) "25.00" [3]=> string(5) "80.00" [4]=> string(6) "205.00" [5]=> string(6) "120.00" } ["Discount"]=> array(6) { [0]=> string(4) "0.00" [1]=> string(4) "0.00" [2]=> string(4) "0.00" [3]=> string(4) "0.00" [4]=> string(4) "0.00" [5]=> string(4) "0.00" } ["Net"]=> array(6) { [0]=> string(5) "25.00" [1]=> string(5) "50.00" [2]=> string(5) "50.00" [3]=> string(5) "80.00" [4]=> string(6) "205.00" [5]=> string(6) "120.00" } ["VATCode"]=> array(6) { [0]=> string(2) "T3" [1]=> string(2) "T3" [2]=> string(2) "T3" [3]=> string(2) "T3" [4]=> string(2) "T3" [5]=> string(2) "T3" } ["VAT"]=> array(6) { [0]=> string(4) "5.00" [1]=> string(5) "10.00" [2]=> string(5) "10.00" [3]=> string(5) "16.00" [4]=> string(5) "41.00" [5]=> string(5) "24.00" } ["ProductCount"]=> string(1) "6" ["NetTotal"]=> string(6) "530.00" ["VATTotal"]=> string(3) "106" ["OrderTotal"]=> string(6) "636.00" ["CheckInButton"]=> string(14) "Check In Items" } 

This is the output from SQL
`25.00
50.00
50.00
80.00
205.00
120.00

`
This is my PHP query

$productSQL = $Database->Execute("SELECT * FROM POProducts inner join PODepartments ON PODepartments.PODepartmentID = POProducts.PODepartmentID  inner join PONominalCodes ON PONominalCodes.PONominalCodeNumber = POProducts.POProductNominalCode   WHERE POID = " . $POID . " ORDER BY POProductID asc");

    while ($rowProduct = mysql_fetch_array($productSQL))
    {
        echo "<br>" . $rowProduct['POProductNet'];
        echo $PostProducts[] = $_POST['Net'];

    }

How can i best use this code or other suggestion

Thanks In advance

D

Edited 1 Week Ago by davidjennings

Hi,

you could use array_diff_assoc():

<?php

$sql  = [25.00, 25.00, 50.00, 82.00, 120.00, 205.00];
$post = [25.00, 50.00, 50.00, 80.00, 120.00, 205.00];

print_r(array_diff_assoc($post, $sql));

Returns:

Array
(
    [1] => 50
    [3] => 80
)

So you can manage the array through the index key of the array. But what is the goal of this compare?

Hi Cereal,
Thanks for your reply
The aim of the compare is the user makes a change to the existing data via a form. The data is passed using $_post and I need to know what value has been changed so that the new value, is recorded in another table with additional data.

Would you still use a while loop to process the date or could you suggest an alternative method.

Thanks

David

Hi Cereal,

I have produced the below code. Looks a bit long winded Does anyone know of a cleaner solution.

while ($rowProduct = mysql_fetch_array($productSQL))
    {
        echo $product[] = $rowProduct['POProductNet'];
    }

    if(is_array(@$_POST['Net']))
    {
        $files=array();
        foreach($_POST['Net'] as $file)
        {
            echo $files[] = basename($file);
        }
    }

    for ($count = 0; $count <= $productCount -1; ++$count){
        if ($files[$count] !== $product[$count]){
                // write data to db
        }

Thanks in advance

David

I was supposing the $_POST['Net'] arrays to hold float values, not file names. Anyway you could write:

$files    = [];
$products = [];

while($rowProduct = mysql_fetch_array($productSQL))
{
    $products[] = $rowProduct['POProductNet'];
}

if(TRUE === isset($_POST['Net']) && TRUE === is_array($_POST['Net']))
{
    $files = array_map('basename', $_POST['Net']);
    $diff  = array_diff_assoc($products, $files);

    if(count($diff) > 0)
    {
        // write data to db
    }
}

Here I'm just using the array functions, instead of the loops, it's just a choice. You can go with loops.

But if $_POST['Net'] is supposed to always be an array, then I would check it in the sanitizing step, not after the query to the database. So it would look like more:

$net = filter_input(INPUT_POST, 'Net', FILTER_SANITIZE_STRING, FILTER_REQUIRE_ARRAY);

if(FALSE !== $net)
{
    $files    = array_map('basename', $net);
    $products = [];

    // select query the database
    // populate the $products array
    // compare with $files
}

Cereal,
Thanks for yor help on this. It was good to see your alternative approch to this issue, which I have now implemented to my code as it was more structured and concise.
Credit well deserved
Thanks

David

This question has already been answered. Start a new discussion instead.