ERROR, when deleting multiple rows in codeigniter - codeigniter

my table name is inbox (id, date, message). and here is my code,
in view : inbox.php
<?php $attribute = array('class' => 'check', 'id' => 'myform'); ?>
<?php echo form_open('messages/remove_checked', $attribute); ?>
<table class="table table-striped table-bordered tablesorter" id="mytable">
<tr><th width="5px" class=specalt ><input type="checkbox" id="select_all" name="select_all"/></th><th>date</th><th>Message</th></tr>
<?php foreach($inbox as $row) : ?>
<tr>
<td width="5px" class=spec><input type="checkbox" name="cntact[]" class="check" value="<?php echo $row->ID; ?>"></td>
<td><? echo $row->Date; ?></td>
<td><? echo $row->Message; ?></td>
</tr>
<?php endforeach;?>
</table>
i add this one to check all the checkbox,
<script>
$(document).ready(function() {
$('#select_all').click(function() {
var c = this.checked;
$(':checkbox').prop('checked',c);
});
});
</script>
my controller : messages.php
function remove_checked()
{
//validation rules
$this->load->library('form_validation');
$this->form_validation->set_rules('cntact[]', 'Private Contact', 'required|xss_clean');
if ($this->form_validation->run() == FALSE)
{
$remove = "No <strong>Data</strong> deleted!";
$this -> json_response(FALSE, $remove);
redirect("messages/inbox");
}
else //success
{
$checked_messages = $this->input->post('cntact'); //selected messages
$this->Mysms_model->delete_checked($checked_messages);
//redirect to
redirect("messages/inbox");
}
}
and my model : Mysms_model.php
function delete_checked($cntact)
{
$this->db->where_in('ID', $cntact)
->delete('inbox');
return $this->db->affected_rows() > 0;
}
the problem is, when i run this code, i found an error.
"DELETE FROM inbox WHERE ID IN ('on', 'on')".
so, what s on? it should number of ID, any answer?
thank you.

This is not meant to be an answer to the question, however, I just wanted to show you an alternative way to write your view using codeigniters built in libraries and helpers:
<?php
$this->load->helper('form');
$this->load->library('table');
echo form_open('messages/remove_checked', array('class' => 'check', 'id' => 'myform'));
$this->table->set_template(array('table_open' => '<table class="table table-striped table-bordered tablesorter" id="mytable">'));
$this->table->set_heading(
array('width' => '5px', 'class' => 'specalt', 'data' => form_checkbox(array('id' => 'select_all', 'name' => 'select_all'))),
'date',
'message'
);
foreach ($inbox as $row) {
$this->table->add_row(
array('width' => '5px', 'class' => 'spec', 'data' => form_checkbox(array('class' => 'check', 'name' => 'cntact[]', 'value' => $row->ID))),
$row->Date,
$row->Message
);
}
echo $this->table->generate();
echo form_close();

Related

Array to string conversion (searching in codeigniter)

