How to fix cart codeigniter? - codeigniter

CONTROLLER
In my controller I add product like this, cart is redirecting but not adding a product, how can this be fixed?
function tambah()
{
$data_produk= array('id' => $this->input->post('id'),
'nama_produk' => $this->input->post('nama_produk'),
'harga' => $this->input->post('harga'),
'gambar' => $this->input->post('gambar'),
//'seller' => $this->input->post('seller'),
'qty' => $this->input->post('qty')
);
//die(print_r($data_produk));
$this->cart->insert($data_produk);
$cart = $this->cart->contents();
if(!empty($cart)){
// print_r($cart);
}
redirect('product');
}
VIEW ;
<div class="col-md-8">
<div class="product_list">
<div class="row">
<?php foreach($shopall as $row){ ?>
<div class="col-lg-6 col-sm-6">
<div class="single_product_item">
<img src="assets/img/categori/product1.png" alt="" class="img-fluid">
<div class="card_area">
<form action="<?php echo base_url();?>cart/tambah" method="post" accept-charset="utf-8">
<div class="product_count_area" style="margin:-40PX -35PX 0PX 0PX;">
<p style="text-align:center;">Rp. <?php echo number_format($row['harga'],0,",",".");?></p>
<div class="product_count d-inline-block">
<span class="product_count_item inumber-decrement"> <i class="ti-minus"></i></span>
<input class="product_count_item input-number" name="qty" type="number" value="1" min="1" max="99">
<span class="product_count_item number-increment"> <i class="ti-plus"></i></span>
</div>
</div>
<p><?php echo $row['nama_produk'] ?></p>
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
<input type="hidden" name="nama_produk" value="<?php echo $row['nama_produk']; ?>" />
<input type="hidden" name="harga" value="<?php echo $row['harga']; ?>" />
<input type="hidden" name="gambar" value="<?php echo $row['gambar']; ?>" />
<div style="text-align:center; margin:-20px;" class="add_to_cart">
<!--<button type="submit" class="btn_3">add to cart</button>-->
<button type="submit" class="btn btn-sm btn-success"><i class="glyphicon glyphicon-shopping-cart"></i> Beli</button>
</div>
</form>
</div>
</div>
</div>
<?php } ?>
</div>
<div class="load_more_btn text-center">
Load More
</div>
</div>
</div>
In my last project I could use cart functionality with it, but in this project cart remains empty, can you point me where my code is wrong?

Codeigniter Cart Using English in it array keys, (unless you modified it somehow)
This should work - Notes: Add any extra data you may need to save under options, don't define your own array key. check here for more Codeingiter Cart
$data_produk = array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red')
);
instead of this
$data_produk= array('id' => $this->input->post('id'),
'nama_produk' => $this->input->post('nama_produk'),
'harga' => $this->input->post('harga'),
'gambar' => $this->input->post('gambar'),
//'seller' => $this->input->post('seller'),
'qty' => $this->input->post('qty')
);
Try to map that according to your language

Related

Showing success message but not stored into database laravel

