【问题标题】:Filter post variables and print a resume for each one combination过滤帖子变量并为每个组合打印一份简历
【发布时间】:2012-06-15 05:06:21
【问题描述】:

我喜欢下面:

<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>

我在 ajax (jquery.post) 中提交此表单。 在服务器端 PHP 接收数据,我需要打印提交表单的简历。 例如 是用户输入(对于行):

ART1 - nabuk - 尼禄 - gomma - 35 - 1

ART1 - nabuk - 尼禄 - gomma - 39 - 5

ART1 - nabuk - 尼禄 - gomma - 40 - 9

ART1 - nabuk - 尼禄 - gomma - 45 - 1

ART1 - nabuk - rosso - gomma - 38 - 1

ART1 - nabuk - rosso - gomma - 38 - 1

ART1 - nabuk - 尼禄 - gomma - 38 - 1

ART1 - vitello - 蓝光 - gomma - 38 - 1

ART1 - vitello - blu - gomma - 40 - 1

ART561 - nabuk - 尼禄 - gomma - 40 - 1

简历将被前 4 个字段过滤,例如:

ART1 - nabuk  - nero - gomma

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

另一份简历将是:

ART1 - vitello - blu - gomma 

                                                            38->1
                                                            40->1

so 4 个字段是过滤条件,另外一个是每个过滤规则的公共数据。 该表使用 javascript 动态扩展/降低高度,我的问题是 如何过滤发送到 PHP 的帖子数据的示例。 然后将它们写入文件,不是问题......

简历将打印在 PDF 文件上(这不是问题)。 我已经尝试过foreach cicle,但我不知道如何选择标准......我认为这非常困难。 (程序没有数据库)

任何人都可以帮助我吗? 非常感谢

【问题讨论】:

    标签: php arrays forms post filter


    【解决方案1】:

    我想我明白你想要做什么。以正确的方式将所有帖子数据添加到数组中,然后使用 array_multisort 应该负责您的过滤,以升序方式对简历进行排序。

    我认为以下应该可以得到你的结果。 请注意,您确实应该在表单标签 (action="mypage.php") 中添加一个操作,并且可能还有 method="post"。 祝你好运。见下文。我希望这会有所帮助。

    为了便于使用,我假设您也希望在表格中输出,但这很容易更改为您的 PDF 或其他内容的字符串。

    //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>';
    }
    ?>
    

    【讨论】:

    • 是的,它有效,我会大胆尝试,但明天我会检查得更好。我不明白 "isset($_POST['pellame'][$key]{0}" 做什么...检查变量是否已设置?!?!
    • 是的,检查一下是否已设置且不为空。
    • mmmm... 我不明白 "$_POST['pellame'][$key]{0}"... 正是 [key]{0} 它是做什么的!??谢谢
    • 你也可以使用 (isset($_POST['pellame'][$key]) && $_POST['pellame'][$key]!=''),这是比较常用的方法公平地说。
    猜你喜欢
    • 2017-09-06
    • 2014-08-25
    • 2021-12-19
    • 2021-07-10
    • 1970-01-01
    • 2021-09-29
    • 2021-11-29
    • 2020-05-17
    • 1970-01-01
    相关资源
    最近更新 更多