Upload picture thumbnail preview with codeigniter - php

I made a codeigniter function so when a user selects a product picture to upload, it must show as preview picture in the form which the user selected. That way a user can see which picture he selected.
This is a link of how it should look like : https://imgur.com/a/2b775
index function in controller:
public function index()
{
$products = $this->db->get('products');
( null !== $products->result() ) ? ( !empty( $products->result() ) ? $products->result() : false ) : false;
$this->data = ['get_image' => $products];
//Laad kado uploaden view
$this->load->view('product_form', $data);
}
This is the upload function in the controller code:
public function upload() {
$this->load->library('upload');
$this->load->library('image_lib');
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['encrypt_name']= true;
$this->upload->initialize($config);
$products = $this->db->get('products');
( null !== $products->result() ) ? ( !empty( $products->result() ) ? $products->result() : false ) : false;
$this->data = ['get_image' => $products];
if(!$this->upload->do_upload('userfile')){
$error = array('error'=>$this->upload->display_errors());
$this->load->view('product_form', $error);
}else{
//Main image
$data = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = './upload/'.$data["raw_name"].$data['file_ext'];
$config['new_image'] = './upload/'.'new_'.$data["raw_name"].$data['file_ext'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 547;
$config['height'] = 430;
//Thumb image
$dataThumb = $this->upload->data();
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = './upload/'.$dataThumb["raw_name"].$dataThumb['file_ext'];
$configThumb['new_image'] = './upload/'.'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'];
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = FALSE;
$configThumb['width'] = 120;
$configThumb['height'] = 112;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
$this-> db-> insert('products', array(
'user_id' => $_SESSION['user_id'],
'product_foto' => 'new_'.$data["raw_name"].$data['file_ext'],
'product_foto_thumb' => 'thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'],
'product_naam' => $this->input->post('product_naam'),
'product_beschrijving' => $this->input->post('product_beschrijving'),
'ophaal_plaats' => $this->input->post('ophaal_plaats'),
'category_id' => !empty($this->input->post('category_id')) ? $this->input->post('category_id') : 0,
'date_created' => date('Y-m-d'),
'date_updated' => date('Y-m-d')
));
$data['img'] = base_url().
'/upload/new_'.$data["raw_name"].$data['file_ext'];
$dataThumb['img'] = base_url().
'/upload/thumb_'.$dataThumb["raw_name"].$dataThumb['file_ext'];
header('location:https://kadokado-ferran10.c9users.io//Product/cadeaupagina_aangeboden');
}
}
And this is the view form file of product_foto row:
<div class="image-upload">
<input id="file-input" type="file" name="userfile"/>
<h3>Upload foto </h3>
<label for="file-input">
<img src="../images/PLUS.jpg"/>
</label>
</div>
</td>
<?php if ($get_image) {
echo '<tr>';
echo '<td>'.img(array('width' => '120', 'height' => '80', 'src' => 'upload/'.$data->product_foto)).'</td>';
echo '<td> Bekijk | Verwijder </td>';
echo '</tr>';
}
else {
echo '<tr>';
echo '<td> </td>';
echo '</tr>';
} ?>
</tr>
The 2 erros that I'm getting:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: controllers/Product.php
Line Number: 17
Backtrace:
File: /home/ubuntu/workspace/application/controllers/Product.php
Line: 17
Function: _error_handler
File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once
2nd error:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: get_image
Filename: views/product_form.php
Line Number: 67
Backtrace:
File: /home/ubuntu/workspace/application/views/product_form.php
Line: 67
Function: _error_handler
File: /home/ubuntu/workspace/application/controllers/Product.php
Line: 21
Function: view
File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once
Database information and other information:
The map where the picture is stored: upload
The database table name: products
The product picture row is named: product_foto

Excluding any possible issues with your upload code, the reason you are getting these errors is because you are assigning $data to the class scope $this->data and not accessing $this->data but just $data in your view call.
Revised code:
public function index()
{
$products = $this->db->get('products');
( null !== $products->result() ) ? (!empty($products->result()) ? $products->result() : false ) : false;
$data = array('get_image' => $products);
//Laad kado uploaden view
$this->load->view('product_form', $data);
}
As a side note I don't think you have to access $this->upload->data(); twice as its the same data. Just assign it to an analogous variable like $image.