I have problem with searching in my script. Problem is that i want to create filter. Filter is dropdown menu and i am entering data from this dropdown menu to array. I think this is problem but idk how to enter it different way.
Or how to pass data from my model as array and not as obj. (I think it is object)
My search:
<?php echo form_open("otk/triedit", 'class="form-inline"'); ?>
<th scope="col"></th>
<th scope="col">ČÍSLO ZÁKAZKY</th>
<th scope="col"><?php echo form_dropdown(['class' => 'form-control', 'name' => 'p'], $pozicia); ?>POZÍCIA</th>
<th scope="col"><?php echo form_dropdown(['class' => 'form-control', 'name' => 's'], $stav); ?>STAV</th>
<th scope="col">PORADOVÉ ČÍSLO</th>
<th scope="col"><?php echo form_dropdown(['class' => 'form-control', 'name' => 'st'], $technologia); ?>TECHNOLÓGIA</th>
<th scope="col"><?php echo form_dropdown(['class' => 'form-control', 'name' => 'd'], $datum); ?>DÁTUM</th>
<th scope="col">DOKUMENT</th>
<th scope="col"><?php echo form_dropdown(['class' => 'form-control', 'name' => 'z'], $zariadenie); ?>ZARIADENIE</th>
<th scope="col">OPERÁTOR</th>
<th scope="col"><center></center></th>
<?php echo form_submit(['type' => 'submit', 'class' => 'btn btn-warning', 'value' => 'Hľadať']); ?>
<?php echo form_close(); ?>
My Controller:
function triedit()
{
$this->load->model('base_model');
$udaje = array(
'hladat' => $this->input->post('p'),
'hladat' => $this->input->post('s'),
'hladat' => $this->input->post('st'),
'hladat' => $this->input->post('d'),
'hladat' => $this->input->post('z')
);
$keyword = $udaje;
$data['resulte'] = $this->base_model->triedit($keyword);
$this->load->view('base_triedit_view',$data);
}
My model:
function triedit($keyword)
{
$this->db->select('*');
$this->db->from('otk');
$this->db->like('ckod_otk',$keyword);
$this->db->join('technologia', 'technologia.id_technologia = otk.technologia_otk', 'left');
$this->db->join('zariadenie', 'zariadenie.id_zariadenie = otk.zariadenie_otk', 'left');
$this->db->join('zamestnanci', 'zamestnanci.id_zamestnanci = otk.operator_otk', 'left');
$this->db->join('stav', 'stav.id_stav = otk.stav_otk', 'left');
$query = $this->db->get();
return $query->num_rows();
}
And my preview of my view:
<?php if(is_array($resulte)): ?>
<?php
foreach ($resulte as $row) {?>
<tr>
<td></td>
<td><?php echo $row->czakazky_otk; ?></td>
<td><?php echo $row->pozicia_otk; ?></td>
.....
<?php } ?>
<?php endif; ?>
as far as i understand your problem
Data options format for form_dropdown is always in an array there is no alternative if you use form_dropdown
If you want your model should return data as an array
Do it like this :
function triedit($keyword)
{
$this->db->select('*');
$this->db->from('otk');
$this->db->like('ckod_otk',$keyword);
$this->db->join('technologia', 'technologia.id_technologia = otk.technologia_otk', 'left');
$this->db->join('zariadenie', 'zariadenie.id_zariadenie = otk.zariadenie_otk', 'left');
$this->db->join('zamestnanci', 'zamestnanci.id_zamestnanci = otk.operator_otk', 'left');
$this->db->join('stav', 'stav.id_stav = otk.stav_otk', 'left');
$query = $this->db->get();
if ($query->num_rows() > 0 )
{
return $query->result_array();
}
}
So changes in the view :
<?php if(is_array($resulte)): ?>
<?php
foreach ($resulte as $row) {?>
<tr>
<td></td>
<td><?php echo $row['czakazky_otk']; ?></td>
<td><?php echo $row['pozicia_otk']; ?></td>
..............
<?php } ?>
<?php endif; ?>
For more : https://www.codeigniter.com/user_guide/database/results.html#result-arrays

Model not inserting array data

