How to display values already selected in a select (PHP) - php

In need some help on this.
I have a form with a select who works perfectly and add all the values selected in my database. But now I want to do my update form, and I want that when I click to modify a user and arrive on the form, the values are already selected.
For example if in my first form, if I had selected the values 1 and 2, I would like 1 and 2 to be already selected when i go to the modification form. I've already did that :
<?php
$nomLogiciel = getDemandeNomLogiciel($id_demande)[0];
$nom = explode(", ", $nomLogiciel);
foreach ($nom as $item) {
$profilsTmp = getProfilByLogiciel($item); ?>
<div class="form-floating mb-3">
<select class="form-control selectpicker" id="id_profil" name="id_profil[]" multiple data-live-search="true">
<?php foreach ($profilsTmp as $item2) { ?>
<option value="<?php echo $item2['id_profil']; ?>"><?php echo $item2['profil']; ?></option>
<?php } ?>
</select>
<label for="id_profil">Profil <?php echo $item ?></label>
</div>
<?php } ?>
I already managed to do it with a simple select like this :
<select class="form-control selectpicker" id="nomPole" name="nomPole">
<option value="Ambulatoire" <?php if ($one['pole'] == "Ambulatoire") { echo ' selected="selected"'; } ?>>Ambulatoire</option>
<option value="Habitat et Vie Sociale" <?php if ($one['pole'] == "Habitat et Vie Sociale") { echo ' selected="selected"'; } ?>>Habitat et Vie Sociale</option>
</select>
But I would like to know how to do it with multiple values in a foreach ?
If you need my SQL function :
function getDemandeNomLogiciel($id_demande) {
global $bd;
$stmt = $bd->prepare('SELECT nom_logiciel FROM demandes WHERE id_demande = :id_demande');
$stmt->bindParam(ID_DEMANDE, $id_demande);
$stmt->execute();
return $stmt->fetch();
}
function getProfilByLogiciel($nomLogiciel) {
global $bd;
$stmt = $bd->prepare('SELECT * FROM profils_logiciels WHERE nom_logiciel = :nomLogiciel');
$stmt->bindParam(NOM_LOGICIEL, $nomLogiciel);
$stmt->execute();
return $stmt->fetchAll();
}
Thanks for your help !

I tried this and it worked very well. I just check if the values of my previous request can be found in the database. If yes I select them :
<?php
foreach ($structures as $item) { ?>
<option value="<?php echo $item['id_structure']; ?>"
<?php if ($item['id_structure'] == $structurePrincipaleID['id_structure']) { echo ' selected="selected"'; } ?>>
<?php echo $item['nom_structure'] ?></option>
<?php } ?>

Related

how to display selected multiple dropdown values in edit page in CodeIgniter

