Muiter
Muiter

Reputation: 1520

Working with an multidimensional array and use them in a new structure

I have this form that is populated by a CSV input and the user needs to assign an header to the columns he would like to use. The user can also disable the rows he does not want to process.

This is my CSV

Klant;Klantref;Posnr.;Aantal;Dikte;Kwaliteit
Client;;60-017-301;8;10;S355
Client;;60-117-301;4;10;S355
Client;;60-031-302;2;20;S355
Client;;60-131-401;4;15;S355

This is the form:

$check_upload = glob('uploads/temp_dropzone/'.$_SESSION['user_id'].'_'.$upload_key.'_*.*');

echo'
<table class="table">
    <thead>';

    //head
    foreach($check_upload as $val)
    {
        $file = fopen($val,'r');

        $data_1 = fgetcsv($file);

        echo'<tr>';

        foreach ($data_1 as $index => $val_1)
        {
            $data_2 = explode(';', $val_1);

                echo '<th><input type="text" class="form-control no-border input-sm" id="calc_import['.$y.'][]" name="calc_import[]" value="Import: ja - nee" style="text-align: center" readonly="readonly"></th>';

            for($y = 1; $y <= count($data_2); $y++)
            {
                echo '<th>
                      <select class="form-control no-border input-sm" id="calc_header['.$y.'][]" name="calc_header[]">
                        <option value="">Kies type waarde</option>
                        <option value="aantal">Aantal</option>
                        <option value="omschr_1">Omschrijving 1</option>
                        <option value="omschr_2">Omschrijving 2</option>
                        <option value="omschr_3">Omschrijving 3</option>
                        <option value="prijs">Prijs</option>
                        <option value="opmerking">Opmerking</option>
                      </select>
                  </th>';
            }
        }
        echo'</tr>';

        fclose($file);
    }
    echo'
    </thead>
    <tbody>';

    // body
    foreach($check_upload as $val)
    {
        $file = fopen($val,'r');

        while($data_1 = fgetcsv($file))
        {
            $z++;

            echo'<tr>';

            foreach ($data_1 as $index => $val_1)
            {
                $data_2 = explode(';', $val_1);

                echo '<td align="center"><input type="radio" id="calc_import['.$z.'][]" name="calc_import['.$z.'][]" value="ja" checked> <input type="radio" id="calc_import['.$z.'][]" name="calc_import['.$z.'][]" value="nee"></td>';

                foreach ($data_2 as $index => $val_2)
                {
                    echo '<td><input type="text" class="form-control no-border input-sm" id="calc_body['.$z.'][]" name="calc_body['.$z.'][]" value="'.$val_2.'" readonly="readonly"></td>';
                }
            }

            echo '</tr>';
        }

        fclose($file);      
        unlink($val);
    }

    echo'
  </tbody>
</table>

The form is filled like this:

So far so good :)

When I send the form with $_POST and print_r($_POST); This is the create array

Array ( [dossier_id] => 9111 [calc_import] => Array ( [0] => Import: ja - nee [1] => Array ( [0] => nee ) [2] => Array ( [0] => ja ) [3] => Array ( [0] => ja ) [4] => Array ( [0] => ja ) [5] => Array ( [0] => ja ) ) [calc_header] => Array ( [0] => [1] => [2] => omschr_1 [3] => aantal [4] => omschr_2 [5] => omschr_3 ) [calc_body] => Array ( [1] => Array ( [0] => Klant [1] => Klantreferentie [2] => Posnr. [3] => Aantal [4] => Dikte [5] => Kwaliteit ) [2] => Array ( [0] => Client [1] => [2] => 60-017-301 [3] => 8 [4] => 10 [5] => S355 ) [3] => Array ( [0] => Client [1] => [2] => 60-117-301 [3] => 4 [4] => 10 [5] => S355 ) [4] => Array ( [0] => Client [1] => [2] => 60-031-302 [3] => 2 [4] => 20 [5] => S355 ) [5] => Array ( [0] => Client [1] => [2] => 60-131-401 [3] => 4 [4] => 15 [5] => S355 ) ) ) 

Now need to create an table with where I have an column with 'Aantal', Omschrijving etc. So I started to find the keys of them in the array with:

echo 'Aantal: '.array_search('aantal', $_POST['calc_header']);
echo '<br>';
echo 'Omschr_1: '.array_search('omschr_1', $_POST['calc_header']);
echo '<br>';
echo 'Omschr_2: '.array_search('omschr_2', $_POST['calc_header']);
echo '<br>';
echo 'Omschr_3: '.array_search('omschr_3', $_POST['calc_header']);
echo '<br>';
echo 'Prijs: '.array_search('prijs', $_POST['calc_header']);
echo '<br>';
echo 'Opmerking: '.array_search('opmerking', $_POST['calc_header']);

But what now? I want all variables of the rows when import is ja (yes) to be processed as

But I am getting stuck now, seen to much code, and I am don;t know for sure if the array has the right structure.

Please help, is the array structure right and how to go from here?

Upvotes: 0

Views: 47

Answers (2)

Muiter
Muiter

Reputation: 1520

I have changed te input script to

