Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following function,

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

what it does is exactly ...the First SQL returns array in FetchAll method, and after a bit of change the result will be like this...

// First Query Result array check this line
$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>

from the above code as you can see, I generate dropdown form, which filled from the above array.

What problem I have faced is, after the customer enter the form data, when I need to populate the form as it is except the customer choice must be selected as default...

So, what i did is i use second SQL2, which generates the following array, this array is exactly what the customer enter it.

// Second Query Result Array $newCorrAc [$wc] [$id] [$ent]= $nameL." ".$nameF;

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

        )

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

        )

) 
</pre>

The main question is, how do I be able to fill the forms from his/her previous enter data when the drop down populated again?

In short what i want to do is, the form is submited, and i have link for correction. if clicked it should show the same form except with the entered data previously, that can be get from database.

some Idea please?

share|improve this question
What about adding the loaded data to the value of each field? Or for dropdowns, add selected="selected" to the option matching the database entry. – UrGuardian4ngel 13 hours ago
how you mean exactly? can you elaborate a bit??? – Mr Internet 13 hours ago
You want to prefill the generated form, using the values the customer entered. When generating the form, you can set 'default' values for an input element, using the value attribute. <input name="..." value="{$defaultValue}" />. For dropdown, you can add selected="selected". <option value="val" selected="selected" />. – UrGuardian4ngel 13 hours ago
are you telling me that if i add selected="selected" to my dropdown works? could you please check the above cod and suggest where i should put this and how it gona work? I appriciate ur help.... – Mr Internet 13 hours ago
Well, only add selected="selected" if that value SHOULD be selected, ofcourse. So you'll have to check if it should be selected, then add it. Else, don't add it. And I'll have a look at it ;) – UrGuardian4ngel 13 hours ago

1 Answer

So I dug through your code. When changing the form generation loop:

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

} 

to this:

foreach ($list as $key => $value) {
    $html_form .= '<option value="'.$key.'"';
    // select option when a value is set for this field
    if (isset($newCorrAc[$orgKey][$key])) {
        $html_form .= ' selected="selected"';
    }
    $html_form .= '>'.$value.'</option>';
}

it selects 'tata lala' in the 8kg Boxer field, and 'Papa lama' in the 2kg Boxer field. The rest is unselected. Is is this behaviour that you're after?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.