Want dynamic table row in codeigniter - php

I am new in codeigniter. I am trying to make a table for product information under a search box in which table row will be increased dynamically from database according to each search by product model number. Now previous search data replaced by new search result and show only one product data but I want to show all previous search result in table row.I tried it by using session but not working. Thanks for any help.
Model:
class Product_Model extends CI_Model {
function product_search($model)
{
$this->db->like('modelno',$model);
$query = $this->db->get('alliteam');
if($query->num_rows() > 0){
foreach ($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
}
Controller:
public function sell(){
$this->load->library('session');
$data = array(
'title' => 'New Customer',
'action' => site_url('Product/sell'),
'button' => 'Add Item'
);
$model = $this->input->post('model',TRUE);
$data['productinfo'] = $this->Product_Model->product_search($model);
$this->session->set_userdata($productinfo);
$this->load->view('customer',$this->session->all_userdata());
}
View
<form action="<?php echo $action;?>" method="post">
<fieldset>
<div class="control-group">
<label class="control-label" >Model No :</label>
<div >
<input type="text" name="model" value="" >
<span> Enter a product model no</span>
</div>
<div class="form-actions">
<input type="submit" name="add_item" value="<?php echo $button;?>"/>
</div>
</fieldset>
</form>
<table >
<thead>
<tr>
<th>Name</th>
<th>Model Number</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
if($this->input->post('add_item')){
foreach($productinfo as $product):?>
<tr>
<td class="center"><?php echo $product->name ?></td><br>
<td class="center"><?php echo $product->modelno ?></td>
<td class="center"><?php echo $product->product_description?></td>
</tr>
<?php endforeach; } ?>
</tbody>
</table>

You don't need to use sessions here.
Edit your controller
public function sell(){
$data = array(
'title' => 'New Customer',
'action' => site_url('Product/sell'),
'button' => 'Add Item'
);
$model = $this->input->post('model',TRUE);
$data['productinfo'] = $this->Product_Model->product_search($model);
$this->load->view('customer',$data);
}

Related

Can't redirect after update data codeigniter and still in current page

Help, i cant redirect after update data, after edit the page still in current page. but when i place the redirect function after query $update, when i press edit button the page will go to redirect page and data edit form not show. How to fix this?
My Controller
is it i make a wrong if?
public function edit_anggaran($getId)
{
$id = encode_php_tags($getId);
$data_id = $this->uri->segment('4');
$this->_validasi();
if ($this->form_validation->run() == false) {
$data['title'] = "Detail Anggaran Pengajuan Proposal";
$data['detail_anggaran_proposal'] = $this->admin->get('detail_anggaran_proposal', ['id_detail_anggaran_proposal' => $data_id]);
$data['proposal'] = $this->admin->get('proposal', ['id_proposal' => $id]);
if ($this->template->load('templates/dashboard', 'proposal/edit_anggaran', $data)){
$d = array( 'nama_kebutuhan'=> $this->input->post('nama_kebutuhan'),
'harga' => $this->input->post('harga'),
'jumlah'=> $this->input->post('jumlah')
);
}
$update = $this->admin->update_detail('detail_anggaran_proposal', 'id_detail_anggaran_proposal', $data_id, $d);
redirect('proposal/anggaran/'.$id);
} else {
if ($update) {
set_pesan('data berhasil disimpan.');
redirect('proposal/anggaran/'.$id);
} else {
set_pesan('data gagal disimpan', false);
redirect('proposal/add');
}
// $input = $this->input->post(null, true);
}
}
My Model
public function update_detail($table, $pk, $id, $data)
{
$this->db->where($pk, $id);
return $this->db->update($table, $data);
}
My View
<div class="card-body">
<?= $this->session->flashdata('pesan'); ?>
<?= form_open('', [], ['id_detail_anggaran_proposal' => $detail_anggaran_proposal['id_detail_anggaran_proposal']]); ?>
<table class="table table-bordered" id="">
<thead>
<tr>
<th>Nama Kebutuhan</th>
<th>Harga</th>
<th>Jumlah</th>
<th>Total Harga</th>
</tr>
</thead>
<tbody>
<td><input value="<?= set_value('nama_kebutuhan', $detail_anggaran_proposal['nama_kebutuhan']); ?>" name="nama_kebutuhan" id="nama_kebutuhan" type="text" class="form-control" ></td>
<td><input value="<?= set_value('harga', number_format($detail_anggaran_proposal['harga'],'0',',','.')); ?>" name="harga" id="harga" type="text" class="form-control" ></td>
<td><input value="<?= set_value('jumlah', $detail_anggaran_proposal['jumlah']); ?>" name="jumlah" id="jumlah" type="text" class="form-control" ></td>
<?php $total=0; ?>
<td>Rp. <?= number_format($subtotal=$detail_anggaran_proposal['harga'] * $detail_anggaran_proposal['jumlah'],'0',',','.'); $total += $subtotal; ?> </td>
</tr>
<tr>
<td colspan="3" class="text-center"><h5><b>Total Anggaran </b></h5></td>
<td>Rp. <?= number_format($total,'0',',','.') ?></td>
</tr>
</tbody>
</table>
<div class="row form-group">
<div class="col-md-9 offset-md-3">
<button type="submit" class="btn btn-primary" style="float: right;">Simpan</button>
</div>
</div>
</div>
</div>
<?= form_close(); ?>
</div>

Codeigniter 3.x, Validation Always Passes Whether Checkbox Checked or Not

I am trying to make selecting one checkbox from a total of four checkboxes required but it's not working. Whether I select a checkbox or not, it always passes validation.
Here is my method in my controller Cargo_inquiry() {}
function step_two() {
$country_to_select = $this->session->userdata('freight_address_country');
$this->data['users'] = $this->My_Users_Model->get_companies_list($country_to_select);
// load up the validation rules for blog Info form
$this->config->load('validate_cargo');
$this->form_validation->set_rules($this->config >item('validate_cargo_create_step_two') );
if ($this->form_validation->run('validate_cargo_create_step_two') === FALSE) {
echo "FALSE";
// die;
$this->load->view('_layouts/main/template1', $this->data);
} else {
echo "TRUE";
// die;
redirect('/Cargo_inquiry/step_three');
}
}
Here is my validation rules located in validate_cargo.php:
$config['validate_cargo_create_step_two'] = array(
'user_id' => array(
'field' => 'user_id',
'label' => '',
'rules' => 'required',
'errors' => array(
'required' => 'Please select at least one company.',
),
),
);
Here is my view/form:
<?php echo form_open('Cargo_inquiry/step_three',array('class'=>'form-horizontal'));?>
<table class="table table-bordered table-striped" id="mytable">
<thead>
<tr>
<th width="3%">Select</th>
<th>company</th>
<th>location</th>
<th>rating</th>
<th>web</th>
<th>Associations/serv_hhgpe</th>
</tr>
</thead>
<tbody>
<?php
foreach ($users as $user) { ?>
<tr>
<td>
<?php // echo form_checkbox('user_id[]', 'user_id[]', set_checkbox("user_id[]"), "Company"); ?>
<input type="checkbox" name="user_id" value="<?php echo $user->id; ?>" />
</td>
<td>
<?php echo $user->company ?>
</td>
<td>
<?php echo $user->city ?>
</td>
<td>
<?php echo $user->state_province ?>
</td>
<td>
<?php echo $user->name ?>
</td>
<td style="text-align:center" width="200px">
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php echo form_submit('cargo_create_step_two', 'Next->', 'class="btn btn-primary btn-lg btn-block"');?>
<?php echo form_close();?>
Arrrgg!!! I was submitting to the wrong form :( So very sorry.

Retrieve data from checkboxes

I want to get all the checked data from my table. I'm using checkboxes in my table. When I click "change status", it will change my role status.
But I have problems with retrieving the checkboxes values. In the code below, it failed to update my data. Checkboxes value that I retrieve is NULL. How can I solve this problem?
Model
function deaktifRole($id_role,$editBy)
{
$data = array(
'update' =>date('Y-m-d H:i:s'),
'updateBy' =>$editBy,
'flag' => '0'
);
$this->db->where('id_role',$id_role);
$this->db->update('tbl_role',$data);
}
Controller
function deaktifRole()
{
$session_data = $this->session->userdata('logged_in');
$editBy = $session_data['username'];
foreach ($this->input->post['pilih'] as $value) {
$this->Role->deaktifRole($value->value,$editBy);
}
redirect('Home/Role');
echo '<script>alert("Your form was successfully submitted!");</script>';
}
View
<div class="x_panel">
<div class="x_title">
<h2>Manage Staff Role</small></h2>
<?php echo form_open_multipart('Home/deaktifRole');?>
<div align="right">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Add</button>
<button type="submit" class="btn btn-primary">Change Status</button>
</div>
<div class="clearfix"></div>
</div>
<div class="x_content">
<table id="datatable-checkbox" class="table table-striped table-bordered bulk_action">
<thead>
<tr>
<th><input type="checkbox" id="check-all" class="flat"></th>
<th>No</th>
<th>Role</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php $j=0; foreach ($RoleList as $rows)
{
$j++;
?>
<tr>
<td><input type="checkbox" class="flat" name="pilih[]" value="<?php echo $rows['id_role']; ?>"></td>
<td><?php echo $j; ?></td>
<td><?php echo $rows['role']; ?></td>
<td>Aktif</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php echo form_close(); ?>
</div>
</div>
</div>
As $this->input->post('pilih') is just an associative array, you can access it directly without ->value properties. Try use this code :
foreach ($this->input->post('pilih') as $value) {
$this->Role->deaktifRole( $value,$editBy );
}
use $this->input->post('pilih');
to fetch checkbox data.

How to submit multiple rows tabular form data in database using code igniter?

Hello i am creating a school management system and facing a little problem.
The problem is that i am having a complex form containing following fields in a table
I have a table fee structure in which i want to insert data.
having columns
id | classID | sectionID | feetypeID | must | amount | active
1 1 1 1 must 500 yes
2 1 1 2 optional 500 no
so how to create an array such that it uses above form to insert data in table.
Basically i want users to choose feetype they want to have for classes and set their amount and then choose classes they want to apply it for so an entry is created in database for each section and each feetype they select.
I don't know how to make controller and model for this but i have this code of view
View
<form class="form-horizontal" role="form" method="post">
<div class="box-body">
<div class="row">
<div class="col-sm-12">
<h3>Fee Types</h3><br>
<div id="hide-table">
<table id="example1" class="table table-striped table-bordered table-hover dataTable no-footer">
<thead>
<tr>
<th class="col-sm-2"><?=$this->lang->line('slno')?></th>
<th class="col-sm-2"><?=$this->lang->line('feetype')?></th>
<th class="col-sm-2"><?=$this->lang->line('amount')?></th>
<th class="col-sm-2"><?=$this->lang->line('must')?></th>
<th class="col-sm-2"><?=$this->lang->line('active')?></th>
<th class="col-sm-2"><input type="checkbox"> Select All</th>
</tr>
</thead>
<tbody>
<?php if(count($feetypes)) {$i = 1; foreach($feetypes as $feetype) { ?>
<tr>
<td data-title="<?=$this->lang->line('slno')?>">
<?php echo $i; ?>
</td>
<td data-title="<?=$this->lang->line('feetype_name')?>" name="feetypeID" value="<?php echo $feetype->feetypeID;?>">
<?php echo $feetype->feetype; ?>
</td>
<td data-title="<?=$this->lang->line('amount')?>" name="amount">
<input type="text" class="form-control" value="<?php echo $feetype->defaultamount; ?>">
</td>
<td data-title="<?=$this->lang->line('must')?>">
<select class="form-control" name="must">
<option value="MUST">Must</option>
<option value="OPTIONAL">Optional</option>
</select>
</td>
<td data-title="<?=$this->lang->line('active')?>">
<select class="form-control" name="active">
<option value="1">Yes</option>
<option value="0">No</option>
</select>
</td>
<td data-title="<?=$this->lang->line('select')?>">
<input type="checkbox" name="selected">
</td>
</tr>
<?php $i++; }} ?>
</tbody>
</table>
</div>
<div class="row">
<div class="col-sm-8">
<h3> Apply To</h3><br>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<table class="table table-bordered table-striped">
<thead>
<th class="col-sm-3">Class</th>
<th class="col-sm-4">Section</th>
</thead>
<tbody>
<?php
foreach ($categories as $category)
{
?>
<tr>
<td> <input type="checkbox"><span style="font-size:18px;margin-left:10px;"> <?php echo $category->classes; ?></span>
</td>
<td>
<?php
if(!empty($category->subs)) {
foreach ($category->subs as $sub) {
echo "<input type='checkbox'> ".$sub->section.' ' ;
}
}
?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<br><br>
<div class="form-group">
<div class="col-sm-12">
<input type="submit" class="btn btn-success" value="<?=$this->lang->line("add_feestructure")?>" >
</div>
</div>
</div>
</form>
Controller
public function addfeestructure(){
$usertype = $this->session->userdata("usertype");
if($usertype == "Admin" || $usertype == "Accountant"){
$this->data['categories'] = $this->feestructure_m->get_categories();
$this->data['feetypes'] = $this->feestructure_m->get_feetypes();
if($_POST){
}
$this->data['subview'] = "feestructure/addfeestructure";
$this->load->view('_layout_main', $this->data);
}
}
To submit multiple fields of the same name you need to add square brackets to your the name attribute like this; <input type="text" name="fieldName[]">.
Then in your controller you'll use a foreach loop to process the form:
public function submitForm() {
$fieldA = $this->input->post('fieldName'); // this will be an array
for ($i = 0; $i < sizeof($fieldA); $i++) {
$array = array('fieldA' => $fieldA[$i]);
$this->your_model->addRecordToDatabase($tableName, $array);
}
}

Why are my values not matching my wordpress options?

I am working on the settings page for a plugin I am writing. There is a form field that let's the user select which currency symbol they want to use when displaying prices. I have created a function that easily displays select menus and determines which option should be selected based on the option stored in the database. For some reason, nothing is being selected in the select menu of the form.
This is the plugin code for this page...
<?php
function hc_options() {
?>
<div class="wrap">
<h2>HeadChef Settings</h2>
<form method="post" action="options.php">
<?php settings_fields( 'my-cool-plugin-settings-group' ); ?>
<?php do_settings_sections( 'my-cool-plugin-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Currency Format</th>
<td>
<?php echo select_menu('currency_format', array('1' => '$12.50', '2' => '$12,50'), esc_attr( get_option('currency_format'))); ?>
</td>
</tr>
<tr valign="top">
<th scope="row">Currency Character</th>
<td><?php
$options = array(
htmlspecialchars('º') => 'º Generic',
htmlspecialchars('$') => '$ Dollar',
htmlspecialchars('£') => '£ Pound',
htmlspecialchars('¥') => '¥ Yen',
htmlspecialchars('€') => '€ Euro',
htmlspecialchars('₱') => '₱ Peso'
);
echo select_menu('currency_prefix', $options, htmlspecialchars(esc_attr(get_option('currency_prefix')))); ?></td>
</tr>
<tr valign="top">
<th scope="row">Currency Suffix <br><small>(I.E. USD... Leave blank to disable.)</small></th>
<td><input type="text" name="currency_suffix" value="<?php echo esc_attr( get_option('currency_suffix') ); ?>" /></td>
</tr><tr>
<th scope="row">Enable Dish Ratings</th>
<td>
<fieldset><?php echo radio_buttons('Enable Dish Ratings', 'enable_dish_ratings', array('1' => 'Yes', '2' => 'No'), esc_attr(get_option('enable_dish_ratings'))); ?>
</td>
</tr><tr>
<th scope="row">Description Character Limit <small>Leave blank for no limit</small></th>
<td><input type="text" name="description_char_limit" value="<?php echo esc_attr( get_option('description_char_limit') ); ?>"></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php }
function select_menu($title, $options, $selected){
$output = '<select name="'.$title.'">\r\n';
if(count($options) > 0){
foreach($options as $val => $label){
$output .= '<option value="'.$val.'"';
if($val == $selected){
$output .= ' SELECTED';
}
$output .= '>'.$label."</option>\r\n";
}
}
$output .= "</select>\r\n";
return $output;
} ?>
Apparently the esc_attr function was screwing with it and the currency_prefix option was parsed as $ instead of $.

Categories