Related

How insert Multiple checkbox value in codeigniter

I need to insert multiple values in the checkbox in Codeigniter. how I can write this in the controller.
I tried so many but the values not inserting .
View.Php
<div class="custom-file">
<input type="checkbox" name="size[]" value="Medium"> M
<input type="checkbox" name="size[]" value="Large">L
<input type="checkbox" name="size[]" value="XL"> XL
</div>
Model
public function product_insert($data)
{
// grab user input
$this->db->insert('product', $data);
return $this->db->insert_id();
}
Controller
public function newProduct()
{
$data = array();
$this->load->library('form_validation');
$this->load->library('image_lib');
$this->load->helper('file');
$this->load->helper('string');
// Load the model
$this->load->model('admin/product_model');
$this->load->model('admin/category_model');
$data['category'] = $this->category_model->active_category_listing();
// $data['brand'] = $this->product_model->brand_listing();
$this->form_validation->set_error_delimiters('<div class="alert alert-danger">', '</div>');
// Validating Field
$this->form_validation->set_rules('product_title', 'Title', 'required');
if ($this->form_validation->run() == false) {
$this->load->view('admin/add_product', $data);
}
//insert size checkboxes into database.
$sizeArray = $this->input->post('size'); // Array
$sizeString = implode(",", $sizeArray); // String
else {
$config['upload_path'] = 'uploads/product_images/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 10024;
$this->load->library('upload', $config);
// upload file to directory
$uploadedFile ='';
if ($this->upload->do_upload('product_img')) {
$uploadData = $this->upload->data();
$uploadedFile = $uploadData['file_name'];
$config['image_library'] = 'gd2';
$config['source_image'] = $uploadData['full_path'];
$config['new_image'] = 'uploads/product_images/thumb';
$config['create_thumb'] = false;
$config['maintain_ratio'] = TRUE;
$config['width'] = 311;
$config['height'] = 415;
$this->image_lib->initialize($config);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$data['success_msg'] = 'File has been uploaded successfully.';
} else {
$data['error_msg'] = $this->upload->display_errors();
}
// Setting values for tabel columns
$data = array(
'product_title' => $this->input->post('product_title'),
'product_description' => $this->input->post('product_description'),
'product_price' => $this->input->post('product_price'),
'product_discount' => $this->input->post('product_discount'),
'product_quantity' => $this->input->post('product_quantity'),
'sub_category_id' => $this->input->post('sub_category_id'),
'category_id' => $this->input->post('category_id'),
'product_status' => $this->input->post('product_status'),
'product_img' => $uploadedFile,
'product_key' => random_string('alnum',8),
'product_cuttingprice' => $this->input->post('product_cuttingprice'),
'size'=>$sizeString);
);
// Transfering data to Model
$prdID = $this->product_model->product_insert($data);
$upImg = $this->uploadAddtnlImages($prdID);
$this->session->set_flashdata('msg', 'Product Added Successfully');
redirect('admin/product/listProduct/', 'refresh');
}
}
View:-
You must do all name checkbox as an array like this.
<div class="custom-file">
<input type="checkbox" name="size[]" value="Medium"> M
<input type="checkbox" name="size[]" value="Large">L
<input type="checkbox" name="size[]" value="XL"> XL
</div>
Controller:-
public function newProduct()
{
$data = array();
$this->load->library('form_validation');
$this->load->library('image_lib');
$this->load->helper('file');
$this->load->helper('string');
// Load the model
$this->load->model('admin/product_model');
$this->load->model('admin/category_model');
$data['category'] = $this->category_model->active_category_listing();
// $data['brand'] = $this->product_model->brand_listing();
$this->form_validation->set_error_delimiters('<div class="alert alert-danger">','</div>');
// Validating Field
$this->form_validation->set_rules('product_title', 'Title', 'required');
if ($this->form_validation->run() == false) {
$this->load->view('admin/add_product', $data);
}
else {
$config['upload_path'] = 'uploads/product_images/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 10024;
$this->load->library('upload', $config);
// upload file to directory
$uploadedFile ='';
if ($this->upload->do_upload('product_img')) {
$uploadData = $this->upload->data();
$uploadedFile = $uploadData['file_name'];
$config['image_library'] = 'gd2';
$config['source_image'] = $uploadData['full_path'];
$config['new_image'] = 'uploads/product_images/thumb';
$config['create_thumb'] = false;
$config['maintain_ratio'] = TRUE;
$config['width'] = 311;
$config['height'] = 415;
$this->image_lib->initialize($config);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$data['success_msg'] = 'File has been uploaded successfully.';
} else {
$data['error_msg'] = $this->upload->display_errors();
}
//insert size checkboxes into database.
$sizeArray = $this->input->post('size'); // Array
$sizeString = implode(",", $sizeArray); // String
// Setting values for tabel columns
$data = array(
'product_title' => $this->input->post('product_title'),
'product_description' => $this->input->post('product_description'),
'product_price' => $this->input->post('product_price'),
'product_discount' => $this->input->post('product_discount'),
'product_quantity' => $this->input->post('product_quantity'),
'sub_category_id' => $this->input->post('sub_category_id'),
'category_id' => $this->input->post('category_id'),
'product_status' => $this->input->post('product_status'),
'product_img' => $uploadedFile,
'product_key' => random_string('alnum',8),
'product_cuttingprice' => $this->input->post('product_cuttingprice'),
'size'=>$sizeString);
);
// Transfering data to Model
$prdID = $this->product_model->product_insert($data);
$upImg = $this->uploadAddtnlImages($prdID);
$this->session->set_flashdata('msg', 'Product Added Successfully');
redirect('admin/product/listProduct/', 'refresh');
}
}
Model Code:-
public function product_insert($data)
{
// grab user input
$this->db->insert('product', $data);
return $this->db->insert_id();
}
Note:- For More info regarding implode().
The implode() function returns a string from the elements of an array.
https://www.php.net/manual/en/function.implode.php

