codeigniter update quantity not working - codeigniter

quantity not update only If I add an item with the exact same options more than once to my cart, it replaces instead of increasing the qty of the existing item
This is cart view code
`<table class="table table-bordered table-hover">
<thead ><!-- Table head -->
<tr>
<th class="active">Sl</th>
<th class="active col-sm-4">Product</th>
<th class="active col-sm-2">Real Price</th>
<th class="active ">Qty</th>
<th class="active ">Disc Price</th>
<th class="active">Total</th>
<th class="active">Action</th>
</tr>
</thead><!-- / Table head -->
<tbody><!-- / Table body -->
<?php $cart = $this->cart->contents() ;
?>
<?php $counter =1 ; ?>
<?php if (!empty($cart)): foreach ($cart as $item) : ?>
<tr class="custom-tr">
<td class="vertical-td">
<?php echo $counter ?>
</td>
<td class="vertical-td"><?php echo $item['name'] ?></td>
<td class="vertical-td"><?php echo $item['pkprice'] ?></td>
<td class="vertical-td">
<input type="text" name="qty" style="width: 50px" value="<?php echo $item['qty'] ?>" onblur ="order(this);" id="<?php echo 'qty'.$item['rowid'] ?>" class="form-control">
</td>
<td>
<div class="input-group">
<span class="input-group-addon">
<input type="checkbox" id="<?php echo 'opt'.$item['rowid'] ?>" onclick="return price_checkbox(this)" name="custom_price"
<?php echo $item['price_option'] == 'custom_price' ? 'checked':'' ?>
data-placement="top" data-toggle="tooltip" data-original-title="Custom Price">
</span>
<input type="text" name="price" value="<?php echo $item['price'] ?>" onblur ="order(this);" id="<?php echo 'pri'.$item['rowid'] ?>" class="form-control"
<?php echo $item['price_option'] == 'custom_price' ? '':'disabled' ?> >
</div>
<input type="hidden" name="product_code" value="<?php echo $item['id'] ?>" id="<?php echo 'code'.$item['rowid'] ?>">
</td>
<td class="vertical-td"><?php echo number_format($item['subtotal'], 2, '.', ',') ?></td>
<td class="vertical-td">
<?php echo btn_delete('admin/order/delete_cart_item/' . $item['rowid']); ?>
</td>
</tr>
<?php
$counter++;
endforeach;
?><!--get all sub category if not this empty-->
<?php else : ?> <!--get error message if this empty-->
<td colspan="6">
<strong>There is no record for display</strong>
</td><!--/ get error message if this empty-->
<?php endif; ?>
</tbody><!-- / Table body -->
`
This is Controller Code
public function add_cart_item_by_barcode(){
$product_code = $this->input->post('barcode', true);
$result = $this->order_model->validate_add_cart_item($product_code);
if($result){
$price = $this->check_product_rate($result->product_id, $qty=1);
//product tax check
$tax = $this->product_tax_calculate($result->tax_id, $qty=1, $price);
$data = array(
'id' => $result->product_code,
'qty' => 1,
'price' => $price,
'buying_price' => $result->buying_price,
'name' => $result->product_name,
'pkprice' => $result->p_price,
'tax' => $tax,
'price_option' => 'general'
);
$this->cart->update($data);
$this->session->set_flashdata('cart_msg', 'add');
}
redirect('admin/order/new_order/'.$flag ='add');
}
public function check_product_rate($product_id=null, $qty=null)
{
//tier Price check
$tire_price = $this->order_model->get_tire_price($product_id, $qty);
if($tire_price)
{
return $price = $tire_price->tier_price ;
}
//special offer check
$this->tbl_special_offer('special_offer_id');
$offer_price = $this->global_model->get_by(array("product_id"=>$product_id), true);
if(!empty($offer_price)) {
$today = strtotime(date('Y-m-d'));
$start_date = strtotime($offer_price->start_date);
$end_date = strtotime($offer_price->end_date);
if (($today >= $start_date) && ($today <= $end_date)) {
return $price = $offer_price->offer_price;
}
}
//return regular rate
$this->tbl_product_price('product_price_id');
$general_price = $this->global_model->get_by(array("product_id"=>$product_id), true);
return $product_price = $general_price->selling_price;
}
/*** Product tax calculation ***/
public function product_tax_calculate($tax_id, $qty ,$price)
{
$this->tbl_tax('tax_id');
$tax = $this->global_model->get_by(array('tax_id'=>$tax_id), true);
//1 = tax in %
//2 = Fixed tax Rate
if($tax){
if($tax->tax_type == 1)
{
$subtotal = $price * $qty;
$product_tax = $tax->tax_rate * ($subtotal / 100);
//return $result = round($product_tax, 2);
return $result = $product_tax;
}else
{
//$product_tax = $tax->tax_rate * $qty;
$product_tax = $tax->tax_rate * $qty;
return $result = $product_tax;
}
}
}
/*** Update Product Cart ***/
public function update_cart_item()
{
$rowid = $this->input->post('rowid');
$qty = $this->input->post('qty');
$product_price = $this->input->post('price');
$product_code = $this->input->post('product_code');
$custom_price = $this->input->post('custom_price');
if($qty !=0 )
{
//tbl product
$this->tbl_product('product_id');
$result = $this->global_model->get_by(array('product_code'=> $product_code ), true);
//product Inventory Check
$this->tbl_inventory('inventory_id');
$product_inventory = $this->global_model->get_by(array('product_id'=> $result->product_id ), true);
if($qty > $product_inventory->product_quantity)
{
$type = 'error';
$message = 'Sorry! This product has not enough stock.';
set_message($type, $message);
echo 'false';
return;
}
if($custom_price == "on")
{
$price = $product_price;
$price_option = 'custom_price';
}
else
{
//product price check
$price = $this->check_product_rate($result->product_id, $qty);
$price_option = 'general';
}
//product tax check
$tax = $this->product_tax_calculate($result->tax_id, $qty, $price);
$data = array(
'rowid' => $rowid,
'qty' => $qty,
'price' => $price,
'tax' => $tax,
'price_option' => $price_option
);
}else
{
$data = array(
'rowid' => $rowid,
'qty' => $qty,
);
}
$this->cart->update($data);
if($this->input->post('ajax') != '1'){
redirect('admin/order/new_order'); // If javascript is not enabled, reload the page with new data
}else{
echo 'true'; // If javascript is enabled, return true, so the cart gets updated
}
}
/*** Show cart ***/
function show_cart(){
$this->load->view('admin/order/cart/cart');
}
/*** cart Summery ***/
function show_cart_summary(){
$this->load->view('admin/order/cart/cart_summary');
}
/*** Delete Cart Item ***/
public function delete_cart_item($id)
{
$data = array(
'rowid' => $id,
'qty' => 0,
);
$this->cart->update($data);
$this->session->set_flashdata('cart_msg', 'delete');
redirect('admin/order/new_order/'.$flag ='delete');
}
This is working fine when add product or change its quantity all perfect working but If I add an item with the exact same options more than once to my cart, it replaces instead of increasing the qty of the existing item

