how to update multiple value in codeigniter - php

I have input data that I will fill with different values, I do multiple updates
but I have an error which is:
Unknown column 'Array' in 'field list' UPDATE service SET
charges_order = WHERE array id_service = '1'
how to overcome this?
example img
View
<?php $no = 1; foreach ($invoice as $m) { ?>
<tbody id="tbody">
<form class="form-signin" method="post" action="<?php echo base_url();?>backend/report/update/<?php echo $m->id_service; ?>">
<tr class="deleted">
<td><input type="text" class="form-control" name="charges_order[]" value="<?php echo $m->charges_order;?>"></td>
</tr>
</form>
</tbody>
<div class="box-footer">
<button type="submit" class="btn bg-blue btns-flat margin">Simpan</button>
</div>
<?php } ?>
Controller
public function update($id_service)
{
foreach ($this->input->post('charges_order') as $data) {
$data = array(
'charges_order' => $this->input->post('charges_order')
);
// echo '<pre>', print_r($data);
$this->M_report->update($id_service, $data);
redirect('backend/report');
}
}
Model
public function update($id_service, $data)
{
$this->db->where('id_service', $id_service);
$this->db->update('service', $data);
}

Change your controller to this-
public function update($id_service){
$charges_order = json_encode($this->input->post('charges_order'));
$data = array(
'charges_order' => $charges_order
);
$this->M_report->update($id_service, $data);
redirect('backend/report');
}
This should work for you.

You are in a loop. You must use the variable.
$data = array(
'charges_order' => $data
);

You can convert array into a string and then update the table.
//example
[1,2,3,4] -> "1,2,3,4"
Here is the code
public function update($id_service)
{
$data = array(
'charges_order' => implode(",", $_POST['charges_order'])
);
// echo '<pre>', print_r($data);
$this->M_report->update($id_service, $data);
redirect('backend/report');
}
When you are retrieving, you can reverse the process
public function get_orders($id_service)
{
$this->db->select('charges_order');
$this->db->where('id_service', $id_service);
$result = $this->db->get('service')->result_array();
return explode(",", $result["charges_order"]); //returns an array
}

Related

cakephp 2.9.7 data validation is not working

