I'm having problems trying to implement the Codeigniter class in my application. The issue is that when I submit my form, the function that executes is not saving the file to my specified folder and it is not throwing an error message. My code is as follows:
View:
<?php echo form_open_multipart('requests/submit_candidate'); ?>
<table>
<tr>
<td><label for="text">Upload CV</label></td>
<td><input type="file" name="file"></textarea></td>
</tr>
<tr>
<td><label for="text">Extra Information</label></td>
<td><textarea name="extra_info"></textarea></td>
</tr>
</table>
<input type="submit" name="submit" value="Submit" />
</form>
Controller:
public function submit_candidate($slug)
{
$this->load->library('upload');
$config = array(
'upload_path' => dirname($_SERVER["SCRIPT_FILENAME"])."/uploads/",
'upload_url' => base_url()."uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|doc|xml",
'overwrite' => TRUE,
'max_size' => "1000KB",
'max_height' => "768",
'max_width' => "1024"
);
if($this->upload->do_upload($config))
{
echo "file upload success";
}
else
{
echo "file upload failed";
}
}
Can anybody help please?
Many thanks,
SR
Try this if it works for you:
1) HTML
<td><input type="file" name="file"></td>
2) Controller
public function submit_candidate($slug)
{
$this->load->library('upload');
$config = array(
'upload_path' => "./uploads/",
//'upload_url' => base_url()."uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf|doc|xml",
'overwrite' => TRUE,
'max_size' => 1000,
'max_height' => 768,
'max_width' => 1024
);
$this->upload->initialize( $config );
if(!$this->upload->do_upload('file')) // "file" is the name of the element default it's "userfile"
{
print_r( $this->upload->display_errors() );die; //display the error
}
else
{
$data = $this->upload->data();
print_r( $data );die; //file uploaded data
}
}
1) Inside the function you need to give the name of the form file element ( for you its "file" )
2) You have also used a config variable "upload_url" which is invalid.
3) You need to initialize the config for upload library.
Here (File Upload Documentation) is the reference for you.
Related
I retrieved form input in three ways in codeigniter? I am unsure which one is correct. I have given this line
$this->load->view('userview',$data);
Is this correct? What is the right way to input the data from the form? When should I use an array?
I also want to know if the record was added successfully. After submitting the form, which function would I have to use and where would I put it?
view folder file name userview.php
userview.php
<form name="f1" action="" method="post"/>
<table width="500" border="1">
<tr>
<td>UserName</td>
<td>:</td>
<td><input type="text" name="username" value=""/></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input type="password" name="password" value=""/></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input type="text" name="email" value=""/></td>
</tr>
<tr>
<td colspan="3"><input type="submit" name="submit" value="Save"/></td>
</tr>
</table>
</form>
First one-created Array and stored into variable:
public function index()
{
$data = array();
if($this->input->post('submit') != NULL ){
$postData = $this->input->post();
$data['response'] = $postData;
}
$this->load->view('userview',$data);
}
Second one: retrieved input variable within array
public function index()
{
$data['response']=array('username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'email' => $this->input->post('email'));
$this->load->view('userview',$data);
}
Third one: created one method within index function
public function index()
{
$this->load->view('userview');
$this->getvalue();
}
public function getvalue()
{
if($this->input->post('submit')!==null)
{
$data['response']=array('username' => $this->input->post('username'),
'password' => $this->input->post('password'),
'email' => $this->input->post('email'));
$this->load->view('viewuser',$data);
}
}
All are OK, Personally I use the following when I have multiple fields:
$UserDetails = $this->input->post(['username', 'email', 'password']);
This would return a key => value pair array that contains the 3 fields I need only.
In your first example, you might be returning extra fields that you don't need.
Your second example is very verbose for my taste but it's OK.
3rd example is also very verbose for my taste but it's OK.
I would use form validation instead of manually checking if the form is posted.
$data = [];
$form_validation = array(
['field' => 'username', 'label' => 'Username', 'rules' => 'trim|required'],
['field' => 'email', 'label' => 'Email', 'rules' => 'trim|required|email'],
['field' => 'password', 'label' => 'Password', 'rules' => 'trim|required'],
);
$this->form_validation->set_rules($form_validation);
if ( $this->form_validation->run() === false ) {
// set some error messages here
}else{
// get data here
$data['UserDetails'] = $this->input->post(['username', 'email', 'password']);
}
// pass data to view ??
$this->load->view('view', $data)
Here you go:
Form Validation: https://www.codeigniter.com/userguide3/libraries/form_validation.html
Input Class: https://www.codeigniter.com/userguide3/libraries/input.html
Form Helper: https://www.codeigniter.com/userguide3/helpers/form_helper.html
Good luck : )
I have a table in which I am adding rows dynamically using JS which works fine, I am able to add those text fields to db as well using for loop, now one of the row has to be an image uploader, by current code I am only able to save one image, the rest images from different rows doesnt save, please help. Thank You.
blade file:
<table class="table table-striped">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Price</th>
<th>Tags</th>
<th>Image</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="button" class="btn-link cursor-pointer" value="Add Menu Item" href="javascript:void(0);">
<input type="hidden" class="tbl_row" name="count_row[]" id="count_row[]" value="1"/>
</td>
<td>
{{ Form::text('item_name[]', null, ['id' => 'name', 'class' => 'form-control'] ) }}
</td>
<td>
{{ Form::text('price[]', null, ['id' => 'price', 'class' => 'form-control'] )}}
</td>
<td>
{{ Form::select('tags[]', $tags, null, ['class' => 'form-control']) }}
</td>
<td>
<input type="file" name="image[]">
</td>
<td>
{{ Form::textarea('description[]', null, ['id' => 'description', 'rows' => 2, 'class' => 'form-control']) }}
</td>
</tr>
</tbody>
</table>
Controller:
$start_count = (isset($end_count)) ? $end_count : 0;
$end_count = (int)$start_count + (int)$request->input('count_row')[$i];
$attachment = $request->file('image');
for ($ln = $start_count; $ln < $end_count; $ln++) {
if (isset($attachment[$i]) && is_file($attachment[$i])) {
$fileNameWithExt = $attachment[$i]->getClientOriginalName();
// Get just filename
$filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
// Get just ext
$extention = $attachment[$i]->getClientOriginalExtension();
// Filename to store
$fileNameToStore = $filename . $extention;
// Upload Image
$path = $attachment[$i]->storeAs('public/content-images/', $fileNameToStore);
} else {
$fileNameToStore = 'no-image.jpg';
}
$item = new MenuItem([
'category_id' => $category->id,
'name' => $request->item_name[$ln],
'price' => $request->price[$ln],
'description' => $request->description[$ln],
'status' => 1,
'image' => $fileNameToStore[$ln], **even the name is not saving properly in db**
]);
$item->save();
DB::table('menu_item_tag')->insert([
['menu_item_id' => $item->id, 'tag_id' => $request->tags[$ln]],
]);
}
Thanks to azeĆ³s's comment managed to solve by changing the code to this, had to change $i to $ln and fileNameToStore[$ln] to fileNameToStore in order to save proper filename to db:
$start_count = (isset($end_count)) ? $end_count : 0;
$end_count = (int)$start_count + (int)$request->input('count_row')[$i];
$attachment = $request->file('image');
for ($ln = $start_count; $ln < $end_count; $ln++) {
if (isset($attachment[$ln]) && is_file($attachment[$ln])) {
$fileNameWithExt = $attachment[$ln]->getClientOriginalName();
// Get just filename
$filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
// Get just ext
$extention = $attachment[$ln]->getClientOriginalExtension();
// Filename to store
$fileNameToStore = $filename . '.' . $extention;
// Upload Image
$path = $attachment[$ln]->storeAs('public/menu/'.$category->id.'/', $fileNameToStore);
} else {
$fileNameToStore = 'no-image.jpg';
}
$item = new MenuItem([
'category_id' => $category->id,
'name' => $request->item_name[$ln],
'price' => $request->price[$ln],
'description' => $request->description[$ln],
'status' => 1,
'image' => 'menu/' . $category->id . '/' . $fileNameToStore,
]);
$item->save();
DB::table('menu_item_tag')->insert([
['menu_item_id' => $item->id, 'tag_id' => $request->tags[$ln]],
]);
}
I have a problem while uploading image files.
This my view file contents
<form action="<?php base_url()."profile/upload"?>" method="POST" enctype="multipart/form-data" >
<input type="hidden" name="id_akun" value="<?php echo $query->id_akun?>"size="20" />
<input type="file" name="userfile" required>
<input type="submit" name="submit" value="Upload" class="btn btn-success" />
</form>
Here is my controller (profile)
public function upload() {
$config = array(
'upload_path' => dirname($_SERVER["SCRIPT_FILENAME"])."/upload/",
'upload_url' => base_url()."upload/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => false,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $this->config);
if($this->upload->do_upload()) {
echo "file upload success";
} else {
echo "file upload failed";
}
}
When I try an upload, the image did not get upload, and there is no error shown. URL in my address bar looks like this
http://localhost/mataramtest/profile?id_akun=6&userfile=1.PNG&submit=Upload
There are several issues on your code. Revised and given below
public function upload()
{
$config = array(
'upload_path' => "./upload/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => false,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if($this->upload->do_upload('userfile'))
{
echo "file upload success";
}
else
{
echo "file upload failed";
}
}
In view file
<form action="<?php echo site_url("profile/upload");?>" method="POST" enctype="multipart/form-data" >
<!--rest of the code is ok-->
public function img_upload()
{
$config = array(
'upload_path' => "uploads",
'allowed_types' => "*",
'overwrite' => TRUE,
'max_size' => "5048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "3000",
'max_width' => "3000"
);
$this->upload->initialize($config);
$this->load->library('upload', $config);
if($this->upload->do_upload()) {
$response = array('upload_data' => $this->upload->data());
}
else{
$error = array('error'=>$this->upload->display_errors());
print_r($error);
}
}
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
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));
}