Passing a variable through function

When I submit my form on my main index I am trying to pass a variable call $store_id to a function on same controller called do_upload_image($store_id)
I am trying to be able to get the last id that was inserted in my store table.
It works fine when loading another model function $this->example_model->add($store_id); but not when doing it on another function on same controller.
Error Output
Column 'store_id' cannot be null
INSERT INTO `setting` (`store_id`, `group`, `key`, `value`) VALUES (NULL, 'config', 'config_image', 'flagnz.png')
Like so
$store_id = $this->model_store_add->add_store(); // this way should let me get id that was inserted.
$this->do_upload_image($store_id);
How am I able to get the $store_id to pass through another function on same controller? I have a model which trying to pass the store id to on do_upload_image
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Store_add extends MX_Controller {
public $store_id;
public function __construct() {
parent::__construct();
if (!$this->session->userdata('isLogged')) {
redirect('admin');
}
$this->load->model('admin/setting/model_store_add');
$this->load->model('admin/setting/model_store_get');
$this->lang->load('admin/setting/store_add', $this->settings->get('config_admin_language'));
}
public function index()
$this->load->library('form_validation');
$this->form_validation->set_rules('config_image', 'Store Image','callback_do_upload_image');
if ($this->form_validation->run($this) == FALSE) {
return $this->load->view('setting/store_add.tpl');
} else {
// $store id lets me get id that was inserted from store table
$store_id = $this->model_store_add->add_store();
$this->model_store_add->add_config_meta_title($store_id); // Works
$this->do_upload_image($store_id);
$this->session->set_flashdata('success', $this->lang->line('text_success'));
redirect('admin/setting/store');
}
}
public function do_upload_image($store_id) {
$config['upload_path'] = './image/upload/';
$config['allowed_types'] = $this->settings->get('config_file_ext_allowed');
$config['max_size'] = $this->settings->get('config_file_max_size');
$config['overwrite'] = $this->settings->get('config_file_overwrite');
$config['max_width'] = '*';
$config['max_height'] = '*';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (is_uploaded_file($_FILES['config_image']['tmp_name'])) {
if (!$this->upload->do_upload('config_image')) {
$this->form_validation->set_message('do_upload_image', $this->upload->display_errors('<b>config_image</b>' .' '));
return false;
} else {
$resize_data = $this->upload->data();
$config_resize['image_library'] = 'gd2';
$config_resize['source_image'] = './image/upload/' . $resize_data['file_name'];
$config_resize['new_image'] = './image/cache/' . $resize_data['file_name'];
$config_resize['maintain_ratio'] = false;
$config_resize['create_thumb'] = false;
$config_resize['width'] = 100;
$config_resize['height'] = 100;
$this->load->library('image_lib', $config_resize);
$this->image_lib->initialize($config_resize);
if (!$this->image_lib->resize()) {
$this->sessions->set_flashdata('error', $this->image_lib->display_errors());
redirect('admin/setting/setting');
} else {
$this->model_store_add->add_config_image($store_id, $this->upload->data());
}
}
}
}
}
Model on do_upload_image
public function add_config_image($store_id = 0, $file_data = array()) {
$file_data = $this->upload->data();
$data = array(
'store_id' => $store_id,
'group' => "config",
'key' => "config_image",
'value' => $file_data['file_name']
);
$this->db->set($data);
$this->db->insert($this->db->dbprefix. 'setting');
}
Model add
public function add_store() {
$data = array(
'name' => $this->input->post('config_name'),
'url' => $this->input->post('config_url'),
'ssl' => $this->input->post('config_ssl')
);
$this->db->set($data);
$this->db->insert_id();
$this->db->insert($this->db->dbprefix . 'store');
return $this->db->insert_id();
}
I have narrowed down my own problem. I need to pass the store id a second time as a if statement and now all works
I just removed the resize function for testing to see if that was issue but now no why. Also need to add return true below for callback function.
public function do_upload_image($store_id) {
$config['upload_path'] = './image/upload/';
$config['allowed_types'] = $this->settings->get('config_file_ext_allowed');
$config['max_size'] = $this->settings->get('config_file_max_size');
$config['overwrite'] = $this->settings->get('config_file_overwrite');
$config['max_width'] = '*';
$config['max_height'] = '*';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('config_image')) {
if ($store_id) {
$this->model_store_add->add_config_image($store_id, $this->upload->data());
}
return true;
} else {
return false;
}
}