When I submit my form my controller[] array post does not work throws errors.
Error 1
A PHP Error was encountered Severity: Notice Message: Array to string
conversion Filename: mysqli/mysqli_driver.php Line Number: 544
Error Number: 1054 Unknown column 'Array' in 'field list' INSERT INTO
user_group (name, controller, access, modify) VALUES
('Admin', Array, '1', '1') Filename:
C:\Xampp\htdocs\riwakawebsitedesigns\system\database\DB_driver.php
Line Number: 331
It is not inserting the controller names. Not sure best way to fix?
Model
<?php
class Model_user_group extends CI_Model {
public function addUserGroup($data) {
$data = array(
'name' => $this->input->post('name'),
'controller' => $this->input->post('controller'),
'access' => $this->input->post('access'),
'modify' => $this->input->post('modify')
);
$this->db->set($data);
$this->db->insert_id();
$this->db->insert($this->db->dbprefix . 'user_group');
}
?>
Controller
<?php
class Users_group extends Admin_Controller {
public function index() {
$data['title'] = "Users Group";
$this->load->model('admin/user/model_user_group');
$user_group_info = $this->model_user_group->getUserGroup($this->uri->segment(4));
if ($this->input->post('name') !== FALSE) {
$data['name'] = $this->input->post('name');
} else {
$data['name'] = $user_group_info['name'];
}
$ignore = array(
'admin',
'login',
'dashboard',
'filemanager',
'login',
'menu',
'register',
'online',
'customer_total',
'user_total',
'chart',
'activity',
'logout',
'footer',
'header',
'permission'
);
$data['controllers'] = array();
$files = glob(FCPATH . 'application/modules/admin/controllers/*/*.php');
foreach ($files as $file) {
$controller = basename(strtolower($file), '.php');
if (!in_array($controller, $ignore)) {
$data['controllers'][] = $controller;
}
}
if ($this->input->post('name') !== FALSE) {
$data['controller'] = $this->input->post('controller');
} else {
$data['controller'] = $user_group_info['controller'];
}
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'User Group Name', 'required');
if ($this->form_validation->run($this) == FALSE) {
$this->load->view('template/user/users_group_form.tpl', $data);
} else {
$this->load->model('admin/user/model_user_group');
$this->model_user_group->addUserGroup($this->input->post());
redirect('admin/users_group');
}
}
}
?>
View
<?php echo validation_errors('<div class="alert alert-warning text-center"><i class="fa fa-exclamation-triangle"></i>
', '</div>'); ?>
<?php if ($this->uri->segment(4) == FALSE) { ?>
<?php $data = array('class' => 'form-horizontal', 'id' => 'form-users-group');?>
<?php echo form_open('admin/users_group/add', $data);?>
<?php } else { ?>
<?php $data = array('class' => 'form-horizontal', 'id' => 'form-users-group');?>
<?php echo form_open('admin/users_group/edit' .'/'. $this->uri->segment(4), $data);?>
<?php } ?>
<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php echo form_label('User Group Name', 'name', $data);?>
<div class="col-sm-10">
<?php $data1 = array('id' => 'name', 'name' => 'name', 'class' => 'form-control', 'value' => $name);?>
<?php echo form_input($data1);?>
</div>
</div>
<table class="table table-striped table-bordered">
<thead>
<tr>
<td>Controller Name</td>
<td>Access</td>
<td>Modify</td>
</tr>
</thead>
<?php foreach ($controllers as $controller) {?>
<tbody>
<tr>
<td><?php echo $controller;?>
<input type="hidden" name="controller[]" value="<?php echo $controller;?>" />
</td>
<td>
<select name="access" class="form-control">
<option>1</option>
<option>0</option>
</select>
</td>
<td>
<select name="modify" class="form-control">
<option>1</option>
<option>0</option>
</select>
</td>
</tr>
</tbody>
<?php } ?>
</table>
<?php echo form_close();?>
The error is because you cannot insert php-array in database.
Instead store comma separated values.
In your model change data array as below:
public function addUserGroup($data) {
$controllers = $this->input->post('controller');
$name = $this->input->post('name');
$access = $this->input->post('access');
$modify = $this->input->post('modify');
for($i=0;$i<count($controllers);$i++) {
$data = array(
'name' => $name,
'controller' => $controllers[$i],
'access' => $access,
'modify' => $modify
);
$this->db->set($data);
$this->db->insert_id();
$this->db->insert($this->db->dbprefix . 'user_group');
}
}
Your controller hidden field is an array. You're passing this array to your addUserGroup function, which is trying to insert this array into the database. It's implicitly trying to convert this array to a string. Maybe try changing your function to this:
'controller' => $this->input->post('controller')[0],
Problem fixed foreach in model Thanks for the ideas on how to fix problems every one.
foreach ($this->input->post('controller') as $controller) {
$data = array(
'name' => $this->input->post('name'),
'controller' => $controller,
'access' => $this->input->post('access'),
'modify' => $this->input->post('modify')
);
$this->db->set($data);
$this->db->insert_id();
$this->db->insert($this->db->dbprefix . 'user_group');
}

how to upload image in database save in folder?

I am new to CI.Can anyone help me giving tutorial for image uploading for CI 2.0.
I have make an folder Product_image in asset folder.My table name is tbl_product.my controller page is product and code is:
<?php
include_once('application/backend/controllers/dashboard.php');
class Product extends Dashboard {
function _construct(){
parent::_construct();
$this->load->model('product_model');
}
public function index() {
$this->islogin();
$data=$this->generateCommonItems();
$this->load->model('product_model');
$data['page_title']="List of Products";
$data['page_heading']="List of Products";
$data['listAllProduct']=$this->product_model->listAllProduct();
$this->load->view('products/listproduct',$data);
}
function addproduct() {
$this->data['title'] = 'Add Product';
//validate form input
$this->form_validation->set_rules('prod_name', 'Product name', 'required|xss_clean');
$this->form_validation->set_rules('prod_des', 'Description', 'required|xss_clean');
$this->form_validation->set_rules('price', 'Price', 'required|xss_clean');
$this->form_validation->set_rules('prod_image', 'Picture', 'required|xss_clean');
if ($this->form_validation->run() == true)
{
$data = array(
'prod_name' => $this->input->post('prod_name'),
'prod_des' => $this->input->post('prod_des'),
'price' => $this->input->post('price'),
'prod_image' => $this->input->post('prod_image')
);
$this->product_model->insert_product($data);
redirect('product/addproduct');
} else {
$data=$this->generateCommonItems();
//display the add product form
//set the flash data error message if there is one
$this->data['prod_name'] = array(
'name' => 'name',
'id' => 'name',
'type' => 'text',
'style' => 'width:300px;',
'value' => $this->form_validation->set_value('name'),
);
$this->data['prod_des'] = array(
'name' => 'description',
'id' => 'description',
'type' => 'text',
'cols' => 60,
'rows' => 5,
'value' => $this->form_validation->set_value('description'),
);
$this->data['price'] = array(
'name' => 'price',
'id' => 'price',
'type' => 'text',
'style' => 'width:40px;text-align: right',
'value' => $this->form_validation->set_value('price'),
);
$this->data['prod_image'] = array(
'value' => $this->form_validation->set_value('prod_image'),
);
$this->load->view('includes/header',$data);
$this->load->view('products/product_form', $this->data);
$this->load->view('includes/footer',$data);
}
}
public function delete($prod_id) {
$this->islogin();
$this->load->model('product_model');
if($this->product_model->deleteByid($prod_id))
redirect('product/index');
}
function edit_product($prod_id) {
$product = $this->product_model->get_product($prod_id);
$this->data['title'] = 'Edit Product';
//validate form input
$this->form_validation->set_rules('prod_name', 'Product name', 'required|xss_clean');$this->form_validation->set_rules('description', 'Description', 'required|xss_clean');
$this->form_validation->set_rules('price', 'Price', 'required|xss_clean');
$this->form_validation->set_rules('prod_image', 'Picture', 'required|xss_clean');
if (isset($_POST) && !empty($_POST))
{
$data = array(
'prod_name' => $this->input->post('prod_name'),
'prod_des' => $this->input->post('prod_des'),
'price' => $this->input->post('price'),
'prod_image' => $this->input->post('prod_image'),
);
if ($this->form_validation->run() === true)
{
$this->product_model->update_product($prod_id, $data);
redirect(base_url().'product/edit/'.$prod_id);
}
}
$this->data['tbl_product'] = $product;
//display the edit product form
$this->data['prod_name'] = array(
'name' => 'name',
'id' => 'name',
'type' => 'text',
'style' => 'width:300px;',
'value' => $this->form_validation->set_value('name', $product['name']),
);
$this->data['prod_des'] = array(
'name' => 'description',
'id' => 'description',
'type' => 'text',
'cols' => 60,
'rows' => 5,
'value' => $this->form_validation->set_value('description', $product['description']),
);
$this->data['price'] = array(
'name' => 'price',
'id' => 'price',
'type' => 'text',
'style' => 'width:40px;text-align: right',
'value' => $this->form_validation->set_value('price', $product['price']),
);
$this->data['prod_image'] = array(
'name' => 'picture',
'id' => 'picture',
'type' => 'text',
'style' => 'width:250px;',
'value' => $this->form_validation->set_value('prod_image', $product['picture']),
);
$this->load->view('products/edit_product', $this->data);
}
}
my model page is product_model:
<?php class Product_model extends CI_Model {
function __construct() {
parent::__construct();
}
function listAllProduct() {
$query = $this->db->select('*')->from('tbl_product')->get();
//return $query = result();
//$query = $this->db->query("select * from tbl_product");
return $query->result();
}
function get_product($prod_id) {
$this->db->select('prod_id, prod_name, prod_des, price, prod_image');
$this->db->where('prod_id', $prod_id);
$query = $this->db->get('tbl_product');
return $query->row_array();
}
public function insert_product($data) {
if ($this->db->insert('tbl_product', $data))
return true;
else {
return false;
}
}
public function update_product($prod_id, $data) {
$this->db->where('prod_id', $prod_id);
$this->db->update('tbl_product', $data);
}
function deleteByid($prod_id) {
$this->db->where('prod_id', $prod_id);
if ($this->db->delete('tbl_product')) {
return true;
} else {
return false;
}
View:
<h2 align="center">Add Product</h2>
<?php echo form_open("product/addproduct");?>
<table width="700" border="1" cellpadding="0" cellspacing="2" align="center">
<tr>
<td width="130" align="right" bgcolor="#FFFFFF">Product Name: </td>
<td><?php echo form_input($prod_name);?></td>
</tr>
<tr>
<td width="130" align="right" bgcolor="#FFFFFF">Product Description: </td>
<td><?php echo form_textarea($prod_des); ?></td>
</tr>
<tr>
<td align="right" bgcolor="#FFFFFF">Price:</td>
<td><?php echo form_input($price); ?></td>
</tr>
<tr>
<td align="right" bgcolor="#FFFFFF">Picture:</td>
<td><?php echo form_input($prod_image); ?></td>
</tr>
<tr>
<td align="right" bgcolor="#FFFFFF"> </td>
<td><?php echo form_submit('submit', 'Submit');?>
</tr>
</table>
<?php echo form_close(); ?>
Form page view:
<h2 align="center">Add Product</h2>
<?php echo form_open("product/addproduct");?>
<table width="700" border="1" cellpadding="0" cellspacing="2" align="center">
<tr>
<td width="130" align="right" bgcolor="#FFFFFF">Product Name: </td>
<td><?php echo form_input($prod_name);?></td>
</tr>
<tr>
<td width="130" align="right" bgcolor="#FFFFFF">Product Description: </td>
<td><?php echo form_textarea($prod_des); ?></td>
</tr>
<tr>
<td align="right" bgcolor="#FFFFFF">Price:</td>
<td><?php echo form_input($price); ?></td>
</tr>
<tr>
<td align="right" bgcolor="#FFFFFF">Picture:</td>
<td><?php echo form_input($prod_image); ?></td>
</tr>
<tr>
<td align="right" bgcolor="#FFFFFF"> </td>
<td><?php echo form_submit('submit', 'Submit');?>
</tr>
</table>
<?php echo form_close(); ?>
I m thankful to him/her who give me solution for this error code by checking mistake n error?
1: You have to use the attribute open_multipart when uploading files in forms:
<?php echo form_open_multipart('product/addproduct');?>
2: Take off the file form attrribute from the form validation: Delete this:
$this->form_validation->set_rules('prod_image', 'Picture', 'required|xss_clean');
3: Use this to save the file in folder:
$config['upload_path'] = './asset/Product_image/'; //The path where the image will be save
$config['allowed_types'] = 'gif|jpg|png'; //Images extensions accepted
$config['max_size'] = '2048'; //The max size of the image in kb's
$config['max_width'] = '1024'; //The max of the images width in px
$config['max_height'] = '768'; //The max of the images height in px
$config['overwrite'] = TRUE; //If exists an image with the same name it will overwrite. Set to false if don't want to overwrite
$this->load->library('upload', $config); //Load the upload CI library
if (!$this->upload->do_upload('userfile')){
$uploadError = array('upload_error' => $this->upload->display_errors());
$this->set_flashdata('uploadError', $uploadError, $urlYouWantToReturn); //If for some reason the upload could not be done, returns the error in a flashdata and redirect to the page you specify in $urlYouWantToReturn
exit;
}
4: After that, you gonna grab the file name to save the file reference in Database. Put right below:
$file_info = $this->upload->data('userfile');
$file_name = $file_info['file_name']; //Now you got the file name in the $file_name var. Use it to record in db.
//You can assign it to your data array to pass to your update_product function.
5: Now, the whole code merged:
function addproduct() {
$this->data['title'] = 'Add Product';
//validate form input
$this->form_validation->set_rules('prod_name', 'Product name', 'required|xss_clean');
$this->form_validation->set_rules('prod_des', 'Description', 'required|xss_clean');
$this->form_validation->set_rules('price', 'Price', 'required|xss_clean');
if ($this->form_validation->run() == true)
{
$config['upload_path'] = './asset/Product_image/'; //The path where the image will be save
$config['allowed_types'] = 'gif|jpg|png'; //Images extensions accepted
$config['max_size'] = '2048'; //The max size of the image in kb's
$config['max_width'] = '1024'; //The max of the images width in px
$config['max_height'] = '768'; //The max of the images height in px
$config['overwrite'] = TRUE; //If exists an image with the same name it will overwrite. Set to false if don't want to overwrite
$this->load->library('upload', $config); //Load the upload CI library
if (!$this->upload->do_upload('userfile')){
$uploadError = array('upload_error' => $this->upload->display_errors());
$this->set_flashdata('uploadError', $uploadError, $urlYouWantToReturn); //If for some reason the upload could not be done, returns the error in a flashdata and redirect to the page you specify in $urlYouWantToReturn
exit;
}
$file_info = $this->upload->data('userfile');
$file_name = $file_info['file_name']; //Now you got the file name in the $file_name var. Use it to record in db.
$data = array(
'prod_name' => $this->input->post('prod_name'),
'prod_des' => $this->input->post('prod_des'),
'price' => $this->input->post('price'),
'prod_image'=> $file_name,
);
$this->product_model->insert_product($data);
redirect('product/addproduct');
} else {
$data=$this->generateCommonItems();
//display the add product form
//set the flash data error message if there is one
$this->data['prod_name'] = array(
'name' => 'name',
'id' => 'name',
'type' => 'text',
'style' => 'width:300px;',
'value' => $this->form_validation->set_value('name'),
);
$this->data['prod_des'] = array(
'name' => 'description',
'id' => 'description',
'type' => 'text',
'cols' => 60,
'rows' => 5,
'value' => $this->form_validation->set_value('description'),
);
$this->data['price'] = array(
'name' => 'price',
'id' => 'price',
'type' => 'text',
'style' => 'width:40px;text-align: right',
'value' => $this->form_validation->set_value('price'),
);
$this->data['prod_image'] = array(
'value' => $this->form_validation->set_value('prod_image'),
);
$this->load->view('includes/header',$data);
$this->load->view('products/product_form', $this->data);
$this->load->view('includes/footer',$data);
}
}
This is what i use in my CI projects, hope it helps!
You can upload images with form by giving it an attribute of enctype.
<?php echo form_open_multipart('product/addproduct');?>
<input type="file" name="userfile" size="20" />
<input type="submit" value="Submit">
<?php echo form_close();?>
CodeIgniter also have some helper classes that can be found in the documentation. Here's one:
http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

CakePHP doesn't show uploaded picture

i'm truly getting crazy!
I made a gallery with Meiouploader and PHPThumb. All is working very nice.
My uploaded images saved in folder img/uploads/images and in my database too.
But in the field for showing the images I only see the alt-text. Not the images.
But when In check the HTML-Code, I see the correct path to my images. But I don't see it.
What wrong???
Please help!
OK, here is all my code:
I think the paths are correct, because in source code in my browser i can see the image - Tag. Here is my code for Image-Model:
class Image extends AppModel {
var $name = 'Image';
var $validate = array(
'gallery_id' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'name' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
//'img_file' => array(
//'notempty' => array(
//'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
//),
//),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $belongsTo = array(
'Gallery' => array(
'className' => 'Gallery',
'foreignKey' => 'gallery_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
var $actsAs = array(
'MeioUpload' => array(
'img_file' => array(
'dir' => 'img{DS}uploads{DS}images',
'create_directory' => false,
'allowed_mime' => array('image/jpeg', 'image/pjpeg', 'image/png'),
'allowed_ext' => array('.jpg', '.jpeg', '.png'),
'zoomCrop' => true,
'thumbnails' => true ,
'thumbnailQuality' => 75,
'thumbnailDir' => 'thumb',
'removeOriginal' => true,
'thumbsizes' => array(
'normal' => array('width' => 400, 'height' => 300),
),
'default' => 'default.jpg'
)
)
);
}
Here is my code for the Images-Controller:
class ImagesController extends AppController {
var $name = 'Images';
function index() {
$this->Image->recursive = 0;
$this->set('images', $this->paginate());
}
function view($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid image', true));
$this->redirect(array('action' => 'index'));
}
$this->set('image', $this->Image->read(null, $id));
}
function add() {
if (!empty($this->data)) {
$this->Image->create();
if ($this->Image->save($this->data)) {
$this->Session->setFlash(__('The image has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The image could not be saved. Please, try again.', true));
}
}
$galleries = $this->Image->Gallery->find('list');
$this->set(compact('galleries'));
}
function edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid image', true));
$this->redirect(array('action' => 'index'));
}
if (!empty($this->data)) {
if ($this->Image->save($this->data)) {
$this->Session->setFlash(__('The image has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The image could not be saved. Please, try again.', true));
}
}
if (empty($this->data)) {
$this->data = $this->Image->read(null, $id);
}
$galleries = $this->Image->Gallery->find('list');
$this->set(compact('galleries'));
}
function delete($id = null) {
if (!$id) {
$this->Session->setFlash(__('Invalid id for image', true));
$this->redirect(array('action'=>'index'));
}
if ($this->Image->delete($id)) {
$this->Session->setFlash(__('Image deleted', true));
$this->redirect(array('action'=>'index'));
}
$this->Session->setFlash(__('Image was not deleted', true));
$this->redirect(array('action' => 'index'));
}
}
Here is my code for index.ctp - View:
<div class="images index">
<h2><?php __('Images');?></h2>
<table cellpadding="0" cellspacing="0">
<tr>
<th><?php echo $this->Paginator->sort('id');?></th>
<th><?php echo $this->Paginator->sort('gallery_id');?></th>
<th><?php echo $this->Paginator->sort('name');?></th>
<th><?php echo $this->Paginator->sort('img_file');?></th>
<th class="actions"><?php __('Actions');?></th>
</tr>
<?php
$i = 0;
foreach ($images as $image):
$class = null;
if ($i++ % 2 == 0) {
$class = ' class="altrow"';
}
?>
<tr<?php echo $class;?>>
<td><?php echo $image['Image']['id']; ?> </td>
<td>
<?php echo $this->Html->link($image['Gallery']['name'], array('controller' => 'galleries', 'action' => 'view', $image['Gallery']['id'])); ?>
</td>
<td><?php echo $image['Image']['name']; ?> </td>
<!--<td><?php echo $image['Image']['img_file']; ?> </td>-->
<td><?php echo $html->image('uploads' . DS . 'images' . DS . $image['Image']['img_file'], array('alt' => 'Gallery Image', 'width' => '400')); ?></td>
<td class="actions">
<?php echo $this->Html->link(__('View', true), array('action' => 'view', $image['Image']['id'])); ?>
<?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $image['Image']['id'])); ?>
<?php echo $this->Html->link(__('Delete', true), array('action' => 'delete', $image['Image']['id']), null, sprintf(__('Are you sure you want to delete # %s?', true), $image['Image']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<p>
<?php
echo $this->Paginator->counter(array(
'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)
));
?>
</p>
<div class="paging">
<?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?>
<?php echo $this->Paginator->numbers();?>
<?php echo $this->Paginator->next(__('next', true) . ' >>', array(), null, array('class' => 'disabled'));?>
</div>
</div>
<div class="actions">
<h3><?php __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('New Image', true), array('action' => 'add')); ?></li>
<li><?php echo $this->Html->link(__('List Galleries', true), array('controller' => 'galleries', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Gallery', true), array('controller' => 'galleries', 'action' => 'add')); ?> </li>
</ul>
</div>
And here is my code for the add.ctp - View:
<div class="images form">
<?php // echo $this->Form->create('Image');?>
<?php echo $form->create('Image',array('type' => 'file')); ?>
<fieldset>
<legend><?php __('Add Image'); ?></legend>
<?php
echo $this->Form->input('gallery_id');
echo $this->Form->input('name');
//echo $this->Form->input('img_file');
echo $form->input('img_file', array('type' => 'file'));
?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true));?>
</div>
<div class="actions">
<h3><?php __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Images', true), array('action' => 'index'));?></li>
<li><?php echo $this->Html->link(__('List Galleries', true), array('controller' => 'galleries', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Gallery', true), array('controller' => 'galleries', 'action' => 'add')); ?> </li>
</ul>
</div>
I did all like in the tutorial of Jason Whydro, but it doesn't work well. It don't show me the pictures in this field, only the alt-text within and the width.
When I click on link to one of these images in my source code in my browser, then he says me: There is no object. The URL coudn't found on server!!
I hope it's enough for you to see whats going wrong. I don't see it. What did you mean with User Permission? How can I fix it, if this is the problem. I work with windows 8.
Greetings...
If your path is correctly it means that when you put this on your url bar at your browser , it shoud appears.
When it doesn't , could be a permission issue. Try to check your files permission to your http user.

Invalid argument supplied for foreach() in codeigniter (tiara)

i get error in the CI view page, as Invalid argument supplied for foreach().
this is my code :
my database:
(table of Penyewa)
**id_penyewa
nama_penyewa
alamat
no_telp**
(table of Jaminan)
**id_penyewa
jenis_jaminan
ket_jaminan**
in Controller (penyewa.php)
function Penyewa()
{
parent::Controller();
$this->load->database();
$this->load->model('model_tampil');
}
function tambah()
{
$ck=$this->input->post('id_penyewa');
if($ck!='')
{
$this->model_tampil->insertPenyewa();
}
$this->load->view('tampilpenyewa');
}
function sukses()
{
echo "Data berhasil di input!";
?>
<br />
Tambah Data
<br />
Lihat Data
<?php
}
in Model (model_tampil.php)
function getPenyewa(){
$this->db->select('*');
$this->db->from('penyewa');
$this->db->join('jaminan','jaminan.id_penyewa = penyewa.id_penyewa');
$q = $this->db->get();
$rows = $q->num_rows();
$q_result = $q->result();
if($rows>0){
foreach($q_result as $row){
$data[] = $row;
}
return $data;
}
}
function insertPenyewa()
{
/*$this->db->trans_start();
$this->db->query('INSERT INTO penyewa VALUES($id_penyewa, $nama_penyewa, $alamat, $no_telp, $jenis_jaminan)');
$table1_id = $this->db->insert_id();
$this->db->query('INSERT INTO jaminan VALUES(id_penyewa,' . $table1_id .',jenis_jaminan)');
$this->db->trans_complete(); */
$this->id_penyewa=$this->input->post('id_penyewa');
$this->nama_penyewa=$this->input->post('nama_penyewa');
$this->alamat=$this->input->post('alamat');
$this->no_telp=$this->input->post('no_telp');
$this->jenis_jaminan=$this->input->post('jenis_jaminan');
$this->db->insert('penyewa',$this);
redirect('penyewa/sukses');
}
in views (tampilPenyewa.php)
<center>
<h3>Tabel data Penyewa</h3>
<table border="1">
<tr align="center" bgcolor="#33CC99">
<td width="100">ID Penyewa</td>
<td width="200">Nama Penyewa</td>
<td width="120">Alamat</td>
<td width="150">Nomor Telepon</td>
<td width="150">Jenis Jaminan</td>
<td>Tindakan Lanjut</td>
</tr>
<?php foreach ($records as $row) : ?>
<tr height="35">
<td> <?php echo $row->id_penyewa; ?></td>
<td> <?php echo $row->nama_penyewa; ?></td>
<td> <?php echo $row->alamat; ?></td>
<td> <?php echo $row->no_telp; ?></td>
<td> <?php echo $row->ket_jaminan; ?></td>
<td></td>
</tr>
<?php endforeach; ?>
</table>
<br />
Tambah Data
and in views too (inputpenyewa.php)
<center>
<?php
$this->load->library('validation');
$id_penyewa=array(
'name' => 'id_penyewa',
'id' => 'id_penyewa',
'value' => '',
'maxlength' => '100',
'size' => '50',
'validation' => "required");
$nama_penyewa=array(
'name' => 'nama_penyewa',
'id' => 'nama_penyewa',
'value' => '',
'maxlength' => '100',
'size' => '50',
'validation' => "required");
$alamat=array(
'name' => 'alamat',
'id' => 'alamat',
'value' => '',
'maxlength' => '500',
'size' => '',
'validation' => "required");
$no_telp=array(
'name' => 'no_telp',
'id' => 'no_telp',
'value' => '',
'maxlength' => '100',
'size' => '50',
'validation' => "required");
$ket_jaminan=array(
'name' => 'jenis_jaminan',
'id' => 'jenis_jaminan',
'value' => '',
'maxlength' => '100',
'size' => '50',
'validation' => "required");
$this->load->helper('form');
echo validation_errors();
echo form_open('penyewa/tambah');
echo '<center><h3>Input Data Penyewa</h3></center>';
echo "<table border='0' class='tabledetail' align='center'>";
echo
"<tr>"."<td>".form_label('ID')."</td>"."<td>".form_input('id_penyewa')."</td>"."</tr>";
echo
"<tr height=50>"."<td>".form_label('Nama Penyewa')."</td>"."<td>".form_input('nama_penyewa')."</td>"."</tr>";
echo
"<tr height=220>"."<td>".form_label('Alamat')."</td>"."<td>".form_textarea('alamat')."</td>"."</tr>";
echo
"<tr>"."<td>".form_label('No Telp')."</td>"."<td>".form_input('no_telp')."</td>"."</tr>";
echo
"<tr height=220>"."<td>".form_label('Jaminan')."</td>"."<td>".form_textarea('jenis_jaminan')."</td>"."</tr>";
echo
"<tr height=50>"."<td colspan=2 align='center'>".form_submit('mysubmit','Simpan')."</td>"."</tr>";
echo "</table>";
echo form_close();
?>
Lihat Data
okay, i am sorry if my question is very much and much..
thank you :)
update:
okey, i've doing that, but nothing result, my page error again..
and, i want to ask again, any mistake in model_tampil->getPenyewa() ??
this is the script :
function getPenyewa(){
$this->db->select('*');
$this->db->from('penyewa');
$this->db->join('jaminan','jaminan.id_penyewa = penyewa.id_penyewa');
$q = $this->db->get();
$rows = $q->num_rows();
$q_result = $q->result();
if($rows>0){
foreach($q_result as $row){
$data[] = $row;
}
return $data;
}
}
i want to input data to 2 table in my localhost, from 1 page, inputpenyewa.php..
You are not passing any data to the view.
$this->load->view('tampilpenyewa');
change this line to this.
$this->load->view('tampilpenyewa',$data);
You have to get the values from db. So before this line ,get value from db as
$data['records'] = $this->model_tampil->getPenyewa();
and then load the view as above, like
$this->load->view('tampilpenyewa',$data);
Hope this helps
Regards
iijb
In the tambah function of your controller,you are directly calling the view file without giving any data to it..
Do it like this..
function tambah()
{
$ck=$this->input->post('id_penyewa');
if($ck!='')
{
$this->model_tampil->insertPenyewa();
}
$records = $this->model_tampil->getPenyewa(); // The array returned from your whatever model function
$this->load->view('tampilpenyewa',array('records' => $records));
}

Resources