0

我有以下功能,

<pre>
public function getFree($cp) {
            global $glb_con;

            try {
                $sql = "SELECT p.ID, w.ID, p.fname, p.lname, w.desc FROM weight w 
                INNER JOIN comp t ON w.wcl = t.cmp AND  w.wc = t.cm
                INNER JOIN pers b ON w.ID = b.ID_l 
                INNER JOIN pn p ON b.ID = p.ID 
                AND t.cp=:cp group";  

                $query = $glb_connection->prepare($sql);
                $query->bindValue(':cp',$cp);
                $query->execute();
                $wes = $query->fetchAll();

                // First array Generated from sql1 query 
                    $newCorr = array();
                foreach ($wes as $option) {
                    $wclID = $option['desc'];
                    $nameF = $option['fname'];
                    $nameL = $option['lname'];
                    $id = $option['ID'];
                    $newCorr[$wclID][$id] = $nameL." ".$nameF;
                }

                 $sql2 = "SELECT p.ID, e.enre w.ID, p.fname, p.lname, w.desc FROM weight w 
                INNER JOIN comp t ON w.wcl = t.cmp AND  w.wc = t.cm
                INNER JOIN pers b ON w.ID = b.ID_l 
                INNER JOIN pn p ON b.ID = p.ID 
                INNER JOIN t_ent e ON e.ID = p.ID
                AND t.cp=:cp group";  

                $query = $glb_connection->prepare($sql2);
                $query->bindValue(':ID_cmp',$ID_cmp);
                $query->execute();
                $wes1 = $query->fetchAll();

                //the second array from sql2 query 
                    $newCorrAc = array();
                foreach ($wes1 as $option) {
                    $wc = $option['desc'];
                    $ent = $option['enre'];
                    $nameF = $option['fname'];
                    $nameL = $option['lname'];
                    $id = $option['ID'];
                    $newCorrAc [$wc] [$id] [$ent]= $nameL." ".$nameF;
                }

            //the form will generate after here here
                $html_form = '';

                if(count($newCorr) == 0){

                    $html_form .= '<p>'.boz(_('Not Found!')).'</p>';
                } else {    

                    $html_form .= '<form  id="checkEnt" name="checkEnt" method="post" action="r.php?cp=<?=$cp?>">';
                    $html_form .= '<input type="hidden" value="checkEntrance" name="ent">';
                    $html_form .= '<table class="table zebra-striped table-bordered table-striped span8">';
                    $html_form .= '<tbody>';

                    foreach ($newCorr as $orgKey => $list) { 
                        $html_form .= '<tr><td width="5%">';
                        $html_form .= '<h5>'.$orgKey.'kg</h5>';
                        $html_form .= '</td>
                        <td width="10%">
                        <label class="control-label" for="inputWei">Boxer</label>';  
                        $html_form .= '<select class="input-xlarge" id="input" name="drop[0][]">';   
                        $html_form .= '<option value="">Select</option>';
                        foreach ($list as $key => $value) {
                            $html_form .= '<option value='.$key.'>'.$value.'</option>';

                        } 
                        $html_form .= '</select>
                        </td>
                        <td width="10%">
                        <label class="control-label" for="inputWei">Res</label>
                        <select class="input-xlarge" name="drop[1][]">';
                        $html_form .= '<option value="">Select</option>
                        <option value="en">Ent</option>
                        <option value="re">Res</option>
                        </select>
                        </td>
                        </tr>';
                    }

                    $html_form .= '<tr><td colspan="3">
                    <div class="modal-footer">
                    <button type=reset class="btn btn-danger">Reset</button>
                    <button class="btn btn-primary" ID="btnSaveBoxer">Save</button>
                    </div>
                    </td>
                    </tr> 
                    </tbody>
                    </table> 
                    </form>';   
                }             
                echo $html_form;

            } catch(PDOException $e) {
                addError($e->getMessage(), "MySql-Error", "error");
            }
        }
</pre>

它所做的正是......第一个 SQL 在 FetchAll 方法中返回数组,经过一些更改,结果将是这样的......

// 第一个查询结果数组检查这一行
$newCorr[$wclID][$id] = $nameL." ".$nameF;

<pre>
Array
(
    [4-6] => Array
        (
            [87] => haha lala
        )

    [8] => Array
        (
            [25] => sasa baba
            [24] => mama fafa
            [26] => tata lala
        )

    [5] => Array
        (
            [29] => papa oaoa
            [27] => laha mana
            [30] => salam sara
        )

    [2] => Array
        (
            [33] => tata kaka
            [32] => lala sasa
            [31] => Papa lama
            [34] => wana michel
        )

    [6] => Array
        (
            [35] => zaza yaya
            [37] => wana mata
            [36] => Kaku luba
        )

)
</pre>

如您所见,从上面的代码中,我生成了从上面的数组填充的下拉表单。

我遇到的问题是,在客户输入表单数据后,当我需要按原样填充表单时,除了必须选择客户选项作为默认值...

所以,我所做的是我使用第二个 SQL2,它生成以下数组,这个数组正是客户输入的。

// 第二个查询结果数组 $newCorrAc [$wc] [$id] [$ent]= $nameL." ".$nameF;

<pre>
Array
(
    [8] => Array
        (
            [26] => Array
                (
                    [ent] => tata lala
                )

        )

    [2] => Array
        (
            [31] => Array
                (
                    [res] => papa lama
                )

        )

) 
</pre>

主要问题是,当下拉菜单再次出现时,我如何能够从他/她之前输入的数据中填写表格?

简而言之,我想做的是提交表格,并且我有更正的链接。如果单击它应该显示相同的表单,除了之前输入的数据,可以从数据库中获取。

请问有什么想法吗?

4

1 回答 1

1

所以我挖掘了你的代码。更改表单生成循环时:

foreach ($list as $key => $value) {
    $html_form .= '<option value='.$key.'>'.$value.'</option>';

} 

对此:

// This will contain selected="selected" html code for your
// second column dropdown menu. This way seemed to be the clearest for me.
$typeEntSelected = '';
$typeResSelected = '';

foreach ($list as $key => $value) {
    $html_form .= '<option value="'.$key.'"';
    if (isset($newCorrAc[$orgKey][$key])) {
        // Select last user input in Boxer dropdown
        $html_form .= ' selected="selected"';

        $userEntry = $newCorrAc[$orgKey][$key];
        if (array_key_exists('ent', $userEntry)) {
            $typeEntSelected = ' selected="selected"';
        } elseif (array_key_exists('res', $userEntry)) {
            $typeResSelected = ' selected="selected"';
        }
    }
    $html_form .= '>'.$value.'</option>';
}

再低一点,从这里更改您的第二列下拉生成代码

$html_form .= '<option value="">Select</option>
<option value="en">Ent</option>
<option value="re">Res</option>
</select>
</td>
</tr>';

对此。块中的第二行和第三行已更改。

$html_form .= '<option value="">Select</option>
<option value="en"' . $typeEntSelected . '>Ent</option>
<option value="re"' . $typeResSelected . '>Res</option>
</select>
</td>
</tr>';

它在8kg Boxer字段中选择“tata lala”,在2kg Boxer字段中选择“Papa lama” 。现在它还在第二个字段中选择“ent”和 res。其余未选中。这是你所追求的行为吗?

于 2013-07-22T16:09:10.220 回答