Filter post variables and print a resume for each one combination

I've like below:

<form name="crea">
        <table class="table table-striped table-bordered table-condensed">
            <thead>
                <tr>
                    <th>Articolo</th>
                    <th>Pellame</th>
                    <th>Colore</th>
                    <th>Fondo</th>
                    <th>Taglia</th>
                    <th>Quantit&agrave;</th>
                    <th>+/-</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <input type="text" name="articolo[]" class="input-small" />
                    </td>
                    <td>
                        <select name="pellame[]" class="input-medium">
                            <option value="nabuk">Nabuk</option>
                            <option value="vitello">Vitello</option>
                            <option value="capretto">Capretto</option>
                        </select>
                    </td>
                    <td>
                        <select name="colore[]" class="input-medium">
                            <option value="nero">Nero</option>
                            <option value="blu">Blu</option>
                            <option value="rosso">Rosso</option>
                            <option value="bianco">Bianco</option>
                            <option value="argento">Argento</option>
                            <option value="platino">Platino</option>
                            <option value="bronzo">Bronzo</option>
                            <option value="tortora">Tortora</option>
                        </select>
                     </td>
                    <td>
                        <select name="fondo[]" class="input-medium">
                            <option value="gomma">Gomma</option>
                            <option value="cuoio">Cuoio</option>
                            <option value="sughero">Sughero</option>
                        </select>
                    </td>
                    <td>
                        <select name="taglia[]" class="input-mini">
                            <option value="35">35</option>
                            <option value="36">36</option>
                            <option value="37">37</option>
                            <option value="38">38</option>
                            <option value="39">39</option>
                            <option value="40">40</option>
                            <option value="41">41</option>
                            <option value="42">42</option>
                            <option value="43">43</option>
                            <option value="44">44</option>
                            <option value="45">45</option>
                            <option value="46">46</option>
                            <option value="47">47</option>
                        </select>
                    </td>
                    <td>
                        <input type="number" min="1" max="200" name="qnt[]" step="1" value="1" class="input-mini"/>
                    </td>
                    <td></td>
                </tr>
            </tbody>

        </table>
                <input type="submit" class="btn btn-primary" name="genera" value="Genera" />

    </form>

and I submit this form in ajax (jquery.post). On the server side PHP receives data and I need to print a resume of the submitted form. For example is users inputs (for row):

ART1 - nabuk - nero - gomma - 35 - 1

ART1 - nabuk - nero - gomma - 39 - 5

ART1 - nabuk - nero - gomma - 40 - 9

ART1 - nabuk - nero - gomma - 45 - 1

ART1 - nabuk - rosso - gomma - 38 - 1

ART1 - nabuk - rosso - gomma - 38 - 1

ART1 - nabuk - nero - gomma - 38 - 1

ART1 - vitello - blu - gomma - 38 - 1

ART1 - vitello - blu - gomma - 40 - 1

ART561 - nabuk - nero - gomma - 40 - 1

the resume will be filtered by the first 4 fields, e.g:

ART1 - nabuk  - nero - gomma

                                                            35->1
                                                            39->5
                                                            40->9
                                                            45->1 

another resume will be:

ART1 - vitello - blu - gomma 

                                                            38->1
                                                            40->1

so 4 fields are the filter criterias, and the other are the common data of each filtered rule. the table is dinamically expanded/reduced in height with javascript, and my problem is how to filter like examples the post data sent to PHP. Then writing them into file, is not a problem...

The resumes will be printed on a PDF file (this is not a problem). I've tried with the foreach cicle, but I don't know how to do the selection of criteria... i think It's very difficult. (the program doesn't have database)

any one can help me? thank you so much

Answers


I think I understand what you are trying to do. Adding all the post data in the correct way to an array and then using array_multisort should take care of your filtering, ordering the resumes in ascending fashion.

I think the following should work to get your result. Note you should really add an action to your form tag (action="mypage.php") and probably method="post" too. Good luck. See below. I hope this helps.

For ease of use I have assumed you want your output in a table as well, but this is easily changed to a string for your PDF or whatever.

//if all post variables are at least set...
if(isset($_POST['articolo']) && $_POST['articolo']!=''
    && isset($_POST['pellame']) && $_POST['pellame']!=''
    && isset($_POST['colore']) && $_POST['colore']!=''
    && isset($_POST['fondo']) && $_POST['fondo']!=''
    && isset($_POST['taglia']) && $_POST['taglia']!=''
    && isset($_POST['qnt']) && $_POST['qnt']!=''){

//resumes array
$resumes = array();

//Loop through each post row (based on the articolo), if all the same keys are set for the required post we have
// a valid resume.
foreach($_POST['articolo'] as $key=>$value){
    if(isset($_POST['articolo'][$key]) && $_POST['articolo'][$key]!=''
        && isset($_POST['pellame'][$key]) && $_POST['pellame'][$key]!=''
        && isset($_POST['colore'][$key]) && $_POST['colore'][$key]!=''
        && isset($_POST['fondo'][$key]) && $_POST['fondo'][$key]!=''
        && isset($_POST['taglia'][$key]) && $_POST['taglia'][$key]!=''
        && isset($_POST['qnt'][$key]) && $_POST['qnt'][$key]!=''){
        $resumes[] = array('articolo'=>$_POST['articolo'][$key],
            'pellame'=>$_POST['pellame'][$key],
            'colore'=>$_POST['colore'][$key],
            'fondo'=>$_POST['fondo'][$key],
            'taglia'=>$_POST['taglia'][$key],
            'qnt'=>$_POST['qnt'][$key]);
    }
}

//Multi sort our resumes array ASCENDING for each key.
array_multisort($resumes);

//set up our "current" article to start with, the details are blank so we print the first details always.
$current['articolo'] = '';
$current['pellame'] = '';
$current['colore'] = '';
$current['fondo'] = '';

//start output
echo '<table>';

//loop through the resumes
foreach($resumes as $thisResume){

    //Print this row
    echo '<tr><td>';
    //If this resume is of a different type (any of the keys/filters) then print the info, otherwise it's the
    // same as above so don't bother.
    if($thisResume['articolo']!=$current['articolo']
            || $thisResume['pellame']!=$current['pellame']
            || $thisResume['colore']!=$current['colore']
            || $thisResume['fondo']!=$current['fondo']){
        echo htmlentities($thisResume['articolo']
                .' - '.$thisResume['pellame']
                .' - '.$thisResume['colore']
                .' - '.$thisResume['fondo']);

    }
    //Echo the resume specific quantity info.
    echo '</td><td>'.htmlentities($thisResume['taglia']
                .' -> '.$thisResume['qnt']).'</td></tr>';

    //Update our current array info which stores the resume attributes for comparison of next resume.
    $current['articolo'] = $thisResume['articolo'];
    $current['pellame'] = $thisResume['pellame'];
    $current['colore'] = $thisResume['colore'];
    $current['fondo'] = $thisResume['fondo'];
}
//end output
echo '</table>';
}
?>

Need Your Help

Javascript: Is This Truly Signed Integer Division

javascript integer ecmascript-6 integer-division

Given the following code, where both a and b are Numbers representing values within the range of signed 32-bit signed integers:

parse xml to json in php while having subnodes next to a textNode

php xml json simplexml

I need to parse some xml from an external XML-API to JSON, to do so, I used this very nice little library from IBM which works very well so far. Unfortunately I discovered some text nodes have subn...