I have a problem to show first id value to show in the select option box, it can show me other id value, just cannot show me the first id value from the table. Below is my coding:
<select onchange="getComboA(this)" class="form-control blank" id="parentid" name="parentid" title="parentid"> <!-- <option selected disabled>Please Select</option>--> <!--Make this selected by default--> <option selected value="0">New Category</option <?php $sql_incharge = 'select * from filing_code_management where status=1 order by id'; $arr_incharge = db_conn_select($sql_incharge); foreach ($arr_incharge as $rs_incharge) { $folder_location = $rs_incharge['folder_location']; $function_code_select = $rs_incharge['function_code']; $function_name_select = $rs_incharge['function_name']; $activity_code_select = $rs_incharge['activity_code']; $activity_name_select = $rs_incharge['activity_name']; $name_select = $rs_incharge['name']; $id_select = $rs_incharge['id']; $category_id = $rs_incharge['category_id']; // get category_id here // save the data here↓↓ in data attributes echo "<option value='{$id_select}' data-cat_id='{$category_id}'>{$name_select}</option>"; } ?></select>
Below is my table value picture, my id 1 value is 100 PENTADBIRAN, but the selection box show me start from id 2 value:
Below is my output to show no first id value in the selection box, the id is start from id 2 not start from id 1:
What I have tried:
I have tried paste this code select * from filing_code_management where id=1 in the sql server to check. It can show me id 1 value in the sql server. But I don’t know why if I using this code in my coding, it cannot work.
Hope someone can guide me which part I am getting wrong and can show me the first id value in the selection box.
Advertisement
Answer
This is an html issue. The first selected option
<option selected value="0">New Category</option
Is missing the closing “>”. As such the first element is being rendered as a part of the HTML. When the first result from your query is rendered, it finally closes this selected option.
You may close it as shown below
<option selected value="0">New Category</option>