Here is the code for controller:
public function save_product(Request $request){
$request->validate([
'product_name' => 'required',
'product_price' => 'required',
'product_category' => 'required',
'description' => 'nullable',
'image1' => 'required',
'image2' => 'nullable',
'image3' => 'nullable',
], [
'product_name.required' => 'Product name field required',
'product_price.required' => 'Asking price field required',
'product_category.required' => 'Product category field required',
'image1.required' => 'You need a upload image-1 field',
]);
if($request->hasFile('image1') || $request->hasFile('image2') || $request->hasFile('image3')){
$file1 = $request->file('image1');
$file2 = $request->file('image2');
$file3 = $request->file('image3');
$text1 = $file1->getClientOriginalExtension();
$text2 = $file2->getClientOriginalExtension();
$text3 = $file3->getClientOriginalExtension();
$fileName1 = time().'.'.$text1;
$fileName2 = time().'.'.$text2;
$fileName3 = time().'.'.$text3;
$file1->move('uploads/product_image', $fileName1);
$file2->move('uploads/product_image', $fileName2);
$file3->move('uploads/product_image', $fileName3);
$product = Product::create([
'image1'=>$fileName1,
'image2'=>$fileName2,
'image3'=>$fileName3,
'product_category' => trim($request->input('product_category')),
'product_name' => trim($request->input('product_name')),
'product_price' => trim($request->input('product_price')),
'description' => trim($request->input('description')),
]);
}
return redirect()->route('add_product')
->with('success','Successfully added Product!');
}
Here is the code for blade template:
#extends('admin.layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
#include('pages.partials.flash_message')
</div>
</div>
</div>
<div class="card card-default">
<div class="card-header card-header-border-bottom">
<h2>Add Product</h2>
</div>
<div class="card-body">
<form action="{{route('save_product')}}" method="POST">
#csrf
<div class="form-group">
<label for="exampleFormControlSelect12">Select Category</label>
<input type="text" class="form-control" placeholder="Enter Arrival Time"
id="exampleFormControlSelect12" name="product_category" list="product_category"
autocomplete="off">
<datalist class="form-control" id="product_category" style="display: none" >
<option value="Women Clothes"></option>
<option value="Jewellery"></option>
<option value="Shoes"></option>
<option value="Sun Glass"></option>
<option value="Hair Band"></option>
</datalist>
</div>
<div class="form-group">
<label for="inputGroupFile02">Upload Product Image-1</label>
</div>
#if ($errors->has('image1'))
<span class="text-danger" style="font-weight: bold">{{ $errors->first('image1') }}
</span>
#endif
<div class="input-group my-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroupFileAddon01">Upload Image</span>
</div>
<div class="custom-file">
<input type="file" name="image1" class="custom-file-input" id="inputGroupFile01"
aria-describedby="inputGroupFileAddon01">
<label class="custom-file-label" for="inputGroupFile01">Choose file</label>
</div>
</div>
<div class="form-group">
<label for="inputGroupFile02">Upload Product Image-2</label>
<small>*Optional</small>
</div>
#if ($errors->has('image2'))
<span class="text-danger" style="font-weight: bold">{{ $errors->first('image2') }}
</span>
#endif
<div class="input-group my-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroupFileAddon01">Upload Image</span>
</div>
<div class="custom-file">
<input type="file" name="image2" class="custom-file-input" id="inputGroupFile02"
aria-describedby="inputGroupFileAddon01">
<label class="custom-file-label" for="inputGroupFile01">Choose file</label>
</div>
</div>
<div class="form-group">
<label for="inputGroupFile02">Upload Product Image-3</label>
<small>*Optional</small>
</div>
#if ($errors->has('image3'))
<span class="text-danger" style="font-weight: bold">{{ $errors->first('image3') }}
</span>
#endif
<div class="input-group my-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroupFileAddon01">Upload Image</span>
</div>
<div class="custom-file">
<input type="file" name="image3" class="custom-file-input" id="inputGroupFile03"
aria-describedby="inputGroupFileAddon01">
<label class="custom-file-label" for="inputGroupFile01">Choose file</label>
</div>
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Product Name or Title</label>
<input type="text" name="product_name" class="form-control"
id="exampleFormControlInput1" placeholder="Enter Product Name or Title">
#if ($errors->has('product_name'))
<span class="text-danger" style="font-weight: bold">{{ $errors-
>first('product_name') }}</span>
#endif
</div>
<div class="form-group">
<label for="exampleFormControlInput2">Asking Price</label>
<input type="number" name="product_price" class="form-control"
id="exampleFormControlInput2" placeholder="Enter Asking Price">
#if ($errors->has('product_price'))
<span class="text-danger" style="font-weight: bold">{{ $errors-
>first('product_price') }}</span>
#endif
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1">Product Description</label>
<textarea class="form-control" name="product_description"
id="exampleFormControlTextarea1" rows="3"></textarea>
</div>
<div class="form-footer pt-4 pt-5 mt-4 border-top">
<button type="submit" class="btn btn-primary btn-default">Submit</button>
</div>
</form>
</div>
</div>
#endsection
Route:
Route::post('/admin/add_product','App\Http\Controllers\Admin\AdminController#save_product')
->name('save_product')->middleware('admin');
Model:
protected $fillable = [
'product_name',
'product_category',
'product_price',
'description',
'image1',
'image2',
'image3',
];
Migration:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('product_name');
$table->string('product_category');
$table->string('product_price');
$table->text('description')->nullable();
$table->string('image1');
$table->string('image2')->nullable();
$table->string('image3')->nullable();
$table->timestamps();
});
}
I can't find any error in my code please help me to find out. Thanks in advance.
You have to make separate if conditions for each image. Try this:
$fileName1='';
$fileName2='';
$fileName3='';
if($request->hasFile('image1')) {
$file1 = $request->file('image1');
$text1 = $file1->getClientOriginalExtension();
$fileName1 = time() . '.' . $text1;
$file1->move('uploads/product_image', $fileName1);
}
if($request->hasFile('image2')) {
$file2 = $request->file('image2');
$text2 = $file2->getClientOriginalExtension();
$fileName2 = time() . '.' . $text2;
$file2->move('uploads/product_image', $fileName2);
}
if($request->hasFile('image3')) {
$file3 = $request->file('image3');
$text3 = $file3->getClientOriginalExtension();
$fileName3 = time() . '.' . $text3;
$file3->move('uploads/product_image', $fileName3);
}
$product = Product::create([
'image1'=>$fileName1,
'image2'=>$fileName2,
'image3'=>$fileName3,
'product_category' => trim($request->input('product_category')),
'product_name' => trim($request->input('product_name')),
'product_price' => trim($request->input('product_price')),
'description' => trim($request->input('description')),
]);
if($product){
return redirect()->route('add_product')->with('success','Successfully added Product!');
}
else{
return redirect()->back()->with('error','Error!');
}
using enctype="multipart/form-data" inside form tag .

