Not able to fetch the static dropdown value using codeigniter php - php

I am having a blog page which needs to be updated but unable to fetch the dropdown value from database.Here is the code for fetching dropdown value from database.While adding the data it is inserting successfully but while fetching the data to edit it is not working.Thanks In Advance
View:
<?php if(isset($records) && is_array($records) && count($records)>0): ?>
<?php foreach($records as $r):?>
<?php
$form_attributes = array('name'=>'edit', 'id'=>'edit', 'enctype' => "multipart/form-data");
echo form_open('blogs/editblogs',$form_attributes);
echo form_hidden('blog_id',$r->blog_id);
?>
<div class="element">
<label for="positions"><font color="black">Position</font></label>
<select name="position">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">Select none</option>
</select>
</div>
<?php echo form_close();?>
<?php endforeach;endif;?>
Controller:
function editblogs()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<br /><span class="message error"> ','</span>');
$this->form_validation->set_rules('position','Position');
if($this->form_validation->run()== FALSE)
{
$data['records']=$this->blogs_model->getblogsdata($this->input->post('blog_id'));
$data['mainpage']='blogs';
$data['mode']='edit';
$this->load->view('templates/template',$data);
}
else
{
$result = $this->blogs_model->update($this->input->post('blog_id'));
if(is_array($result))
{
$data['errors']=$result;
$data['records']=$this->blogs_model->getblogsdata($this->uri->segment('blog_id'));
$data['mainpage']='blogs';
$data['mode']='edit';
$this->load->view('templates/template',$data);
}
else
$this->flash->success('<h2>Successfully Updated the record.<h2>');
redirect('blogs');
}
}
Model:
function update($id)
{
$data=array(
'position'=>$this->input->post('position')
);
$this->db->where(array('blog_id'=>$id));
$this->db->update('blogs', $data);
return true;
}

Can you try something like this below one
Please try the below code
<?php if(isset($records) && is_array($records) && count($records)>0): ?>
<?php foreach($records as $r):?>
<?php
$form_attributes = array('name'=>'edit', 'id'=>'edit', 'enctype' => "multipart/form-data");
echo form_open('blogs/editblogs',$form_attributes);
echo form_hidden('blog_id',$r->blog_id);
?>
<div class="element">
<label for="positions"><font color="black">Position</font></label>
<select name="position">
<?php for($i=1;i<3;i++)
$option = '<option value="'.$i.'"';
if($i === $r->position ){
$option .= 'selected';
}
$option .= '>'.$i.'</option>';
<?php?>
<option value="4">Select none</option>
</select>
</div>
<?php echo form_close();?>
<?php endforeach;endif;?>

Fetching the value from database for static dropdown. Here is the answer.
<div class="element">
<label for="positions"><font color="black">Position</font></label>
<?php
$selected = ($this->input->post('position')) ? $this->input->post('position') : $r->position; ;
$position = array("1" => "1", "2" => "2", "3" => "3", "4" => "Select none");
echo form_dropdown('position', $position, $selected);
?>
</div>

Related

How to display values already selected in a select (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 } ?>

How to Save the Checkbox Value Form to MySQL in One Field

How to Save the Checkbox Value Form to MySQL in One Field, can anyone help me..
Here is my code:
function student($param1 = '') {
if ($this->session->userdata('admin_login') != 1) {
redirect('login', 'refresh');
}
$running_year = $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description;
if ($param1 == 'create') {
$data['ekskul_id'] = $this->input->post('ekskul_id');
$this->db->insert('student', $data);
}
}
this is my display code
<div class="form-group">
<label for="field-2" class="col-sm-3 control-label">Ekstrakurikuler</label>
<div class="col-sm-5">
<select data-placeholder="Select" name="ekskul_id[]" multiple class="chosen-select" tabindex="8">
<?php
$ekstra = $this->db->get('ekstrakurikuler')->result_array();
foreach($ekstra as $row):
?>
<option value="<?php echo $row['ekskul_id'];?>"><?php echo $row['ekskul_name'];?></option>
<?php
endforeach;
?>
</select>
</div>
</div>
use below code to store multiple id in one field:
<?php
$ekskul_id= implode(",",$_POST['ekskul_id']);
?>
In your code :
if(isset($this->input->post('ekskul_id'))){
$data['ekskul_id'] = implode(",",$this->input->post('ekskul_id'));
}

