Reputation: 64
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?
Upvotes: 0
Views: 107
Reputation: 568
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?
Upvotes: 1