Getting the wrong route instead of declared one in laravel to upload a file

I am trying to upload a file. I did as usual but when I click the submit button it's not getting to the post route instead the page is just refreshing. Is there anything wrong with my filecontroller? besides the method which I am applying for uploading a file, is it correct?
add_file.blade.php:
<form class="form-horizontal" action="{{url('upload_file')}}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<fieldset>
<div class="control-group">
<label class="control-label" for="date01">File name</label>
<div class="controls">
<input type="text" class="input-xlarge" name="file_name" required="" >
</div>
</div>
<div class="control-group" >
<label class="control-label" for="selectError3">Department name</label>
<div class="controls" >
<select id="selectError3" name="department_id"style=" width: 200px">
<option>select department</option>
<?php
$all_department=DB::table('dept')
->get();
foreach($all_department as $v_department){?>
<option value="{{$v_department->department_id}}">{{$v_department->department_name}}</option>
<?php } ?>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="selectError3">Session </label>
<div class="controls">
<select id="selectError3" name="session_id">
<option>select Session</option>
<?php
$all_session=DB::table('session')
->get();
foreach($all_session as $v_session){?>
<option value="{{$v_session->session_id}}">{{$v_session->session_name}}</option>
<?php } ?>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="selectError3">Semester</label>
<div class="controls">
<select id="selectError3" name="semester_id">
<option>select semester</option>
<?php
$all_semester=DB::table('semester')
->get();
foreach($all_semester as $v_semester){?>
<option value="{{$v_semester->semester_id}}">{{$v_semester->semester_name}}</option>
<?php } ?>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="selectError3">Course </label>
<div class="controls">
<select id="selectError3" name="course_id">
<option>select course </option>
<?php
$all_course=DB::table('course')
->get();
foreach($all_course as $v_course){?>
<option value="{{$v_course->course_id}}">{{$v_course->course_code}}</option>
<?php } ?>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="fileInput">Upload file</label>
<div class="controls">
<input class="input-file uniform_on" name="file_any" type="file" required="">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Add File</button>
<button type="reset" class="btn">Cancel</button>
</div>
</fieldset>
</form>
fileController#store file:
public function store(Request $request)
{
$this->validate($request,[
'file_name' =>'required',
'file_path' =>'required',
'department_id' => 'required',
'session_id' => 'required',
'semester_id' => 'required',
'course_id' => 'required',
]);
$file = $request->file('file_any');
$name= $file->getClientOriginalName();
$extension= $file->getClientOriginalExtension();
$size = $file->getClientSize();
$newName= $name . '.' . $extension;
$path= Storage::putFileAs('public',$request->file('file_any'),$newName);
$files= File::create([
'file_name' => $newName,
'file_path' => $path,
'department_id' => $request->department_id,
'session_id' => $request->session_id,
'semester_id'=> $request->semester_id,
'user_id' => Auth::id(),
'course_id' => $request->course_id,
'file_ext' => $extension,
'file_size' => $size,
]);
Session::put('message','File added successfully !!!');
return redirect()->back();
}
Routes:
Route::post('upload_file', 'fileController#store');
Route::get('/add_files', 'fileController#create');
Seems that the file_path is required in your validation. You have to add file_path input in your form. Also can you try to add displaying of errors in the view form. e.g. add this on your view form:
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif

