0

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?

5
  • 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. Commented Jul 22, 2013 at 15:34
  • how you mean exactly? can you elaborate a bit??? Commented Jul 22, 2013 at 15:40
  • 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" />. Commented Jul 22, 2013 at 15:49
  • 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.... Commented Jul 22, 2013 at 15:59
  • 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 ;) Commented Jul 22, 2013 at 16:00

1 Answer 1

1

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:

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

and a little bit lower, change your second column dropdown generation code from this

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

to this. The second and third row in the block are changed.

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

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

Sign up to request clarification or add additional context in comments.

8 Comments

Hi UrGuardian4ngel, thanks for your help...it seems working well except the other drop down which is this part... It doesn't look picking up the corresponding entry. in other word when 'Papa lama is selected' the other drop down in front of the name should be pick up this '[res] => papa lama' res...very important..but we are in tip just need some amendment...idea lease?
waiting if you have any idea?
how do i handle this part? $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>';
Hey sorry... Just came home, after being a whole day out. But yes, that's those boxes are the only thing that select the last entry. Couldn't find the rest in your "user input array" back then :/ But I'll check again... Edit: oh it's just the key. I'll update the answer ;)
Please do so, there is another select drop down which comes together with first side by side, you solve the first but the second still empty...please help, I am waiting...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.