how to display selected multiple dropdown values in edit page in CodeIgniter
Values ​​are not displaying in multiple dropdowns on the edit page
this is how to retrieve data in db
<?php $assignuserstable = $this->db->get_where('assignuserstable',array('user_id'=>$user_id))->row_array(); ?>
<div class="form-group col-md-6">
<label for="admin_id"><?php echo get_phrase('Assign User'); ?>
<span class="text-danger">*</span></label>
<select class="form-control selectpicker" name="admin_idd[]" id="admin_id"
placeholder="Assign User" required multiple>
<option value="" hidden><?php echo get_phrase('Select User'); ?></option>
<?php
$system_usertable = $this->db->get('system_usertable')->result_array();
foreach($system_usertable as $row2):
?>
<option value="<?php echo $row2['admin_id'];?>"
<?php if($assignuserstable['admin_id'] == $row2['admin_id'])echo 'selected';?>>
<?php echo $row2['first_name'];?>
</option>
<?php
endforeach;
?>
</select>
</div>
This is how multiple array is added to the database
model
public function addclientdetails(){
$data['business_name'] = html_escape($this->input->post('business_name'));
$data['legal_name'] = html_escape($this->input->post('legal_name'));
$data['status'] = html_escape($this->input->post('status'));
$data['rating'] = html_escape($this->input->post('rating'));
$data['SU_id'] =html_escape($this->input->post('admin_id'));
date_default_timezone_set("Asia/Kolkata");
$data['created_at'] = Date('Y-m-d h:i:s');
$data['created_by'] = $this->session->userdata('admin_id');
$this->db->insert('user_table', $data);
$insertId = $this->db->insert_id
$admin_idd =html_escape($this->input->post('admin_idd'));
$result = array();
foreach($admin_idd AS $key => $val){
$result[] = array(
'user_id' => $insertId,
'admin_id' => $_POST['admin_idd'][$key],
'user_type' => html_escape($this->input->post('user_type')),
);
}
$this->db->insert_batch('assignuserstable', $result);
$insertId = $this->db->insert_id();
return $insertId;
}
it is impossible to identify the problem when you don't share your code. Please share your model and controller too.
At first please echo $system_usertable and $row2 and check whether there is a data or not.
and your option should looks like this
<option value="<?php echo $row2['admin_id'];?>" <?php echo (isset($assignuserstable['admin_id']) && $assignuserstable['admin_id'] == $row2['admin_id'])? 'selected' : NULL; ?>><?php echo $row2['first_name'];?></option>

How to insert text into database table if we write any thing on multiselect ?

i already made a box in which skills are multiselected if we type something in text box,but i want to add those skills which are not inside my database that user type.
my question is how i make that?
my code is-
<span class="pf-title">skills</span>
<div class="pf-field no-margin">
<select id="lstFruits"multiple="multiple"class="chosen"name="skill[]"
data-placeholder="Please Select Skill">
<?php
$fetch="SELECT * FROM `skill`";
$record=mysqli_query($conn,$fetch);
while($records_row=mysqli_fetch_array($record))
{?>
<option value="<?php echo $records_row['skill_name'];?>"<?
php foreach($sk as $s){if($records_row['skill_name'] == $s)
{ echo 'selected="selected"';}}?>><?php echo
$records_row['skill_name'];?></option>
<?php }
?>
</select>
Try this way:-
<select id="lstFruits" multiple="multiple" class="chosen" name="skill[]" data-placeholder="Please Select Skill">
<?php
$fetch="SELECT * FROM `skill`";
$record=mysqli_query($conn,$fetch);
$flag = false;
while($records_row = mysqli_fetch_array($record)){
foreach($sk as $s){
if($records_row['skill_name'] == $s) {
echo '<option value="'.$records_row['skill_name'].'" selected="selected">'.$records_row['skill_name'].'</option>';
$flag = true;
break;
}
}
if(!$flag) {
echo '<option value="'.$records_row['skill_name'].'">'.$records_row['skill_name'].'</option>';
}
$flag = false;
}
?>
</select>

How to echo selected values on multiselect from database on a multiselect form

i have a registration form using multiselect which insert options in the value 1,2,3,4,5,6,7 based on selection
However, after inserting i want to provide an option for editing where users edit
and i want the already selected values from database to be preselected while looping other options as unselected
I am experiencing unidentified offset 3, unidentified offset 3........
<div class="form-group">
<label for="touch-spin-1">Course Days</label>
<select multiple="multiple" name="coursedays[]" style="width: 100%;" data-toggle="select2" data-placeholder="<?php //echo $course_days; ?>" data-allow-clear="true" >
<?php
$entities = "1,2,3,4,5,6,7"; $entity = explode(",",$entities); $servs = explode("," , $course_days); $arr_length = count($entity); for($i=0;$i<=$arr_length;$i++){
?>
<option <?php if (in_array($servs, $entity)) { echo "selected";} ?> value="<?php echo $servs[$i]; ?>"> <?php echo $servs[$i]; ?></option>
<?php } ?></select></div>
try in this way
<?php
$course_days = "2,3,4";
$entities = "1,2,3,4,5,6,7";
$entity = explode(",", $entities);
$servs = explode(",", $course_days);
foreach($entity as $en) {
?>
<option value="<?php echo $en; ?>" <?php echo (in_array($en, $servs)) ? 'selected' : ''; ?>>
<?php echo $en; ?>
</option>
<?php } ?>