How can fixed the codeigniter cart update issue

I am unable to update cart on checkout page. I do not know what is Happen.
Here is my code.
function update_cart()
{
$rowid =$this->input->post('rowid');
$price =$this->input->post('price');
$qty = $this->input->post('qty');
$pr = preg_replace( '/,/', '', $price);
$amount = $pr * $qty;
$data = array(
'rowid' => $rowid,
'price' => $price,
'amount' => $amount,
'qty' => $qty
);
$this->cart->update($data);
redirect('checkout');
}
Here is my car view code .I am trying to update cart value here.No error showing. I unable to solve .Any help .Only update cart is not working . Other functionality is working good .add to cart delete to cart but update to cart does not working . I create session table and also try other suggestion and solution form stack overflow.
<form action="<?=base_url()?>cart/update_cart" method="post">
<div class="panel panel-info">
<div class="panel-heading">
<div class="panel-title">
<div class="row">
<div class="col-xs-6">
<h5><span class="glyphicon glyphicon-shopping-cart"></span> Shopping Cart</h5>
</div>
<div class="col-xs-6">
<a href="<?=base_url()?>">
<button type="button" class="btn btn-primary btn-sm btn-block">
<span class="glyphicon glyphicon-share-alt"></span> Continue shopping
</button>
</a>
</div>
</div>
</div>
</div>
<div class="panel-body">
<?php $i = 1; ?>
<?php
//print_r($this->cart->contents() );
//die;
foreach ($this->cart->contents() as $items): ?>
<?php echo form_hidden($i.'[rowid]', $items['rowid']); ?>
<div class="row">
<div class="col-xs-2">
<img class="img-responsive" src="<?=base_url()?>uploads/product/<?=$items['image'];?>" />
</div>
<div class="col-xs-4">
<h5 class="product-name">
<strong><?php echo $items['name']; ?>
</strong>
<?=$items['options'];?>
</h5>
</div>
<div class="col-xs-6">
<div class="col-xs-6 text-right">
<h6><strong><?= $this->cart->format_number($items['price']); ?>
<span class="text-muted">x</span></strong></h6>
</div>
<div class="col-xs-4">
<?php //echo form_input(array('name' => $i.'[qty]', 'value' => $items['qty'], 'size' => '1')); ?>
<input type="text" name="qty" min="1" value="<?=$items['qty'] ?>" maxlength="1" style="width: 50%;">
<input type="hidden" name="price" value="<?=$items['price'] ?>">
<input type="hidden" name="name" value="<?=$items['name'] ?>">
<input type="hidden" name="description" value="<?=$items['description'] ?>">
</div>
<div class="col-xs-2">
<a href="<?=base_url()?>cart/remove/<?=$items['rowid']?>">
<button type="button" class="btn btn-link btn-xs">
<span class="glyphicon glyphicon-trash" onclick="confirm('Are you sure want to delete')";> </span>
</button>
</a>
</div>
</div>
</div>
<hr>
<?php $i++; ?>
<?php endforeach; ?>
at view .... use only two input
<input type="text" name="qty[]" min="1" value="<?=$items['qty'] ?>" maxlength="1" style="width: 50%;">
<input type="hidden" name="rowid[]" value="<?=$items['rowid'] ?>">
at controller
you only need to change qty if you are going to update a cart
function update_cart()
{
for ($i=0; $i < count($_POST) - 1; $i++) {
$data = array(
'rowid' => $_POST['rowid'][$i],
'qty' => $_POST['qty'][$i]
);
$this->cart->update($data);
}
redirect('checkout');
}