Have you tried this?
<?php foreach ($this->cart->contents() as $items): ?>
<?php echo form_hidden($counter.'[rowid]', $items['rowid']); ?> // write this
and then this
<td><?php echo form_input(array('name' => $counter.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?></td>
instead of-
<?php echo form_input(array('name' =>'rowid1[]', 'type'=>'text', 'value' => $items['rowid'], 'maxlength' => '3', 'size' => '5')); ?>
I hope this works

Check what query is generated when update query is fire, i think any signal quote is added in your query.
for check the last database query use this function:-
print_r($this->db->last_query());

After a lot of struggled i get my code correctly here is the correct code of insert and update
function add_cart_item_by_barcode(){
$product_code = $this->input->post('barcode', true);
$result = $this->order_model->validate_add_cart_item($product_code);
$rowid = $this->input->post('rowid');
$cart = $this->cart->contents();
foreach ($cart as $cart) {
if($product_code == $cart['id']){
$rowid=$cart['rowid'];
$qty=$cart['qty'];
$data=array(
'rowid'=>$rowid,
'qty'=>$qty+1
);
$data=$this->cart->update($data);
$this->session->set_flashdata('cart_msg', 'add');
redirect('admin/order/new_order/'.$flag ='add');
}
}
if ($result) {
// update rate
$price = $this->check_product_rate($result->product_id, $qty=1);
//product tax check
$tax = $this->product_tax_calculate($result->tax_id, $qty=1, $price);
$data = array(
'id' => $result->product_code,
'qty' => $qty,
'price' => $price,
'buying_price' => $result->buying_price,
'name' => $result->product_name,
'pkprice' => $result->p_price,
'tax' => $tax,
'price_option' => 'general'
);
$this->cart->insert($data);
$this->session->set_flashdata('cart_msg', 'add');
}
redirect('admin/order/new_order/'.$flag ='add');
}

Related

Photo don't display in shop cart LARAVEL 6

i'm using laravel in my project , so when i add a product in the shop cart all the data is displayed except the product image.
This is the cartcontroller.php:
public function add(Request $request) {
$produit=productmodel::find($request->id);
Cart::add(array(
'id' =>$request->id, // inique row ID
'name' =>$request->product_name,
'price' => $request->product_price,
'quantity' =>$request->product_quantity,
'attributes' => array('photo'=>$request->product_image)));
return redirect ('shop-cart');
}
and this is the shop-cart.blade.php
<tbody>
#foreach(\Cart::getContent() as $item)
<tr>
<td class="cart__product__item">
<div class="cart__product__item__title">
<img src="{{asset('storage/product/September2020/'.$item->attributes['photo'])}}" alt="">
<h6> {{Str::words($item->name,20) }}</h6>
#foreach($item->attributes as $key => $value)
<dl class="dlist-inline small">
<dt>{{ ucwords($key) }}: </dt>
<dd>{{ ucwords($value) }}</dd>
</dl>
#endforeach
</div>
</td>
<td class="cart__price"> {{$item->price}} TND</td>
<td class="cart__quantity">
{{ $item->quantity }}
</td>
<td class="cart__total"> {{ $item->price * $item->quantity }} TND</td>
<td class="cart__close"><i class="fa fa-times"></i>
</td>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endif
If you are using darryldecode/cart for cart. You can go to your vendor folder and make some slight changes to add method of Cart.php file.
public function add($id, $name = null, $price = null, $quantity = null, $image = null, $attributes = array(), $conditions = array(), $associatedModel = null)
{
// if the first argument is an array,
// we will need to call add again
if (is_array($id)) {
// the first argument is an array, now we will need to check if it is a multi dimensional
// array, if so, we will iterate through each item and call add again
if (Helpers::isMultiArray($id)) {
foreach ($id as $item) {
$this->add(
$item['id'],
$item['name'],
$item['price'],
$item['quantity'],
$item['image'],
Helpers::issetAndHasValueOrAssignDefault($item['attributes'], array()),
Helpers::issetAndHasValueOrAssignDefault($item['conditions'], array()),
Helpers::issetAndHasValueOrAssignDefault($item['associatedModel'], null)
);
}
} else {
$this->add(
$id['id'],
$id['name'],
$id['price'],
$id['quantity'],
$id['image'],
Helpers::issetAndHasValueOrAssignDefault($id['attributes'], array()),
Helpers::issetAndHasValueOrAssignDefault($id['conditions'], array()),
Helpers::issetAndHasValueOrAssignDefault($id['associatedModel'], null)
);
}
return $this;
}
$data = array(
'id' => $id,
'name' => $name,
'price' => Helpers::normalizePrice($price),
'quantity' => $quantity,
'image'=>$image,
'attributes' => new ItemAttributeCollection($attributes),
'conditions' => $conditions
);
if (isset($associatedModel) && $associatedModel != '') {
$data['associatedModel'] = $associatedModel;
}
// validate data
$item = $this->validate($data);
// get the cart
$cart = $this->getContent();
// if the item is already in the cart we will just update it
if ($cart->has($id)) {
$this->update($id, $item);
} else {
$this->addRow($id, $item);
}
$this->currentItemId = $id;
return $this;
}
Now you can simply store image in cart as below
$userId = auth()->user()->id;
\Cart::session($userId)->add(array(
'id' => $request->id,
'name' =>$request->item_name,
'price' =>$request->item_price,
'quantity' => $request->quantity,
'image'=>$request->image,
'attributes' => array(),
));
And view your stored image from path like
#foreach(Cart::session(auth()->user()->id)->getContent() as $items)
<div class="row pt-5">
<div class="col-md-3 offset-md-2">
<img class="card-img-top" src="{{asset('photos').'/'.$items->image}}"
style="height:120px; width:120px;"alt="Card image cap">
</div>
<div class="col-md-6 ">
<h5 class="font-weight-bold">{{$items->name}}</h5>
Rate: Rs {{$items->price}}<br>
Qty: {{$items->quantity}}<br>
<?php
$price="";
$price=$items->quantity*$items->price;
?>
Price: Rs {{$price}}<br>
<button class="btn-sm btn-outline-danger"><i class="far fa-trash-alt"></i></button>
</div>
</div>
<hr>
#endforeach

fetch email from id and send mail to that particular user in codeigniter

i have one view candidates form in my application ..in that i display all the candidates details and i display user_id also..i gave on button called send in the view page..
so what i am trying is when i click on the button i need to fetch the user email and send mail to that user..
view code:
<section class="content">
<div class="row">
<div class="col-xs-12">
<!-- /.box-header -->
<div class="box-body">
<table id="example2" class="table table-bordered table-hover">
<thead>
<tr>
<th></th>
<th>Vendor</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Mobile Number</th>
<th>experience</th>
<th>CTC</th>
<th>Expected Ctc</th>
<th>role</th>
<th>Current Location</th>
<th>Desired Location</th>
<th>Notice Period</th>
<th>Resume</th>
<th>Actions</th>
</tr>
</thead>
<?php
foreach ($view_candidates as $idata)
{
?>
<tbody>
<tr id="domain<?php echo $idata->user_id;?>">
<td class="cell checkbox">
<input type="checkbox" class="selectedId" name="selectedId" />
</td>
<td><?php echo $idata->user_id;?></td>
<td><?php echo $idata->first_name;?></td>
<td><?php echo $idata->last_name;?></td>
<td><?php echo $idata->email;?></td>
<td><?php echo $idata->mobile_number;?></td>
<td><?php echo $idata->experience;?></td>
<td><?php echo $idata->ctc;?></td>
<td><?php echo $idata->expectedctc;?></td>
<td><?php echo $idata->role_name;?></td>
<td><?php echo $idata->current_location;?></td>
<td><?php echo $idata->desired_location;?></td>
<td><?php echo $idata->notice_period;?></td>
<td><?php echo $idata->resume;?></td>
<td><button id="<?php echo $idata->candidate_id; ?>" name="button" onClick="CallFunction(this.id)" class="btn btn-info">send</button></td>
</tr>
<?php
}
?>
</tbody>
Controller:
public function change_status()
{
$candidate_id = $this->input->post('candidate_id');
$this->CandidateModel->update_status($candidate_id);
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://md-in-42.webhostbox.net',
'smtp_port' => 465,
'smtp_user' => 'test3#clozloop.com',
'smtp_pass' => 'test3'
);
$this->load->library('email',$config);
$this->email->set_mailtype("html");
$this->email->from('test3#clozloop.com', 'bharathi');
$this->email->to('someone#gmail.com');
$this->email->subject('Request for contact info');
$link = 'Click on this link - Click Here';
$this->email->message($link);
if($this->email->send())
{
echo "email send";
}
else
{
echo "failed";
}
// echo true;
// exit;
}
Model:
function update_status($candidate_id)
{
$this->db->select('*');
$this->db->from('candidates_details');
$status = $this->db->query("update candidates_details set status='1' where candidate_id ='$candidate_id'");
$data=array('status'=>$status);
$this->db->where('candidate_id',$candidate_id);
//$this->db->update('candidates_details',$data);
//$query=$this->db->get();
echo $this->db->last_query();
//return $query->result();
}
Can anyone help me how to send mail to that user..
Thanks in advance..
Do something like below:
<script>
function CallFunction(id){
var url = '127.0.0.1/job_portal/index.php/Candidate/change_status/'‌​;;
$.ajax({
url: url,
type: 'POST',
data: {
candidate_id: id
},
dataType: 'JSON',
success: function(data) {
if(data == 1) {
$('.button').text('FINISHED');
} else {
$('.button').text('TRY AGAIN!');
}
}
});
}
</script>
Controller and Model:
<?php
// controller
function change_status()
{
$data = $_POST;
$d = $this->user_model->send_email($data['candidate_id']);
echo json_encode($d);
}
// model
function send_email($candidate_id)
{
$q = $this->db->query("SELECT u.email_id as email_id FROM tbl_candidate as c, tbl_user as u WHERE c.candidate_id = $candidate_id AND c.user_id = u.user_id");
if($q->num_rows() > 0) {
$d = $q->row_array();
$to = $d['email_id'];
$config = Array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://md-in-42.webhostbox.net',
'smtp_port' => 465,
'smtp_user' => 'test3#clozloop.com',
'smtp_pass' => 'test3'
);
$this->load->library('email',$config);
$this->email->set_mailtype("html");
$this->email->from('test3#clozloop.com', 'bharathi');
$this->email->to($to);
$this->email->subject('Request for contact info');
$link = 'Click on this link - Click Here';
$this->email->message($link);
if($this->email->send())
{
return 1;
}
else
{
return 0;
}
} else {
return 0;
}
}
?>