Set_value() Array fields select option Codeigniter not working

I trying to create dynamic server side select input, after i submit, the set_value('nilai[]') not showing any value.
Here's my Controller below:
$this->load->library('form_validation');
$this->form_validation->set_rules('nilai[]', 'Nilai Pantuhir', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('pantuhir/pantuhir_form');
} else {
$list_pantuhir = $this->input->post('nilai');
foreach ($list_pantuhir as $key => $value) {
echo $value."<br />";
}
}
Here's my view below :
<div class="form-group <?php if(form_error('nilai[]')){echo 'has-error';} ?>">
<select class="form-control" name="nilai[]">
<option value="">- Choose-</option>
<option value="<?php echo $rowPerson['intUserId'].'-'.'A';?>" <?php if(set_value('nilai[]') == $rowPerson['intUserId'].'-'.'A') { echo 'selected'; } ?>>A</option>
<option value="<?php echo $rowPerson['intUserId'].'-'.'B';?>" <?php if(set_value('nilai[]') == $rowPerson['intUserId'].'-'.'B') { echo 'selected'; } ?>>B</option>
</select>
<?php echo form_error('nilai[]'); ?>
</div>
I want to show set_value and get selected in option field if validation not correct.
Hope this will help you :
Use set_select instead of set_value. If you use a menu, this function permits you to display the menu item that was selected, after the form validation throws any error
It should be like this :
<div class="form-group <?php if(form_error('nilai[]')){echo 'has-error';} ?>">
<select name="nilai[]" >
<option value="" >---Choose----</option>
<option
value="<?=$rowPerson['intUserId'].'-A';?>"
<?=set_select('nilai[]', $rowPerson['intUserId'].'-A');?>
>A</option>
<option
value="<?php echo $rowPerson['intUserId'].'-B';?>"
<?=set_select('nilai[]', $rowPerson['intUserId'].'-B');?>
>B</option>
</select>
<?php echo form_error('nilai[]'); ?>
</div>
For more : https://www.codeigniter.com/user_guide/helpers/form_helper.html#set_select
Use this code in the view
<div class="form-group <?php if(form_error('nilai[]')){echo 'has-error';} ?>">
<select class="form-control" name="nilai[]">
<option value="">- Choose-</option>
<option value="<?php echo $rowPerson['intUserId'].'-'.'A';?>" <?php if(set_value('nilai[]',rowPerson['intUserId'].'-'.'A') == $rowPerson['intUserId'].'-'.'A') { echo 'selected'; } ?>>A</option>
<option value="<?php echo $rowPerson['intUserId'].'-'.'B';?>" <?php if(set_value('nilai[]',$rowPerson['intUserId'].'-'.'B') == $rowPerson['intUserId'].'-'.'B') { echo 'selected'; } ?>>B</option>
</select>
<?php echo form_error('nilai[]'); ?>
</div>

Codeigniter Searchbar