The problem is very simple but why it is complicating for me.I have written data validation also.But for empty fields also it is accepting the input.please check whether any error in my action.php.
Model/action.php
<?php
App::uses('AppModel', 'Model');
class Actions extends AppModel {
public $validate = array(
'value_to_plot' => array(
'required'=>true,
'message' => 'atleast select one measure'
),
'column_name' => array(
'required'=>true,
'rule'=>array('notBlank'),
'message' => 'atleast select one table'
)
);
}
?>
View/Actions/index.ctp
<div align="center">
<fieldset>
<?php echo $this->Form->create('valueToSend',array('type' => 'get'));?>
<?php if(isset($_GET['table_name'])){ ?>
<table class="table table-bordered table-hover table-striped">
<?php echo $this->Form->hidden('table_name', array('hiddenField' => true, 'value'=> $_GET['table_name'],'id'=>'table_name'));
echo $this->Form->hidden('chart', array('hiddenField' => true, 'value'=> "column3d",'id'=>"chart"));
?>
<tr>
<th>MEASURES</th>
<td>
<?php echo $this->Form->select('value_to_plot',$measures1,array('class'=>'form-control','id'=>'measures','required'=>true),['empty' => 'choose one']);?>
</td>
</tr>
<tr>
<th>DIMENSIONS</th>
<td>
<?php echo $this->Form->select('column_name[]',$measures2,array('multiple'=>'true','class'=>'form-control','id'=>'dimensions','required'=>true),['empty' =>'choose one']);?>
</td>
</tr>
</table>
<div style="text-align:center">
<?php echo $this->Form->end('submit'); ?>
</div>
<?php } ?>
</fieldset>
</div>
Controller/ActionsController.php
<?php
App::uses('AppController', 'Controller');
class ActionsController extends AppController {
public function beforeFilter() {
if(!isset($_SESSION['Auth']) && empty($_SESSION['Auth'])) {
$userId = $_SESSION['Auth[User[id]]'];
return $this->redirect(array('controller' => 'Users', 'action' => 'login'));
}
}
public Function index(){
App::import('Model', 'ConnectionManager');
$con = new ConnectionManager;
$cn = $con->getDataSource('default');
$tablequery="SELECT TABLE_NAME as table_name
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='phygital_visualize' AND TABLE_NAME != 'report'";
$rows = $cn->query($tablequery);
//$rows = $result->fetchAll('assoc');
$dataToTable = [];
foreach ($rows as $row) {
$dataToTable[$row['TABLES']['table_name']] = $row['TABLES']['table_name'];
}
$this->set('table',$dataToTable);
if(isset($_GET['table_name'])){
$table_name = $_GET['table_name'];
$column = "select column_name as name from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='$table_name' ";
$result = $cn->query($column) ;
foreach ($result as $key => $value) {
foreach($value as $key => $value) {
foreach($value as $key =>$value){
$column_counts[$value] = $value;
}
}
}
$column_counts = array_unique($column_counts);
//print_r($column_counts);
$measures1=array();
$measures2=array();
$diff=array();
foreach($column_counts as $key => $value){
$sql="select * from $table_name where concat('',$value * 1 ) = $value limit 1";
$resultset = $cn->query($sql) ;
if(!empty($resultset)){
if(!in_array($value, $measures1)){
$measures1[$value]= $value;
}
}
}
$measures2 = array_diff($column_counts,$measures1);
$this->set('measures1',$measures1);
$this->set('measures2',$measures2);
}
}
}
?>
This will validate and save your data
if ($this->request->is('post')) {
$this->Actions->set($this->request->data);
if ($this->Actions->save($this->request->data)) {
// Set a session flash message and redirect.
}
}
I think you're confusing "'required'=>true" with "'allowEmpty' => true". The former means that you cannot save the record without that field being included in the list of fields updated, no matter whether it holds actual data or not. The latter, which I think is what you really mean, allows a field to be empty although it doesn't force a declaration.

Update function code igniter does not save changes

hi am making this edit function in my code igniter hmvc project. i am getting the value of the things i want to edit, and it is posted to the textbox i want to. but, i cannot save the changes. my model is this,
public function update(){
$sql = "UPDATE $this->table SET PROVINCE = ? WHERE PROV_ID = ?";
$input = array(
'PROVINCE' => $this->input->post('PROVINCE'),
'PROV_ID' =>$this->uri->segment(3)
);
// print_r($input);
// exit;
$query = $this->db->query($sql, $input);
return $query;
}
when i print_r the input, it says
Array ( [PROVINCE] => Province [PROV_ID] => )
i think dont get the uri value. how can i fix this?
here is my controller
/// EDIT
public function update(){
$data['content'] = $this->Provinces_Model->getrow();
$data['content_view'] = 'Provinces/edit_view';
$this->templates->admin_template($data);
}
public function update_row(){
if($this->Provinces_Model->update()){
redirect('Provinces/index');
}else{
$this->update();
}
}
}
here is my full model
//// EDIT
public function getrow(){
$this->db->where('PROV_ID', $this->uri->segment(3));
$query = $this->db->get($this->table);
return $query->row();
}
public function update(){
$sql = "UPDATE $this->table SET PROVINCE = ? WHERE PROV_ID = ?";
$input = array(
'PROVINCE' => $this->input->post('PROVINCE'),
'PROV_ID' =>$this->uri->segment(3)
);
// print_r($input);
// exit;
$query = $this->db->query($sql, $input);
return $query;
}
here is my edit view
<?php
echo form_open('Provinces/update_row');
?>
<p>
<label class="field" for="PROVINCE"><span>*</span>Province Name:</label>
<input type = "text" name="PROVINCE" class ="textbox-300" value= "<?php echo $content->PROVINCE; ?>">
<label class = "error"><?php echo form_error("PROVINCE"); ?></label>
</p>
<?php
echo form_submit('submit','Update');
echo form_close();
?>
here is the part of my table where i click to edit
<td><?php echo anchor("Provinces/update/$i->PROV_ID","<i class='fa fa-edit'>" ); ?></td>
Try this
public function update() {
$input = array(
'PROVINCE' => $this->input->post('PROVINCE'),
'PROV_ID' =>$this->uri->segment(3)
);
$this->db->where('prov_id',$input['PROV_ID']);
$this->db->update('province',$input);
}
get the all segments of URI in an array
For that use this line...
$segs = $this->uri->segment_array();
foreach ($segs as $segment)
{
echo $segment;
echo '<br />';
}
and use that specific key to get the ID or something else!!

