I can't show the data that I want to edit from my table. The combobox and the table don't show data when I edit the data categories, sub-categories, and manufacturers. I want to be able to see the data from my table after editing.
Controller form
public function data_items_edit($id)
{
$this->load->model('additem_m');
$model3 = $this->additem_m;
$data['table'] = $model3->get_where2($id)[0];
//$data['table'] = $model3->get();
$this->load->model('category_m');
$model = $this->category_m;
$data['category'] = $model->get();
$this->load->model('subcategory_m');
$model1 = $this->subcategory_m;
$data['sub'] = $model1->get();
$this->load->model('manufactures_m');
$model2 = $this->manufactures_m;
$data['manu'] = $model2->get();
//print_r($data['table']);
$this->load->view('items/edit_items_v', $data);
}
Data table controller
public function index()
{
if($this->session->has_userdata('isLogin')){
$this->load->model('additem_m');
$model = $this->additem_m;
$data['table'] = $model->get('items.*, item_categories.name as ic,
item_categories_sub.name as ics, item_manufactures.name as im',
[
['table'=>'item_categories','condition'=>'item_categories.id =
items.item_category_id'],
['table'=>'item_categories_sub','condition'=>'item_categories_sub.id
= items.item_category_sub_id'],
['table'=>'item_manufactures','condition'=>'item_manufactures.id =
items.item_manufacturer_id'],
]);
//print_r($data['table']);
$this->load->view('items/items_v', $data);
}else{
redirect('login');
}
}
View Form
<div class="form-group">
<label class="col-sm-4 control-label form-label">Category :</label>
<div class="col-sm-8">
<select name="item_category_id" class="form-control" id="category">
<option value='' <?php if($category == '0'){ echo 'selected';} ?>>--Select--</option>
<?php foreach($category as $category){
echo '<option value="'.$category->id.'">'.$category->name.'</option>';
} ?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label form-label">Sub Category</label>
<div class="col-sm-8">
<select name="item_category_sub_id" class="form-control" id="category_sub">
<option value=''>- Select Sub Category -</option>
</select>
</div>
</div>
<div class="rightcontact">
<div class="form-group">
<label class="col-sm-4 control-label form-label">Manufacturer</label>
<div class="col-sm-8">
<select name="item_manufacturer_id" class="form-control" id="item_manufacturer_id">
<option>- Select Manufacturer -</option>
<?php foreach($manu as $manu){
echo '<option value="'.$manu->id.'">'.$manu->name."</option>";
} ?>
</select>
</div>
</div>
</div>
My view
You have to add selected attribute to the options.
$category == $category->id ? "selected" : ""
Something like this
<?php
foreach($category as $category){
echo '<option value="'.$category->id.'" '. $category == $category->id ? "selected" : "" .'>'.$category->name.'</option>';
^ ^
}
?>
Related
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 am using yii2. I want to pass the model id to a view but while doing so I am getting an error.
Controller
public function actionProcess($id){
try {
$model = $this->findModel($id);
} catch (NotFoundHttpException $e) {
} // this will find my model/record based on the id
$file_name = $_GET['file_name'];
// $data = \moonland\phpexcel\Excel::import("uploads/test.xlsx"); // $config is an optional
try {
$header_index = $_GET['header_no'];
$data = \moonland\phpexcel\Excel::widget([
'mode' => 'import',
'fileName' => 'uploads/' . $file_name,
'setFirstRecordAsKeys' => false, // if you want to set the keys of record column with first record, if it not set, the header with use the alphabet column on excel.
'setIndexSheetByName' => false, // set this if your excel data with multiple worksheet, the index of array will be set with the sheet name. If this not set, the index will use numeric.
'getOnlySheet' => 0, // you can set this property if you want to get the specified sheet from the excel data with multiple worksheet.
]);
if (isset($data[0])) {
$headers = $data[0][$header_index];
} else {
$headers = $data[$header_index];
}
}catch (Exception $x){
print_r($x->errorInfo);
}
return $this->render('excel_options',['headers'=>$headers,'file_name'=>$file_name,'header_index'=>$header_index,'id'=>$model->id]);
}
View
In my view, I am trying to pass the model id in a submit button
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-4">
<br />
Submit
</div>
</div>
Now when I try to access my excel_options view, I am getting the below error.
Undefined variable: model
in E:\xampp\htdocs\inventory-web\backend\views\meteracceptanceheader\excel_options.php at line 103
Submit
Update 1
The controller from which I am rendering the excel_options is below
public function actionProcess($id){
$model = $this->findModel($id);
// this will find my model/record based on the id
$file_name = $_GET['file_name'];
// $data = \moonland\phpexcel\Excel::import("uploads/test.xlsx"); // $config is an optional
try {
$header_index = $_GET['header_no'];
$data = \moonland\phpexcel\Excel::widget([
'mode' => 'import',
'fileName' => 'uploads/' . $file_name,
'setFirstRecordAsKeys' => false, // if you want to set the keys of record column with first record, if it not set, the header with use the alphabet column on excel.
'setIndexSheetByName' => false, // set this if your excel data with multiple worksheet, the index of array will be set with the sheet name. If this not set, the index will use numeric.
'getOnlySheet' => 0, // you can set this property if you want to get the specified sheet from the excel data with multiple worksheet.
]);
if (isset($data[0])) {
$headers = $data[0][$header_index];
} else {
$headers = $data[$header_index];
}
}catch (Exception $x){
print_r($x->errorInfo);
}
return $this->render('excel_options',['headers'=>$headers,'file_name'=>$file_name,'header_index'=>$header_index,'id'=>$model->id]);
}
View
<div class="box">
<!-- /.box-header -->
<div class="box-body">
<form action="import" method="post">
<input type="hidden" name="file_name" value="<?=$_GET['file_name']?>" />
<input type="hidden" name="header_index" value="<?= $_GET['header_no'] ?>"/>
<h1>Maping</h1>
<div class="row">
<div class="col-md-2">
Ref #:
</div>
<div class="col-md-4">
<select name="field[0][ref_no]" class="form-control">
<option value="">Select A field</option>
<?php foreach($headers as $k=>$v) { ?>
<?php if (trim($v) != '') { ?>
<option value="<?=$k?>"><?=$v?></option>
<?php } ?>
<?php } ?>
</select>
</div>
</div>
<div class="row">
<div class="col-md-2">
Meter MSN:
</div>
<div class="col-md-4">
<select name="field[0][meter_msn]" class="form-control">
<option value="">Select A field</option>
<?php foreach ($headers as $k => $v) { ?>
<?php if (trim($v) != '') { ?>
<option value="<?= $k ?>"><?= $v ?></option>
<?php } ?>
<?php } ?>
</select>
</div>
</div>
<div class="row">
<div class="col-md-2">
Meter Type:
</div>
<div class="col-md-4">
<select name="field[0][meter_type]" class="form-control">
<option value="">Select A field</option>
<?php foreach ($headers as $k => $v) { ?>
<?php if (trim($v) != '') { ?>
<option value="<?= $k ?>"><?= $v ?></option>
<?php } ?>
<?php } ?>
</select>
</div>
</div>
<div class="row">
<div class="col-md-2">
Sub-Div:
</div>
<div class="col-md-4">
<select name="field[0][sub_div]" class="form-control">
<option value="">Select A field</option>
<?php foreach ($headers as $k => $v) { ?>
<?php if (trim($v) != '') { ?>
<option value="<?= $k ?>"><?= $v ?></option>
<?php } ?>
<?php } ?>
</select>
</div>
</div>
<div class="row">
<div class="col-md-2"></div>
<div class="col-md-4">
<br />
Submit
</div>
</div>
</form>
</div>
</div>
How can I pass my model id into the view?
Any help would be highly appreciated
Try to pass not id but $model it self.
return $this->render('excel_options',['headers'=>$headers,'file_name'=>$file_name,'header_index'=>$header_index,'model'=>$model]);
and Keep you code in view.
Submit
I am going to build a dynamic product category with their subs for my final year project. I tried the tree way but somehow it make me confused so i decided to make it as simple. I want to display Clothe's parent name.I want to display it like this
Can i do it so ?
Here is my PHP code
<form method="post" action="product_category_add_exec.php" enctype="multipart/form-data">
<div class="form-group">
<label for="recipient-level" class="control-label"> Parent Category</label>
<select class="form-control" name="admin_lid" required="">
<option></option>
<?php
$sql_pcat = "SELECT * FROM product_category";
$select_pcat = mysqli_query($db,$sql_pcat) or die (mysqli_error().$sql_pcat);
$x =1;
while($list_pcat = mysqli_fetch_array($select_pcat))
{
$product_cat_id = $list_pcat['product_cat_id'];
$parent_id = $list_pcat['parent_id'];
$product_cat_name = $list_pcat['product_cat_name'];
?>
<?php
if ($parent_id == 0)
{
?>
<option value = "<?php echo $product_cat_id;?>"><?php echo $product_cat_name; ?></option>
<?php
} else
{
$sql_cat = "SELECT * FROM product_category WHERE parent_id= $parent_id ORDER BY product_cat_name ASC";
$select_cat = mysqli_query($db,$sql_cat) or die (mysqli_error().$sql_cat);
$list_cat = mysqli_fetch_array($select_cat);
$product_cat_id = $list_cat['product_cat_id'];
$parent_id = $list_cat['parent_id'];
$product_cat_name = $list_cat['product_cat_name'];
?>
<option value = "<?php echo $parent_id;?>">--<?php echo $parent_id;?><?php echo $product_cat_name; ?></option>
<?php
}
?>
<?php
$x++;
}
?>
</select>
</div>
<div class="form-group">
<label for="recipient-category" class="control-label">Product Name </label>
<input type="text" class="form-control" id="recipient-category" name="product_cat_name">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-info">Add</button>
<button type ="reset" class ="btn btn-danger">Reset</button>
</div>
</form>
This is my database table
I want to make it appear like this
I have a modal form with two select boxes with the same current_models class.
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="control-label">Model</label>
<select class='form-control' id='left_model_id' name='left_model_id'>
<div id="left_model" class="current_models"></div>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="control-label">Model</label>
<select class='form-control' id='right_model_id' name='right_model_id'>
<div id="right_model" class="current_models"></div>
</select>
</div>
</div>
</div>
Each select needs to have it's own id, but they are loading the same select options into groups like this:
$('.current_models').load('ajax_get_models_man_group.php');
Here's my ajax_get_models_man_group.php page:
<?php
require_once 'core/init.php';
$user = new User();
if(!$user->isLoggedIn()) {
Redirect::to('login.php');
}
$models = DB::getInstance()->query("SELECT man.id AS man_id, man.man AS man, models.name AS model, models.id AS model_id FROM hearing_aid_models models LEFT JOIN hearing_aid_man man ON man.id = models.man_id WHERE models.currently_fitting = 'Y' AND company_id = '" . $user->data()->company_id . "' ORDER BY man ASC");
if ($models->error()) {
echo 'Error occurred.';
} else {
$currentGroup = null;
foreach( $models->results() as $result ) {
// start a new optgroup
if( $currentGroup == null || $result->man_id != $currentGroup ) {
// end the previous group
if( $currentGroup != null ) {
echo "</optgroup\n>";
}
// start a new group
echo "<optgroup data-id='{$result->man_id}' label='{$result->man}'>\n";
$currentGroup = $result->man_id;
}
echo "<option value='{$result->model_id}'>{$result->model}</option>\n";
}
// end the last opt group
if( $currentGroup != null ) echo "</optgroup>\n";
}
?>
The problem is that the values do not load into the select divs!
Here's my ajax source code results:
<optgroup data-id='1' label='Audibel'>
<option value='95'>Start 7 Wireless</option>
<option value='96'>Start 7</option>
<option value='99'>A3i Platinum</option>
</optgroup>
<optgroup data-id='16' label='Phonak'>
<option value='98'>Naida Q70</option>
</optgroup>
<optgroup data-id='2' label='Starkey'>
<option value='100'>S Series i30</option>
<option value='81'>3 Series I 70</option>
<option value='82'>3 Series I 90</option>
</optgroup>
1) Get rid of divs inside select tags
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="control-label">Model</label>
<select class='form-control current_models' id='left_model_id' name='left_model_id'>
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="control-label">Model</label>
<select class='form-control current_models' id='right_model_id' name='right_model_id'>
</select>
</div>
</div>
</div>
2) load ajax inside the right div
$('.current_models').load('ajax_get_models_man_group.php');
I have a problem, where I want to get a value from the database, and if the value matches the one in my option, the option should be selected.
This is my form:
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="tidspunkt_varighed">Vælg Antal Timer</label>
<div class="col-md-4">
<select id="tidspunkt_varighed" name="tidspunkt_varighed" class="form-control">
<option value="1">1 Time</option>
<option value="2">2 Timer</option>
<option value="3">3 Timer</option>
<option value="4">4 Timer</option>
</select>
</div>
</div>
I don't want to make an if clause every line.
Thanks in advance.
Kristian
You should use a loop as your values are incremental when check with one if() inside the loop for the checked value.
<?php
$options = '';
$valueFromDb = 1;
for($i = 1;$i<=4 ; $i++)
{
if( $i == $valueFromDb) {
$options .= '<option value="'.$i.'" selected="selected">'.$i.'Time</option>';
} else {
$options .= '<option value="'.$i.'" >'.$i.'Time</option>';
}
}
?>
<div class="form-group">
<label class="col-md-4 control-label" for="tidspunkt_varighed">V�lg Antal Timer</label>
<div class="col-md-4">
<select id="tidspunkt_varighed" name="tidspunkt_varighed" class="form-control">
<?php echo $options;?>
</select>
</div>
</div>
I think, you can use an array somehow like that:
<?
$value=YOUR_VALUE_FROM_DB
$selected[$value]="selected";
?>
<option value="1" <?=$selected[1]?>>1 Time</option>
<option value="2" <?=$selected[2]?>>2 Timer</option>
etc.
if you are not displaying these options using a loop then you have to put if with every option like
<option value="1" <?php if($dbvalue == 1){echo 'selected="selected"';}>1 Time</option>
and in case you are showing options using a loop you can put a condition in the loop and make a string like
$selected = 'selected="selected"';
based on the condition, and echo $selected with every option.
$val_from_db = 3;
$output = '<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="tidspunkt_varighed">Vælg Antal Timer</label>
<div class="col-md-4">
<select id="tidspunkt_varighed" name="tidspunkt_varighed" class="form-control">';
foreach( $values as $key=>$value ) {
if ( $key==$val_from_db )
$selected = 'selected="selected" ';
else
$selected = '';
$output .= '<option '.$selected.'value="'.$key.'">'.$value.'</option>';
}
$output .= '</select>
</div>
</div>';
echo $output;