CodeIgniter insert into two tables

This is my model and i have problem when i go create "pjesma", i choose "izvodjac" in the form, and how to implement this with codeigniter3, i have to insert values into "izvodi_pjesmu", from where do i get "pjesma_id" that i am just inserting, i hope its clear, if you see the EER diagram below you will understand i hope. Thank you.
diagram
controller method:
public function kreiraj()
{
if (isset($_POST['kreiraj']))
{
$data1 = array(
'naslov' => $this->input->post('naslov'),
'tablatura' => $this->input->post('tablatura'),
'korisnik_korisnik_id' => $_SESSION['korisnik_id']
);
$data2 = array(
'izvodjac_izvodjac_id' => $this->input->post('izvodjaci')
);
$this->form_validation->set_rules('izvodjaci','Izvođači','required|callback_check_default');
$this->form_validation->set_message('check_default', 'Morate odabrati izvođača');
$this->form_validation->set_rules('naslov', 'Naslov', 'required', array('required' => 'Naslov je obavezan'));
$this->form_validation->set_rules('tablatura', 'Tablatura', 'required', array('required' => 'Tablatura je obavezna'));
if ($this->form_validation->run() != FALSE)
{
$this->pjesma_model->kreiraj($data1, $data2);
redirect('pjesma', 'location');
}
}
$data['izvodjaci'] = $this->izvodjac_model->svi_izvodjaci();
$this->load->view('zaglavlje');
$this->load->view('pjesma_kreiraj', $data);
$this->load->view('podnozje');
}
model method:
public function kreiraj($data1, $data2)
{
$this->db->insert('izvodi_pjesmu', $data2);
$this->db->insert('pjesma', $data1);
}
view:
<h1>Kreiranje pjesme</h1>
<hr>
<div class="row">
<div class="col"></div>
<div class="col-9">
<?php if (validation_errors()): ?>
<div class="alert alert-info">
<?php echo validation_errors(); ?>
</div>
<?php endif; ?>
<?php echo form_open('pjesma/kreiraj'); ?>
<div class="form-group">
<label for="izvodjaci">Izvođač:</label>
<select class="form-control" name="izvodjaci" id="izvodjaci">
<option value="0">odabir izvođača</option>
<?php foreach($izvodjaci as $izvodjac): ?>
<option value="<?php echo $izvodjac->izvodjac_id; ?>"><?php echo $izvodjac->naziv; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="naslov">Naslov:</label>
<input type="text" class="form-control" id="naslov" name="naslov" value="<?php echo set_value('naslov'); ?>" />
</div>
<div class="form-group">
<label for="tablatura">Tablatura:</label>
<textarea class="form-control" id="tablatura" name="tablatura"></textarea>
</div>
<button type="submit" class="btn btn-default" name="kreiraj">Kreiraj pjesmu</button>
<?php echo form_close(); ?>
</div>
<div class="col"></div>
Try this:
Controller:
public function kreiraj()
{
if (isset($_POST['kreiraj']))
{
$data1 = array(
'naslov' => $this->input->post('naslov'),
'tablatura' => $this->input->post('tablatura'),
'korisnik_korisnik_id' => $_SESSION['korisnik_id']
);
$this->form_validation->set_rules('izvodjaci','Izvođači','required|callback_check_default');
$this->form_validation->set_message('check_default', 'Morate odabrati izvođača');
$this->form_validation->set_rules('naslov', 'Naslov', 'required', array('required' => 'Naslov je obavezan'));
$this->form_validation->set_rules('tablatura', 'Tablatura', 'required', array('required' => 'Tablatura je obavezna'));
if ($this->form_validation->run() != FALSE)
{
$id= $this->pjesma_model->kreiraj($data1);
$data2 = array(
'izvodjac_izvodjac_id' => $this->input->post('izvodjaci'),
'pjesma_pjesma_id' => $id
);
$this->pjesma_model->pjesma_pjesmu($data2);
redirect('pjesma', 'location');
}
}
$data['izvodjaci'] = $this->izvodjac_model->svi_izvodjaci();
$this->load->view('zaglavlje');
$this->load->view('pjesma_kreiraj', $data);
$this->load->view('podnozje');
}
Model:
public function kreiraj($data1)
{
$this->db->insert('pjesma', $data1);
return $this->db->insert_id();
}
public function pjesma_pjesmu($data)
{
$this->db->insert('izvodi_pjesmu', $data);
return $this->db->insert_id();
}
You can use $this->db->insert_id();
Which will return the last inserted id. Add this line after inserting data in pjesma table which will return 'pjesma_id'. Then you can use it.
Like this -
public function kreiraj($data1, $data2)
{
$this->db->insert('pjesma', $data1);
// ge tlast inserted id
$pjesma_id = $this->db->insert_id();
$izvodjac_izvodjac_id = $data2['izvodjac_izvodjac_id'];
$data = array( 'pjesma_id' => $pjesma_id,
'izvodjac_izvodjac_id' => $izvodjac_izvodjac_id );
$this->db->insert('izvodi_pjesmu', $data);
}
You can do this thing like this, may be this will helps you,
$insert = array(
'naslov' => $this->input->post('naslov'),
'tablatura' => $this->input->post('tablatura'),
'korisnik_korisnik_id' => $_SESSION['korisnik_id']
);
$this->db->insert('tabl1', $insert);
$output["insert_id"] = $this->db->insert_id();
if (isset($output["insert_id"])) {
$insert = array(
'field1' => $value1,
'field2' => $value2,
);
$this->db->insert('tabl2', $insert);
}