Looping in array (php codeigniter)

The problem is looping in $t array on controller page
Any Solution?
Here's my code
Controller:
function save($id)
{
$i=0;
$t = array(
for($i;$i<=34;$i++)
'j'.$i => $this->input->post('j'.$i),
'status' => $this->input->post('1')
);
$this->mmeeting->save($id,$t);
redirect('admin/meeting','refresh');
}
Model:
function save($t){
$this->db->insert("meeting", $t);
return $this->db->insert_id();
}
View:
<?
$i=1;
foreach ($im as $row):
$i++;
?>
<input name="j.<? echo $i; ?>" type="hidden" value="<? echo $row['when'] ?">>
<? endforeach; ?>
<input type="submit" value="register" tabindex="7" />
Meeting table in database
CREATE TABLE IF NOT EXISTS `meeting`
(
`j1` date ,
`j2` date ,
....
`j34` date ,
status int(1),
)ENGINE=InnoDB DEFAULT CHARSET=armscii8;
This has nothing to do with codeigniter, just php. The following is not valid:
$t = array(
for($i;$i<=34;$i++)
'j'.$i => $this->input->post('j'.$i),
'status' => $this->input->post('1')
);
Use this instead:
$t = array('status' => $this->input->post('1'));
for ($i; $i <= 34; $i++) {
$t["j$i"] = $this->input->post("j$i");
}
If i understood correctly what you want
function save($id)
{
$i=0;
$t = array();
for($i;$i<=34;$i++)
$t["j$i"] = $this->input->post("j$i");
$t['status'] => $this->input->post('1');
$this->mmeeting->save($id,$t);
redirect('admin/meeting','refresh');
}

codeigniter array and loop to insert for multiple data

