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>
Related
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 } ?>
I have a php form where I want to select options for a field Email from a list of values saved in a table emailaddress in the database. The table emailaddress has 2 fields 1. idemailaddress and 2. emailaddress, but I am still getting error trying to pull data, please help
Agreement_ctrl
$data['department_list'] = $this->Main_model->get_department_list();
$data['unit_list'] = $this->Main_model->get_unit_list();
$data['terms'] = $this->Main_model->get_terms_list();
$data['emailaddress_list'] = $this->Main_model->get_emailaddress_list();
$data['managers_list'] = $this->Main_model->get_managers_list();
$data['mtnlocation_list'] = $this->Main_model->get_mtnlocation_list();
$data['institution_list'] = $this->Main_model->get_institution_list();
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('gender', 'Gender', 'alpha|xss_clean');
$this->form_validation->set_rules('department', 'Department');
$this->form_validation->set_rules('unit', 'Unit', 'numeric|xss_clean');
Main Model File
}
function get_emailaddress_list()
{
$this->db->order_by('emailaddress', 'ASC');
$query = $this->db->get('emailaddress');
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$result [$row->id] = $row;
}
return $result;
}else{
return FALSE;
}
}
Form Page
<div class="form-group row">
<label class="col-sm-3 col-form-label" for="Email">Email Address:</label>
<div class="col-sm-9">
<select name="Email" class="Email form-control" id="Email">
<option value="" <?php echo set_select('Email', ''); ?> >-Select-</option>
<?php if($emailaddress_list){
foreach($emailadress_list as $emailaddress){ ?>
<option value="<?php echo $emailadress->idemailaddress?>" <?php echo set_select('Email', $emailaddress->idemailaddress); ?>><?php echo $emailaddress->emailaddress?></option>
<?php }
}?>
</select>
</div>
</div>
You don't need the foreach loop in your model, just return the query result.
function get_emailaddress_list(){
$this->db->order_by('emailaddress', 'ASC');
$query = $this->db->get('emailaddress');
$data = ($query->num_rows())? $query->result():false;
return $data;
}
Then you loop through this results in your view to create the select options:
<select name="Email" class="Email form-control" id="Email">
<?php if($emailaddress_list):?>
<option value="" disabled selected>-Select-</option>
<?php foreach($emailadress_list as $row): ?>
<option value="<?=$row->idemailaddress?>">
<?=$row->emailaddress?>
</option>
<?php endforeach;?>
<?php else:?>
no records found
<?php endif;?>
</select>
I've tried to create form where there are any options generate from database. Each type of product will generate different option.
For example:
Product A , there will be option: upgrade ram and upgrade core. And the choice available is different between another product.
But on the product B, there will be no option.
I've succeeded displaying those options on my form, but I can't continue to save it to the database. Because I am confused to determine the variable that will use to input to database (on the picture below variable "var"). What is the next step.
my table database:
here is my view
<?php foreach($configcat->result_array() as $row):
$idcat = $row['id_category'];
$category =$row['name'];
?>
<div class="form-group col-md-12">
<label><?php echo htmlentities($category, ENT_QUOTES, 'UTF-8'); ?></label>
<select name="<?php echo htmlentities("conf".$idcat, ENT_QUOTES, 'UTF-8'); ?>" class="form-control select2" style="width: 100%;">
<option value="" selected="selected"></option>
<?php
foreach ($configOption->result_array() as $option){
$idCatO = $option['id_category'];
$detail = htmlentities($option['detail_config'], ENT_QUOTES, 'UTF-8');
if ($idCatO == $idcat){
echo "<option value=\"$detail\">$detail</option>";
}
} ?>
</select>
</div>
<?php endforeach; ?>
And this my controller:
$hostname = $this->input->post("hostname");
$rootPassword = $this->input->post("rootPassword");
$hitung = $this->m_user->hitungRow();
$idUser= $this->session->userdata('id_user');
$timeStamp = sha1(date('Y-m-d'));
if ($hitung > 0){
for ($x=1; $x <= $hitung; $x++){
$var1 = "conf".$x;
$var= $this->input->post($var1);
}
}
//input to database????
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 } ?>
I have a PHP Crud system. One of the options when creating a new record is a selection box with two options. Netman and Administrator. When I update the record I see the option, but it duplicates and it has the other option,
like this. I cant see how I can show the record from the DB and the other option. Is this possible?
<?php
include "config.php";
include "header.php";
if(isset($_GET['u'])):
if(isset($_POST['bts'])):
$stmt = $mysqli->prepare("UPDATE passwordtable SET school=?,usertype=?, password=?, notes=? WHERE id_personal=?");
$stmt->bind_param('sssss', $nm, $gd, $tl, $ar, $id);
$nm = $_POST['nm'];
$gd = $_POST['gd'];
$tl = $_POST['tl'];
$ar = $_POST['ar'];
$id = $_POST['id'];
if($stmt->execute()):
echo "<script>location.href='index.php'</script>";
else:
echo "<script>alert('".$stmt->error."')</script>";
endif;
endif;
$res = $mysqli->query("SELECT * FROM passwordtable WHERE id_personal=".$_GET['u']);
$row = $res->fetch_assoc();
?>
And the selection box code us
<label for="gd">User Type</label>
<select class="form-control" id="gd" name="gd">
<option><?php echo $row['usertype'] ?></option>
<option>Administrator</option>
<option>NetMan</option>
</select>
If you still not find answer please have look at below code
<?php
$usertypes = array('Administrator' => 'Administrator', 'NetMan' => 'NetMan');
$selcted_usertype = !empty($row['usertype']) ? $row['usertype'] : "";
?>
<label for="gd">User Type</label>
<select class="form-control" id="gd" name="gd">
<?php
foreach ($usertypes as $user_type_val => $usertype_txt) {
$selected = ($user_type_val == $selcted_usertype) ? "selected='selected'" : "";
?>
<option <?php echo $selected; ?> value="<?php echo $user_type_val; ?>"><?php echo $usertype_txt; ?></option>
<?php }
?>
</select>
You will need to value attribute for option.