Saving data from a drop down list in CodeIgniter

I created a menu page where it has a drop down menu with a list of menus from the database and it also has a textbox to enter new menus.
The problem I'm having is that I can't seem to figure out how to save my dropdown. So for example I have a menu called "About Us" in the drop down list and I want to create a new menu called "Team", and "Team" is a child of "About Us"
So in my table I would have something like this
id | parent | title
------------------------
1 | NULL | About Us
2 | 1 | Team
Menu Controller
function get_data_from_post()
{
$data['title'] = $this->input->post('title', TRUE);
$data['parent'] = $this->input->post('parent', TRUE);
if(!isset($data)){
$data = '';
}
return $data;
}
function get_data_from_db($update_id)
{
$query = $this->get_where($update_id);
foreach($query->result() as $row){
$data['title'] = $row->title;
$data['parent'] = $row->parent;
}
return $data;
}
function create()
{
$update_id = $this->uri->segment(3);
$submit = $this->input->post('submit', TRUE);
if($submit == "Submit"){
//person has submitted the form
$data = $this->get_data_from_post();
}else{
if(is_numeric($update_id)){
$data = $this->get_data_from_db($update_id);
}
}
if(!isset($data)){
$data = $this->get_data_from_post();
}
//$titles = array();
$query = $this->get('title');
foreach($query->result() as $row){
$titles[] = $row->title;
}
$data['titles'] = $titles;
$data['update_id'] = $update_id;
$data['view_file'] = "create";
$this->load->module('templates');
$this->templates->admin_template($data);
}
function submit()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'Title', 'required|xss_clean');
if($this->form_validation->run($this) == FALSE){
$this->create();
}else{
$data = $this->get_data_from_post();
$update_id = $this->uri->segment(3);
if(is_numeric($update_id)){
$this->_update($update_id, $data);
}else{
$this->_insert($data);
}
redirect('menus/manage');
}
}
create.php view
<div class="row">
<div class="col-md-12">
<h2>Create Menus</h2>
<h5>Welcome Jhon Deo , Need to make dynamic. </h5>
</div>
</div>
<hr />
<?php
echo validation_errors("<p style='color: red;'>", "</p>");
echo form_open('menus/submit/'.$update_id);
?>
<div class="row">
<div class="col-md-12">
<form role="form">
<div class="form-group">
<select name="menus">
<?php
foreach($titles as $title){
echo "<option value=".$title.">".$title."</option>";
}
?>
</select>
</div>
<div class="form-group">
<label>Title</label>
<!-- <input class="form-control" /> -->
<?php
$data = array(
'name' => 'title',
'id' => 'title',
'value' => $title,
'class' => 'form-control',
);
echo form_input($data);
?>
</div>
<?php
$data = array(
'name' => 'submit',
'id' => 'submit',
'value' => 'Submit',
'class' => 'btn btn-success',
'style' => 'width: 100%',
);
echo form_submit($data);
?>
</form>
</div>
</div>
<?php
echo form_close();
?>
UPDATE:
this is what I have when I print_r($titles)
Array
(
[0] => About Us
[1] => Home
)
If there is anything you don't understand or if you need me to give more information please let me know.
You should have declared a model. From there, you can create a function that will save the values in the database that you initialize via controller. You should utilize the MVC pattern of it. CodeIgniter has a great documentation to read about what I am pointing out.. https://codeigniter.com/user_guide/overview/mvc.html?highlight=model