Undefined variable in CodeIgniter view using submit function

I'm trying create a website using codeigniter. The structure that I use is only using view and controller. The model is directly in controller.
So, i got this error message:
Severity: Notice
Message: Undefined variable: view
Filename: template/default.php
Line Number: 146
And this is my code on controller (artikeladmin.php)
public function add()
{
if($this->input->post())
{
$judul = $this->input->post('judul');
$kategori_id = $this->input->post('kategori_id');
$tanggal = $this->input->post('tanggal');
$gambar = $this->input->post('gambar');
$published = $this->input->post('published');
$deskripsi = $this->input->post('deskripsi');
$config['upload_path'] ='assets/images/upload/';
$config['allowed_types'] ='jpeg|png|gif|jpg';
$config['max_size'] ='*';
$config['max_width'] ='*';
$config['max_height'] ='*';
$this->load->library('upload', $config);
if(!$this->upload->do_upload())
{
$data['error'] = array('error' =>$this->upload->display_errors());
$this->load->view('admin/template/default', $data);
}
else
{
$attr = $this->upload->data();
$args = array(
'gambar' => $attr['file_name'],
'kategori_id' => $kategori_id,
'judul' => $judul,
'published' => $published,
'tanggal' => $tanggal,
'deskripsi' => $deskripsi
);
$this->db->insert('artikel', $args);
$this->session->set_flashdata('message','Data berhasil disimpan');
redirect('artikeladmin');
}
}
$user = $this->session->userdata('myuser');
if(!isset($user) or empty($user))
redirect('login');
$sql = " SELECT * FROM kategori";
$query = $this->db->query($sql);
$kategori = $query->result_array();
$data['view'] = 'admin/add_article';
$data['kategori'] = $kategori;
$data['action'] = 'artikeladmin/add/';
$this->load->view('admin/template/default', $data);
}
And this is my view (template/default.php)
<div id="content" class="span10">
<?php $this->load->view($view); ?>
</div>
in my controller you can see that i have define variable view like this
$data['view'] = 'admin/add_article';
And im trying to add new variable like
$data['view'] = $data;
but the error still happen
If do_upload() fails it will load the view without the $view variable being set:
if(!$this->upload->do_upload())
{
$data['error'] = array('error' =>$this->upload->display_errors());
$this->load->view('admin/template/default', $data);
}
default.php looks for $view
<?php $this->load->view($view); ?>
and you get the Undefined variable error.
Try instantiating $view before any calls to default.php are made.