How will it show the selected value (SELECTED) in CodeIgniter 3.x?

I am trying to populate a drop-down list of the database. In my view file I have the following code
Here is my controller
$query = $this->interprete_model->interpreteID($this->session->userdata('user_id'));
print_r($query);
$data['interprete'] = $query;
Aqui esta mi vista, usa set_select.
<select class="form-control" name="regionI" id="regionI">
<option value="">- Select -</option>
<?php foreach($result as $row):?>
<option value="<?php echo $row->id;?>"
<?php echo set_select('regionI', $row->id, TRUE); ?>><?php echo $row->name;?></option>
<?php endforeach; ?>
</select>
Result:
enter image description here
Many selected, I need one selected to modify (update) the data.
You can try this :
<select class="form-control" name="regionI" id="regionI">
<option value="">- Select -</option>
<?php foreach($users as $row):
$selected = FALSE;
// 1 is the id u want to be selected u can change it according to you
if ($row->id == 1){
$selected = TRUE;
}
?>
<option value="<?php echo $row->id;?>"
<?php echo set_select('regionI', $row->id, $selected); ?>><?php echo $row->name;?></option>
<?php endforeach; ?>
</select>
You can also use form_dropdown as
// FOR ids
$ids = array(1,2,3,4); // array of user ids
echo form_dropdown('regionI',$ids,1,array('class'=>'form-control'));
// FOR name
$names= array('name1','name2','name4','name3'); // array of user names
echo form_dropdown('regionI',$names,'name1',array('class'=>'form-control'));
For More :
https://www.codeigniter.com/user_guide/helpers/form_helper.html
i write this way for edit time selection
<?php foreach ($select_single as $select_single_show):?>
<select class="form-control" name="regionI">
<?php foreach ($users as $row):?>
<option <?php if($row->id==$select_single_show->regionI)echo "selected";?> value="<?php echo $all_branch_show->id?>"><?php echo $row->name?>
</option>
<?php endforeach;?>
</select>
<?php endforeach;?>

How to pass two variables from form select using POST method?

Supplier:* <br/><select name="supplier">
<?php foreach ($db->query($sql) as $row) { ?>
<option value="<?php echo $row['supplier_id']; ?>">
<?php echo $row['supplier_name']; ?>
</option>
<?php } ?>
</select>
Retrieving data on the next php script:
$supplier_name = ??????
$supplier_id = ?????
The above code is allowing a user to make a selection of a supplier from a supplier table.
How can I pass both the supplier_name and the associated supplier_id to two different variable from the form using POST?
Bellow you have a quick example. It's not the case to use hidden input elements.
Your select:
<select name="supplier">
<?php foreach ($db->query($sql) as $row) { ?>
<option value="<?php echo $row['supplier_id'] .'|' . $row['supplier_name']; ?>">
<?php echo $row['supplier_name']; ?>
</option>
<?php } ?>
</select>
Php script that processes the form:
if(isset($_POST['supplier']) {
$arr = explode('|', $_POST[supplier]);
if( count($arr) == 2 ) {
$supplierId = $arr[0];
$supplierName = $arr[1];
}
}
The easiest way would be to do something like:
<option value="<?php echo $row['supplier_id'].';'.$row['supplier_name']; ?>">
<?php echo $row['supplier_name']; ?>
</option>
And then evaluate them like so:
$values = explode( ';', $_POST['supplier_info'] );
$supplier_id = $values[0];
$supplier_name = $values[1];
But it would be better to only transmit the id and then llokup the name of the supplier when evaluating the $_POST-Data

Categories