Input Multiple Data Codeigniter

Sorry if this question has been asked before. I would like to ask for help to check my script below. Because when I tried to submit my form with multiple input field it only results in one data, whereas, there should be two data entered into the database.
So, which part of my script is wrong?
Controller
public function add() {
// ... some script before 'else' ...
} else {
$post = $this->input->post();
$result = array();
$total_input = count($post['input_acc_code']);
foreach ($post['input_acc_code'] as $key => $value) {
$result[] = array(
'trans_type' => 'journal',
'form_type' => NULL,
'acc_code' => $post['input_acc_code'][$key],
'acc_type_id' => $post['input_acc_type_id'][$key],
'refference' => '',
'customer_ID' => NULL,
'acc_side' => '',
'debet' => $post['input_debet'][$key],
'credit' => $post['input_credit'][$key],
'summary' => $post['input_note'][$key],
'files' => NULL,
'create_at' => date("Y-m-d H:i:s", strtotime("now"))
);
if($this->model_transaction->savedata('fi_acc_journal', $result) == TRUE) {
$this->session->set_flashdata('alert', 'Success');
redirect(base_url().'admin/transaction');
} else {
$this->session->set_flashdata('alert', 'Failed');
redirect(base_url().'admin/transaction');
}
}
}
}
Model
function savedata($table, $data = array()) {
$this->db->insert_batch($table, $data);
if($this->db->affected_rows() > 0) {
return TRUE;
}
return FALSE;
}
View
<?php $attributes = array('class' => 'form-horizontal', 'id' => '');
echo form_open_multipart(base_url().$this->session->userdata('user_status').'/transaction/add', $attributes);?>
<div class="row">
<div class="col-sm-12 col-md-12 panel-form-input">
<div class="form-group form-group-sm">
<label for="input_datetime" class="col-sm-2 control-label">Tanggal Transaksi</label>
<div class="col-sm-10">
<input type="text" class="input-date form-control" name="input_datetime[]" id="input-date">
<?php echo form_error('input_datetime');?>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12 col-md-12 panel-form-input">
<div class="panel panel-default">
<div class="table-responsive">
<table class="table table-unbordered">
<thead>
<th class="col-25">Account</th>
<th class="col-5">Account Type</th>
<th class="col-35">Notes</th>
<th class="col-15">Debet</th>
<th class="col-15">Credit</th>
<th class="col-5"></th>
</thead>
<tbody>
// First Input Field Form Table
<tr>
<td class="col-25">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<select class="select-transaction input-group-sm form-control" name="input_acc_code[]" id="acc_code_1">
<?php if ($account_list != NULL): ?>
<option>— Choose Account Number —</option>
<?php foreach ($account_list as $value): ?>
<option value="<?php echo $value->acc_code;?>"><?php echo $value->acc_name;?></option>
<?php endforeach;?>
<?php else:?>
<option>— No Data —</option>
<?php endif;?>
</select>
<?php echo form_error('input_acc_code[]');?>
</div>
</div>
</td>
<td class="col-5">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<input type="text" class="form-control" name="input_acc_type_id[]" id="acc_type_id_1">
<?php echo form_error('input_acc_type_id[]');?>
</div>
</div>
</td>
<td class="col-35">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<input type="text" class="form-control" name="input_note[]">
<?php echo form_error('input_note[]');?>
</div>
</div>
</td>
<td class="col-15">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<input type="text" class="form-control" name="input_debet[]">
</div>
</div>
</td>
<td class="col-15">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<input type="text" class="form-control" name="input_credit[]">
</div>
</div>
</td>
<td class="col-5"></td>
</tr>
// Second Input Field Form Table
<tr>
<td class="col-25">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<select class="select-transaction input-group-sm form-control" name="input_acc_code[]" id="acc_code_1">
<?php if ($account_list != NULL): ?>
<option>— Choose Account Number —</option>
<?php foreach ($account_list as $value): ?>
<option value="<?php echo $value->acc_code;?>"><?php echo $value->acc_name;?></option>
<?php endforeach;?>
<?php else:?>
<option>— No Data —</option>
<?php endif;?>
</select>
<?php echo form_error('input_acc_code[]');?>
</div>
</div>
</td>
<td class="col-5">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<input type="text" class="form-control" name="input_acc_type_id[]" id="acc_type_id_1">
<?php echo form_error('input_acc_type_id[]');?>
</div>
</div>
</td>
<td class="col-35">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<input type="text" class="form-control" name="input_note[]">
<?php echo form_error('input_note[]');?>
</div>
</div>
</td>
<td class="col-15">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<input type="text" class="form-control" name="input_debet[]">
</div>
</div>
</td>
<td class="col-15">
<div class="form-group form-group-sm">
<div class="col-sm-12">
<input type="text" class="form-control" name="input_credit[]">
</div>
</div>
</td>
<td class="col-5"></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12 col-md-12">
<div class="menu-bar">
<button class="btn btn-md btn-primary" type="submit">Save</input>
</div>
</div>
</div>
<?php echo form_close();?>
Thank you for your help....
Try moving the insert_batch related model call out of the foreach, as otherwise you could just run a straight insert there. First you are building the queries to insert, then after the foreach you insert the multidimensional array!
public function add()
{
$post = $this->input->post();
$result = array();
$total_input = count($post['input_acc_code']);
foreach ($post['input_acc_code'] as $key => $value) {
$result[] = array(
'trans_type' => 'journal',
'form_type' => NULL,
'acc_code' => $post['input_acc_code'][$key],
'acc_type_id' => $post['input_acc_type_id'][$key],
'refference' => '',
'customer_ID' => NULL,
'acc_side' => '',
'debet' => $post['input_debet'][$key],
'credit' => $post['input_credit'][$key],
'summary' => $post['input_note'][$key],
'files' => NULL,
'create_at' => date("Y-m-d H:i:s", strtotime("now"))
);
}
if ($this->model_transaction->savedata('fi_acc_journal', $result) == TRUE) {
$this->session->set_flashdata('alert', 'Success');
redirect(base_url() . 'admin/transaction');
} else {
$this->session->set_flashdata('alert', 'Failed');
redirect(base_url() . 'admin/transaction');
}
}

