Hello guys I'm new to OOP PHP and I want to imitate the retaining of user generate data in form from procedural
here is my code:
<?php
class View_LType extends Config{
public function view_ltype(){
$con = $this->con();
$sql = "SELECT * FROM `tbl_leave_type`";
$data = $con->prepare($sql);
$data->execute();
$result = $data->fetchAll();
foreach ($result as $data){
echo "
<tr class='table-head'>
<td class='numbers'>$data[leave_id]</td>
<td>$data[leave_code]</td>
<td>$data[leave_name]</td>
<td class='small'>$data[leave_description]</td>
<td><a href='#'></a></td>
</tr>";
}
}
public function select_ltype(){
$con = $this->con();
$sql = "SELECT * FROM `tbl_leave_type`";
$data = $con->prepare($sql);
$data->execute();
$result = $data->fetchAll();
foreach ($result as $data){
echo '<option value="' . $data['leave_name'] . '">' . $data['leave_name'] . '</option>';
}
}
}
html page:
<div >
<label for="ltype">Leave Type</label>
<select name="ltype" id="ltype" name="ltype" required >
<option>Select leave...</option>
<?php $view_leave->select_ltype(); ?>
</select>
</div>
$data[leave_name] is what the user will select what I want is to echo selected when the user select its prefered leave name. please help me guys
CodePudding user response:
If the current $data['leave_name']
is the same as the submitted "ltype", then select that option:
echo '<option value="' . $data['leave_name'] . '"' . (($data['leave_name'] == $_REQUEST['ltype']) ? ' selected' : '') . '>' . $data['leave_name'] . '</option>';