<table class="table">
    <thead>';

    //head
    foreach($check_upload as $val)
    {
        $file = fopen($val,'r');

        $data_1 = fgetcsv($file);

        echo'<tr>';

        foreach ($data_1 as $index => $val_1)
        {
            $data_2 = explode(';', $val_1);

                echo '<th><input type="text" class="form-control no-border input-sm" value="Import: ja - nee" style="text-align: center" readonly="readonly"></th>';

            for($y = 1; $y <= count($data_2); $y++)
            {
                echo '<th>
                      <select class="form-control no-border input-sm" id="calc_header['.$y.']['.$y.']" name="calc_header[]">
                        <option value="none">Kies type waarde</option>
                        <option value="aantal">Aantal</option>
                        <option value="omschr_1">Omschrijving 1</option>
                        <option value="omschr_2">Omschrijving 2</option>
                        <option value="omschr_3">Omschrijving 3</option>
                        <option value="prijs">Prijs</option>
                        <option value="opmerking">Opmerking</option>
                      </select>
                  </th>';
            }
        }
        echo'</tr>';

        fclose($file);
    }
    echo'
    </thead>
    <tbody>';

    // body
    foreach($check_upload as $val)
    {
        $file = fopen($val,'r');

        while($data_1 = fgetcsv($file))
        {
            echo'<tr>';

            foreach ($data_1 as $key_1 => $val_1)
            {       
                $z++;

                echo '<td align="center"><input type="radio" id="calc_import['.$z.']" name="calc_import['.$z.']" value="ja" checked> <input type="radio" id="calc_import['.$z.']" name="calc_import['.$z.']" value="nee"></td>';

                $data_2 = explode(';', $val_1);

                foreach ($data_2 as $key_2 => $val_2)
                {
                    echo '<td><input type="text" class="form-control no-border input-sm" id="calc_body['.$key_2.']['.$z.']" name="calc_body['.$key_2.']['.$z.']" value="'.$val_2.'" readonly="readonly"></td>';
                }
            }

            echo '</tr>';
        }

        fclose($file);      
        unlink($val);
    }

    echo'
  </tbody>
</table>

Now I was able to:

$key_aantal = array_search('aantal', $_POST['calc_header']);
$key_omschr_1 = array_search('omschr_1', $_POST['calc_header']);
$key_omschr_2 = array_search('omschr_2', $_POST['calc_header']);
$key_omschr_3 = array_search('omschr_3', $_POST['calc_header']);
$key_prijs = array_search('prijs', $_POST['calc_header']);
$key_opmerking = array_search('opmerking', $_POST['calc_header']);

foreach($_POST['calc_import'] as $key => $value)
{
    if($value == 'ja')
    {
        if($key_aantal > 0) { $aantal = $_POST['calc_body'][$key_aantal][$key]; }
        if($key_aantal > 0) { $omschr_1 = $_POST['calc_body'][$key_omschr_1][$key]; }
        if($key_aantal > 0) { $omschr_2 = $_POST['calc_body'][$key_omschr_2][$key]; }
        if($key_aantal > 0) { $omschr_3 = $_POST['calc_body'][$key_omschr_3][$key]; }
        if($key_aantal > 0) { $prijs = $_POST['calc_body'][$key_prijs][$key]; }
        if($key_opmerking > 0) { $opmerking = $_POST['calc_body'][$key_opmerking][$key]; }
    }
}

Upvotes: 0

Nigel Ren
Nigel Ren

Reputation: 57121

This code should help, I've included comments in the code to try and indicate what each item does...

$_POST["calc_import"] = Array ( 0 => "Import: ja - nee",
         1 => Array ( "0" => "nee" ),
         2 => Array ( "0" => "ja" ),
         3 => Array ( "0" => "ja" ),
         4 => Array ( "0" => "ja" ),
         5 => Array ( "0" => "ja" ) );
$_POST["calc_header"] = Array ( 0 => "",
        1 => "",
        2 => "omschr_1",
        3 => "aantal",
        4 => "omschr_2",
        5 => "omschr_3" );

// This is where the code decides the positions of each column 
// (you already do this - just store this for the processing)
$columnPositions = [3, 2, 4, 5, "", ""]; 
$fileName = "a.txt";
$import = fopen($fileName, "r");
// rowNumber is the match to check if this row is output
$rowNumber = 1;
$output = [];
while ( $rowData = fgetcsv($import, null, ";")) {
    // Is this row to be exported?
    if ( $_POST["calc_import"][$rowNumber][0] == "ja" )    {
        $newOut = [];
        // Build up the output from the column positions
        foreach ( $columnPositions as $extractColumn )  {
            // Only output columns which have a position (i.e. non blank)
            if ( $extractColumn )  {
                $newOut[] = $rowData[$extractColumn];
            }
        }
        $output[] = $newOut;
    }
    // Increment row number
    $rowNumber++;
}
fclose($import);     
print_r($output);

The basis is to take the column ordering you already have and turn it into a pick list of the data columns to extract. Each time you read a row from the file, first check if it is to be processed and then for each row re-order the data (throwing away unwanted data) and accumulate an output of the combined data.

Upvotes: 1

Related Questions