Trying to fetch image from database in codeigniter

I was uploaded Image in database Successfully. But not succeed to fetch image from database.
i want to fetch the image from database.
here is my code,
controller -> login.php
public function user_update(){
$this->load->model('login_model');
$this->form_validation->set_rules('fname', 'First Name','required');
$this->form_validation->set_rules('lname', 'Last Name','required');
$this->form_validation->set_rules('cnumber', 'Contact Number','required|min_length[10]|max_length[10]|numeric');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'doc|docx|pdf|gif|jpg|png|xlsx';
$config['max_size'] = 10000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if (( ! $this->upload->do_upload('image')) && ($this->form_validation->run() === FALSE ))
{
$this->load->view('student/home');
}
else
{
$data = array('upload_data' => $this->upload->data());
$image_name=($data['upload_data']['file_name']);
$resume=base_url().$image_name;
$udata = array(
'id' => $this->input->post('id'),
'fname' => $this->input->post('fname'),
'mname' => $this->input->post('mname'),
'lname' => $this->input->post('lname'),
'email' => $this->input->post('email'),
'address' => $this->input->post('address'),
'cnumber' => $this->input->post('cnumber'),
'image'=> $resume
);
$this->login_model->Updateuser($udata);
}
}
model file:- login_model.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class login_model extends CI_Model
{
function Updateuser($param) {
$id = $param['id'];
$usar1 = array(
'Student_Name' => $param['fname'],
'm_name' => $param['mname'],
'l_name' => $param['lname'],
'Student_Email' => $param['email'],
'contact_no' => $param['cnumber'],
'Address' => $param['address'],
'image' => $param['image']
);
$this->db->where('Student_id', $id);
$this->db->update('students', $usar1);
//echo ($this->db->last_query());
//exit();
$sturesult = $this->login_model->get_student_list();
$data['Stulist'] = $sturesult;
$this->load->view('student/default_list',$data);
}
}
view file:- edit_info.php
<div class="container" id='main-layout' style="border-top: 1px solid #D14B54; background: #f5f5f5f5">
<div class="row">
<div class="col-md-8 col-sm-8">
<div class="ajaxResponse"><input type="hidden" name="ajaxResponse"></div>
<div class="row" style="padding: 10px 5px;">
<?php $id = $this->uri->segment(3); if(!empty($id)): ?>
<div class="">
<?php echo validation_errors();
echo $lname;
?>
<div class="thumbnail familycol" style="padding:16px">
<?php echo form_open_multipart('login/user_update'); ?>
<legend>Personal Information</legend>
<div class="row">
<?php //echo form_label('Id :'); ?> <?php echo form_error('id'); ?>
<input type="hidden" name="id" value="<?php echo $row->Student_id; ?>" class="form-control input-sm" placeholder="id"/>
<!-- <label class="required">First name</label>-->
<div class="col-xs-6 col-md-6">
<label for="First Name">First Name:</label>
<input type="input" name="fname" class="form-control" value="<?php echo $row->Student_Name;?>"/><font color="red"><?php echo form_error('fname'); ?></font>
</div>
<div class="col-xs-6 col-md-6">
<!--<label class="required">Middle name</label>-->
<label for="Last Name">Middle Name:</label>
<input type="input" name="mname" class="form-control" value="<?php echo $row->m_name?>"/><font color="red"><?php echo form_error('mname'); ?></font>
</div>
</div>
<div class="row">
<div class="col-xs-6 col-md-6">
<!--<label class="required">Last name</label>-->
<label for="Last Name">Last Name:</label>
<input type="input" name="lname" class="form-control" value="<?php echo $row->l_name ;?>"/><font color="red"><?php echo form_error('lname'); ?></font>
</div>
<div class="col-xs-6 col-md-6">
<?php echo form_label('Email :'); ?> <?php echo form_error('email'); ?>
<input type="text" name="email" value="<?php echo $row->Student_Email; ?>" class="form-control input-sm" placeholder="Email" />
</div>
</div>
<br>
<div class="row">
<div class="col-sm-6">
<!--<label>Address</label>-->
<label for="text">Address</label>
<textarea name="address" class="form-control"><?php echo $row->Address ;?></textarea><font color="red"><?php echo form_error('address'); ?></font>
</div>
<div class="col-sm-6">
<label for="text">Contact Number</label>
<textarea name="cnumber" class="form-control"><?php echo $row->contact_no ;?></textarea><font color="red"><?php echo form_error('cnumber'); ?></font>
</div>
<br/>
</div>
<br/>
<label for='uploaded_file'>Select A File To Upload:</label>
<input type="file" name="image" accept="image/*" value="upload">
<br/>
<button class="btn btn-lg btn-primary btn-block signup-btn" type="submit" style="margin-top: 5px;">
Update my Profile</button>
</div>
</div>
<?php endif;?>
</div>
</div>
</div>
</div>
Ok Lets Put Code in your View file:
<img src="<?php echo base_url('uploads/').$Stulist[$i]->image; ?>"
You have updated the wrong image directory of uploaded images in database.
Simply update the following lines of code
$data = array('upload_data' => $this->upload->data());
$image_name=($data['upload_data']['file_name']);
$resume=base_url().$image_name;
to
$data = array('upload_data' => $this->upload->data());
$image_name=($data['upload_data']['file_name']);
$resume=base_url('uploads').$image_name;
here the base_url points to the uploads directory where you have uploaded your images.

Resources