Searchbar is working in codeigniter website but it does not load suggestion products. As I want if someone write 'dell' in searchbay he should get dell laptops suggestions below. and one another thing 'enter' button does not work for search one have to click the search icon to search the product.code is below
Controller
function text_search(){
if ($this->crud_model->get_settings_value('general_settings','vendor_system') !== 'ok') {
$search = $this->input->post('query');
$category = $this->input->post('category');
redirect(base_url() . 'index.php/home/category/'.$category.'/0-0/0/0/'.$search, 'refresh');
}else{
$type = $this->input->post('type');
$search = $this->input->post('query');
$category = $this->input->post('category');
if($type == 'vendor'){
redirect(base_url() . 'index.php/home/store_locator/'.$search, 'refresh');
} else if($type == 'product'){
redirect(base_url() . 'index.php/home/category/'.$category.'/0-0/0/0/'.$search, 'refresh');
}
}
}
Model
function get_type_name_by_id($type, $type_id = '', $field = 'name')
{
if ($type_id != '') {
$l = $this->db->get_where($type, array(
$type . '_id' => $type_id
));
$n = $l->num_rows();
if ($n > 0) {
return $l->row()->$field;
}
}
}
function get_settings_value($type, $type_name = '', $field = 'value')
{
if ($type_name != '') {
return $this->db->get_where($type, array('type' => $type_name))->row()->$field;
}
}
View
<div class="header-search">
<?php
echo form_open(base_url() . 'index.php/home/text_search/', array(
'method' => 'post'
));
?>
<input class="form-control" type="text" name="query" placeholder="<?php echo translate('what_are_you_looking_for');?>?"/>
<select
class="selectpicker header-search-select cat_select hidden-xs" data-live-search="true" name="category"
data-toggle="tooltip" title="<?php echo translate('select');?>">
<option value="0"><?php echo translate('all_categories');?></option>
<?php
$categories = $this->db->get('category')->result_array();
foreach ($categories as $row1) {
if($this->crud_model->if_publishable_category($row1['category_id'])){
?>
<option value="<?php echo $row1['category_id']; ?>"><?php echo $row1['category_name']; ?></option>
<?php
}
}
?>
</select>
<?php
if ($this->crud_model->get_type_name_by_id('general_settings','58','value') == 'ok') {
?>
<select
class="selectpicker header-search-select" data-live-search="true" name="type" onchange="header_search_set(this.value);"
data-toggle="tooltip" title="<?php echo translate('select');?>">
<option value="product"><?php echo translate('product');?></option>
<option value="vendor"><?php echo translate('vendor');?></option>
</select>
<?php
}
?>
<button class="shrc_btn"><i class="fa fa-search"></i></button>
</form>
</div>
<!-- /Header search -->

validate if checkbox has been checked

I'm trying to show an error message if a checkbox has not been selected. I've managed to get it done input field, but unsure of how to get it done with a checkbox. This is what I have so far:
<?php
# check if data has been posted
if (!empty($_POST)) {
# keep track validation errors
$author_error = null;
$categories_error = null;
# keep track of post values
$author = $_POST['author'];
$categories = $_POST['categories'];
# validate input
if (empty($author)) {
$author_error = 'Please select author';
$valid = false;
}
if (empty($categories)) {
$categories = 'Please select categories';
$valid = false;
}
# if data is valid, insert into the database
if ($valid) {
}
}
?>
<div class="control-group <?php if (!empty($author_error)){ echo 'error'; } ?>">
<label class="control-label">Author</label>
<div class="controls">
<select name="author" id="author">
<option value="">Select one</option>
<?php $sql2 = 'SELECT id, name FROM author';
foreach ($dbConnection->query($sql2) as $data2) { ?>
<option value="<?php echo $data2['id']; ?>"
<?php if(isset($author) && $author == $data2['id']) { echo 'selected'; } ?>>
<?php echo $data2['name']; ?>
</option>
<?php } ?>
</select>
<?php if (!empty($author_error)) { echo '<span class="help-inline">' . $author_error . '</span>'; } ?>
</div>
</div>
<div class="control-group <?php if (!empty($categories_error)){ echo 'error'; } ?>">
<fieldset>
<legend class="control-label">Categories:</legend>
<?php $sql3 = 'SELECT id, name FROM category';
foreach ($dbConnection->query($sql3) as $data3) { ?>
<div class="controls">
<label for="category<?php echo($data3['id']);?>">
<input type="checkbox" name="categories" id="categories" value="<?php echo($data3['id']); ?>"
<?php if(isset($_POST['categories']) && in_array($data3['id'], $_POST['categories'])) { echo 'checked'; } ?>>
<?php echo($data3['name']); ?>
</label>
</div>
<?php } ?>
</fieldset>
<?php if (!empty($categories_error)) { echo '<span class="help-inline controls">' . $categories_error . '</span>'; } ?>
</div>
Where am I going wrong with the categories field?
Check it like:
isset($_POST['categories'])
On line #62 you are checking if $categories_error is empty but you are using variable $categories for storing error message on line #19

Categories