Insert in to database using codeigniter

HI
i want to insert data using form in CI but i am facing the problem
Here is my model code
function add_form() {
$this->load->database();
$id = $this->input->post('id');
$name = $this->input->post('name');
$age = $this->input->post('age');
$data = array(
'name' => $this->input->post('name'),
'age' => $this->input->post('age'),
);
$this->db->insert('user',$data);
}
Here is my controller code
function simpleform() {
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('welcomedb_model');
if( $this->input->post('submit') ) {
$this->welcomedb_model->add_form();
}
$this->load->view('welcomedb_view');
}
and here is my view code
<?php echo form_open('welcomedb/submit'); ?>
<? echo $name; ?>:
<? echo form_input('name'); ?>
</br>
<? echo $age; ?>:
<? echo form_input('age'); ?>
</br>
<?php echo form_submit('submit', 'Submit'); ?>
<?php echo form_close(); ?>
Thanks for your help
Your form is submitting to welcomedb/submit, but your controller appears to be at welcomedb/simpleform... maybe you need to change that.
Otherwise, there doesn't appear to be anything wrong.
Probably:
$data = array(
'name' => $this->input->post('name'),
'age' => $this->input->post('age'),
);
Remove comma from the last element (age) like this:
$data = array(
'name' => $this->input->post('name'),
'age' => $this->input->post('age')
);
and always dump error.
Best way is to do these code ......
First conifg the autoload and database.
into autoload:$autoload['libraries'] = array('database');
$autoload['helper'] = array('url','form','file');
Into controller
<?php
class CDemo extends CI_Controller
{
function index()
{
$this->load->view('VDemo');
}
function save()
{
$this->load->model('MDemo');
if($this->input->post('submit'))
{
$this->MDemo->process();
}
redirect('CDemo');
}
}
?>
Into Model
<?php
class MDemo extends CI_Model
{
function process()
{
$Id = $this->input->post('Id');
$Name = $this->input->post('Name');
$data = array(
'Id'=>$Id,
'Name'=>$Name
);
$this->db->insert('test',$data);
}
}
?>
Into View
<html>
<head>
<title>DEMO</title>
</head>
<body>
<h1>Form Biodata</h1>
<?php
echo form_open('CDemo/save', array('name' => 'VDemo'));
?>
<table>
<tr>
<td>Id :</td>
<td><input type="text" name="Id"></input></td>
</tr>
<tr>
<td>Name :</td>
<td><input type="text" name="Name"></input></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"></input></td>
</tr>
</table>
<?php
echo form_close();
?>
<textarea rows="" cols="">Hello</textarea>
</body>
</html>

Resources