Uploading logo using Codeigniter not working on editform

I have an editform for editing my company information. I also have a field for uploading a logo. When I click on choose file, and click Submit (the form) It stores the filename in my database as imagename.jpg for example.
I also want to add the image to the folder assets/uploads/. I tried it using the codeigniter upload class, same as I did on my other upload form, but this one does not work. I just don't know why.
Here's my editform:
<?= form_open_multipart('members/update/'.$id);?>
//other fields for editing company
<tr>
<td><?= form_label('Logo:'); ?></td>
<td><input type="file" name="logo" size="20" /></td>
</tr>
<tr>
<td><?= form_submit('submit', 'Opslaan');?> <?= form_reset('reset', 'Reset');?></td>
</tr>
</table>
<?= form_close()?>
My controller:
function update() //de update functie voor bovenstaande functie.
{
$id = $this->uri->segment(3);
$data = array(
'Bedrijfsnaam' => $this->input->post('Bedrijfsnaam'),
'Postcode' => $this->input->post('Postcode'),
'Plaats' => $this->input->post('Plaats'),
'Telefoonnummer' => $this->input->post('Telefoonnummer'),
'Email' => $this->input->post('Email'),
'Website' => $this->input->post('Website'),
'Profiel' => $this->input->post('Profiel'),
'Adres' => $this->input->post('Adres'),
);
if($this->input->post('logo')) { $data['logo'] = $this->input->post('logo'); }
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '';
$config['max_height'] = '';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
}
$this->members_model->updatebedrijf($id, $data);
$b = $this->session->userdata('idbedrijven');
redirect("members/$b");
}
function updatebedrijf($id, $data)
{
foreach($this->input->post('categorieen') as $cat){
$this->db->where('idbedrijven', $id);
$this->db->update('bedrijven', $data);
$to_bedrijfcategorieen2['idcategorieen'] = $this->input->post('categorieen');
$this->insert_bedrijfcat1($to_bedrijfcategorieen2);
};
}
My model:
function updatebedrijf($id, $data)
{
foreach($this->input->post('categorieen') as $cat){
$this->db->where('idbedrijven', $id);
$this->db->update('bedrijven', $data);
$to_bedrijfcategorieen2['idcategorieen'] = $this->input->post('categorieen');
$this->insert_bedrijfcat1($to_bedrijfcategorieen2);
};
}
It just does not upload. The path is the same as my other uploadfunction. that one works fine.
You have not set field name in the do_upload function
if ( ! $this->upload->do_upload("logo"))
{
$error = array('error' => $this->upload->display_errors());
}else{
$image_data = $this->upload->data();
}
And there is one thing wrong you have a if check for logo field and you are trying to get in post why ?? it should be
if($_FILES['logo']['name'] != '') { $data['logo'] = $_FILES['logo']['name']; }
if ( ! $this->upload->do_upload(?))
You should set file name as argument to upload function, since it is not default 'userfile'?
Not any of the solutions woked. This the way that I came up with.
if ($_FILES['userfile']['name'] != null) {
// upload configuration
$upload_config = array(
'upload_path' => './assets/uploads/',
'allowed_types' => 'jpeg|jpg|png',
'max_size' => 1048576,
'max_width' => 10240,
'max_height' => 7680,
'encrypt_name' => TRUE
);
// load upload library config
$this->load->library('upload', $upload_config);
if (!$this->upload->do_upload()) {
// assign upload errors
$data['upload_errors'] = $this->upload->display_errors();
// load page title
$page['title'] = "Edit Parent";
// fetch parent information
$this->db->select('Database Query');
// info data in row
$data['info'] = $this->db->get()->row();
// load view with page title
$this->load->view('temp/header', $page);
$this->load->view('editParents', $data);
$this->load->view('temp/footer');
} else {
$upload_photo = $this->upload->data();
// profile image
$parent_photo = $upload_photo['file_name'];
}
} else {
// profile image
$parent_photo = $this->input->post('profile_image');
}

