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.
Related
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
}
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!!
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
Model
$id = $this->session->userdata('id');
$q = $this->db->get_where('shipping_address', array('customer_id' => $id));
if ($q->num_rows == 0)
{
$data['id'] = 'You dont have a shipping address saved.';
} else {
foreach ($q->result() as $row)
{
$data['first'] = $row->firstname;
$data['last'] = $row->lastname;
}
}
return $data;
Controller
$this->load->model('Customer_accounts');
$customer = $this->Customer_accounts->get_customer_info();
$ship = $this->Customer_accounts->shipping_address();
$data = $ship + $customer;
$this->load->view('account_dashboard/personal_information', $data);
View
<?php foreach ($ship as $row) : ?>
<table class="fixCap" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><?php echo $row['firstname'] . $row['lastname']; ?></td>
</tr>
. . .
</tr>
</table>
<?php endforeach; ?>
Var_dump
Is only showing an array with 1 of the the table rows but it should be showing 2 rows which contain the defined customer_id
Problem
Unable to pass the all the db data to the foreach what am I doing wrong?
use:
$data = array_merge($ship, $customer); // they will merged as one array
or you can do it this way if u want them separated
$data = array();
$data['ship'] = $ship;
$data['customer'] = $customer;
//In the view
//ship
foreach($data['ship'] as $ship)
{
//Ship values
}
//customer
foreach($data['customer'] as $customer)
{
//Customer value
}
thx
You are not actually puling the result from database with your code.
try something like this
$ret = $q->get()->result();
foreach($ret as $row)
{
}
The problem was in my Model I replaced the above code with
Model
return $this->db->get_where('shipping_address', array('customer_id' => $id))->result();
And in my
View
#I echoed the vars as objects
$row->firstname
I have a array in controller I have to send it to view and then show the value
The Controller page code
public function showbookedmenu(){
$rowLength=$this->input->post('rowno');
$check=$this->input->post('checkeed');
$j=0;
for($i=1;$i<=$rowLength;$i++)
{
$check=$this->input->post('checkeed'.$i);
if($check== "checked")
{
$orderedItem[$j][$i] = $this->input->post('txtMenuItem'.$i);
$orderedItem[$j][$i+1] = $this->input->post('quantity'.$i);
$orderedItem[$j][$i+2] = $this->input->post('lebPrice'.$i);
$orderedItem[$j][$i+3]= $this->input->post('grandtotal'.$i);
$j++;
}
else{}
}
$data['order']= $orderedItem;
$this->load->view('orderpage', $data);
}
The view page
What will be the code??
First rewrite your controller action like this:
public function showbookedmenu()
{
$rowLength = $this->input->post('rowno');
$orderedItem = array();
for($i = 1; $i <= $rowLength; $i++)
{
$check = $this->input->post('checkeed'.$i);
if($check == "checked")
{
$orderedItem[] = array(
'menu_item' => $this->input->post('txtMenuItem'.$i),
'quantity' => $this->input->post('quantity'.$i),
'leb_price' => $this->input->post('lebPrice'.$i),
'grand_total' => $this->input->post('grandtotal'.$i)
);
}
}
$data['order'] = $orderedItem;
$this->load->view('orderpage', $data);
}
Then in your view you can do this:
<? foreach($order as $order_item): ?>
<? echo $order_item['menu_item']; ?>
<? echo $order_item['quantity']; ?>
<? echo $order_item['leb_price']; ?>
<? echo $order_item['grand_total']; ?>
<? endforeach; ?>
first try to print array value in your view file..
print_r($order);
if it is coming fine..
you can give it on
foreach ($order as $value):
and use it :