I want to make an invoice form. For this reason i need to insert multiple data at once. for example I want to input db 5 category sale or purchase product at a time. But I am not interested with insert_batch. I try but i got some null value in db.
My Model is:
<?php
class Purchase_model extends CI_Model{
public function purchase(){
$price = $this->input->post('price');
$quantity = $this->input->post('quantity');
$date = $this->input->post('date');
$vendor_name = $this->input->post('vendor_name');
$model = $this->input->post('model');
$invoice_no = $this->input->post('invoice');
//$temp = count($this->input->post('vendor_name'));
for($i=0; $i<10; $i++){
$data = array(
'date'=>$date[$i],
'vendor_name'=>$vendor_name[$i],
'model'=>$model[$i],
'price' =>$price[$i],
'purchase_quantity'=>$quantity[$i],
'amount' =>$price[$i]*$quantity[$i],
'invoice_no'=>$invoice_no[$i]
);
$insert = $this->db->insert('purchase',$data);
return $insert; }
}}
My controller is:
public function purchase(){
if($this->Purchase_model->purchase()){
$this->session->set_flashdata('Success',
'You are entered data successfully');
redirect('home/purchase_form');
}
}
My view for example:
<?php echo form_label ('Price:'); ?>
<select name="price[]" id="price" class="input-xlarge">
</select>
<?php echo form_label ('Quantity'); ?>
<?php
$data = array ('name' =>'quantity[]',
'class' =>'input-xlarge',
'value' => set_value('quantity')); ?>
<?php echo form_input ($data); ?>
<?php echo form_label ('Price:'); ?>
<select name="price[]" id="price2" class="input-xlarge">
</select>
<?php echo form_label ('Quantity'); ?>
<?php
$data = array ('name' =>'quantity[]',
'class' =>'input-xlarge',
'value' => set_value('quantity')); ?>
<?php echo form_input ($data); ?>
Please help.
The problem has been solved. My mistake in my model. The correct model is
public function purchase(){
$data = array();
$temp = count($this->input->post('quantity'));
for($i=0; $i<$temp; $i++){
$invoice_no = $this->input->post('invoice');
$date = $this->input->post('date');
$price = $this->input->post('price');
$quantity = $this->input->post('quantity');
$vendor_name = $this->input->post('vendor_name');
$model = $this->input->post('model');
if( $quantity[$i]!= '') {
$data[] = array(
'date'=>$date,
'vendor_name'=>$vendor_name[$i],
'model'=>$model[$i],
'price' =>$price[$i],
'purchase_quantity'=>$quantity[$i],
'amount' =>$price[$i]*$quantity[$i],
'invoice_no'=>$invoice_no
);} }
$insert = count($data);
if($insert)
{
$this->db->insert_batch('purchase', $data);
}
return $insert;
}
Thanks every one who try to help me.

Inserting two fields into a database

I am struggling to add data and a category_id in to my database using the MVC architecture.
here is my controller method, which is probably all wrong.
public function create(){
$data['categories'] = $this->get_m->createJoke();
$data = array(
'joke' => $this->input->post('joke'),
'category_id' => $this->input->post('category')
);
$this->get_m->createJoke($data);
$this->load->view('create', $data);
}
Here is my model method:
function createJoke($data){
// Retrieving categories from the database
$categories = $this->db->query('SELECT * FROM category');
$this->db->insert('jokes', $data);
return $categories->result();
}
and finally, this is the form which i want to be able to select a category for a joke:
<?php
echo form_open('home/create');
?>
<p>
<label for="joke">Joke</label>
<input type="text" name="joke" id="joke" />
</p>
<select class="category" name="category">
<option value=0>Select something…</option>
<?php foreach ($categories as $category) { ?>
<option value="<?php echo $category['category_id']; ?>"<?php echo $category_id == $category['category_id'] ? ' selected="selected"' : ''; ?>><?php echo $category['name']; ?></option>
<?php } ?>
</select>
<p>
<input type="button" value="Submit"/>
</p>
<?php echo form_close(); ?>
Before i just had the joke label up (although it did add data in the database), it only added a "0" for some reason.
I have been watching some CRUD tutorials which focus on inserting data, and this is the best i can come up with really!
You didn't check whether form is submitted or not in your controller.
public function create(){
$data['categories'] = $this->get_m->createJoke();
if($this->input->post("joke")!="" and $this->input->post("category")!="") // check post have some value
{
$data = array(
'joke' => $this->input->post('joke'),
'category_id' => $this->input->post('category')
);
$this->get_m->createJoke($data);
}
$this->load->view('create', $data);
}
try this i think this will work
//controller
public function create(){
$data = array(
'joke' => $this->input->post('joke'),
'category_id' => $this->input->post('category')
);
$this->get_m->createJoke($data);
$this->load->view('create', $data);
}
public function selectdata(){
$data['categories'] = $this->get_m->get_joke_categoryid();
}
//model
function createJoke($data){
$this->db->insert('joker', $data);
$this->session->set_flashdata("message", "Contract record successfully created.");
function get_joke_categoryid(){
$query = $this->db->get('joker');
return $query->result();
}
kindly check this if this code help you

Categories