Viewing files from database tables using Codeigniter

I am trying to view my image files taken from the database and i have no idea why i get a undefined variable when loading my view.
Pretty much I am just trying to view the imagenames stored in my database images.
Any help would be great thank you.
Here are the errors i receive when viewing in browser
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: imagename
Filename: layouts/home.php
Line Number: 8
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: layouts/home.php
Line Number: 8
Here is my Controller
class Home extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->library('upload');
}
function index() {
$this->load->model('index_model');
$foldername['foldername'] = $this->index_model->getFoldernames();
$imagename['imagename'] = $this->index_model->getImagenames();
$this->load->view('layouts/home', $foldername, $imagename, array('error' => ' ' ));
}
function create() {
if(array_key_exists('createFolder',$_POST)){
$data = array(
'folderName' => $this->input->post('folderName')
);
$data = str_replace(' ', '_', $data);
$this->index_model->createFolder($data);
}
$this->foldercreated();
}
function delete() {
$this->load->model('index_model');
$foldername['foldername'] = $this->index_model->getFoldernames();
$this->load->view('layouts/delete', $foldername);
}
function deleteFolder() {
if($this->input->post('checkbox') !== false) {
$checkbox = $this->input->post('checkbox');
$this->index_model->deleteFolder($checkbox);
}
$this->folderdeleted();
}
function foldercreated() {
$this->load->view('partials/foldercreated');
}
function folderdeleted() {
$this->load->view('partials/folderdeleted');
}
function do_upload() {
$config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->upload->initialize($config);
$this->load->library('upload', $config);
if ( !$this->upload->do_upload('userfile'))
{
// echo '<p>IMAGE NOT WORKING</p>'. '<br/><p>' . $this->upload->display_errors() . '</p>';
$uploadError = $this->upload->display_errors();
$this->load->view('partials/upload_error', $uploadError);
}
else
{
// $imagefile = array('upload_data' => $this->upload->data());
$upload_data = $this->upload->data();
$db_data = array('imageName' => $upload_data['file_name']);
$this->index_model->addImage($db_data); // replace the tablename
$this->load->view('partials/upload_success', $db_data);
}
}
}
Here is my Model
function getFolderNames() {
$this->db->order_by('id', 'DESC');
$this->db->select('folderName');
$q = $this->db->get('senior');
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
function getImagenames() {
$this->db->order_by('id', 'DESC');
$this->db->select('imageName');
$query = $this->db->get('images');
if($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
}
And Here is my View
<div id="imagefiles">
<?php
foreach ($imagename as $img) { ?>
<div><?php echo $img->imageName; ?></div>
<?php } ?>
</div>
You aren't passing data to your view correctly.
You have this: $this->load->view('layouts/home', $foldername, $imagename, array('error' => ' ' ));
Your second parameter should be an array of data that you want to pass to your view. Try something like this:
$data['foldername'] = $this->index_model->getFoldernames();
$data['imagename'] = $this->index_model->getImagenames();
$this->load->view('layouts/home', $data);
Please check the CodeIgniter documentation for more info.

Categories