I would like to know if it is possible to get a $variable id mixed in with the codeigniter.
Currently just shows http://localhost/codeigniter/codeigniter-cms/0
I just need it to display http://localhost/codeigniter/codeigniter-cms/admin/setting with id hidden and still access this controller.
But If create new website it has different ID so different settings.
foreach ($results as $result) {
$data['websites'][] = array(
'website_id' => $result->website_id,
'name' => $result->name,
'url' => $result->url,
'edit' => site_url('admin/setting') . $result->website_id
);
}
View
<?php if ($websites) { ?>
<?php foreach($websites as $website) { ?>
<tr>
<td class="text-center"><?php if (in_array($website['website_id'], $selected)) { ?>
<input type="checkbox" name="selected[]" value="<?php echo $website['website_id']; ?>" checked="checked" />
<?php } else { ?>
<input type="checkbox" name="selected[]" value="<?php echo $website['website_id']; ?>" />
<?php } ?></td>
<td><?php echo $website['name'];?></td>
<td><?php echo $website['url'];?> </td>
<td class="text-right"><i class="fa fa-pencil"></i> Edit Website</td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td class="text-center" colspan="4">NO RESULTS</td>
</tr>
<?php } ?>
I can view link now changed edit on controller and made on model result_array instead of result. link working fine.
'edit' => site_url('admin/setting', $result['website_id'])
controller updated
$results = $this->model_website->getWebsites();
foreach ($results as $result) {
$data['websites'][] = array(
'website_id' => $result['website_id'],
'name' => $result['name'],
'url' => $result['url'],
'edit' => site_url('admin/setting', $result['website_id'])
);
}
model updated
function getWebsites() {
$this->db->order_by('url', 'asc');
$query = $this->db->get('website');
if($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}
Related
How I can get street address 1 and street address 2 value. I create an extension and I want to get street address in Magento. In the block I have this function:
public function updateFormData() {
$data = $this->getData('form_data');
if (is_null($data)) {
/** #var array $formData */
$formData = Mage::getSingleton('customer/session')->getCustomerFormData(true);
$order = $this->getOrder();
$address = $order->getShippingAddress();
$customerData = [
'email' => $order->getCustomerEmail(),
'firstname' => $order->getCustomerFirstname(),
'lastname' => $order->getCustomerLastname(),
'city' => $order->getBillingAddress()->getCity(),
'country' => $order->getBillingAddress()->getCountry(),
'telephone' => $order->getBillingAddress()->getTelephone(),
'company' => $order->getBillingAddress()->getCompany(),
];
$data = new Varien_Object();
if ($formData) {
$data->addData($formData);
$data->setCustomerData(1);
}
$data->addData($customerData);
if (isset($data['region_id'])) {
$data['region_id'] = (int)$data['region_id'];
}
$this->setData('form_data', $data);
}
return $data;
}
and in the phtml file I add this:
<?php $_streetValidationClass = $this->helper('customer/address')->getAttributeValidationClass('street'); ?>
<li class="wide">
<label for="street_1" class="required"><em>*</em><?php echo $this->__('Street Address') ?></label>
<div class="input-box">
<input type="text" name="street[]" value="<?php echo $this->escapeHtml($this->getFormData()->getStreet(1)) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Street Address')) ?>" id="street_1" class="input-text <?php echo $_streetValidationClass ?>" />
</div>
</li>
<?php $_streetValidationClass = trim(str_replace('required-entry', '', $_streetValidationClass)); ?>
<?php for ($_i = 2, $_n = $this->helper('customer/address')->getStreetLines(); $_i <= $_n; $_i++): ?>
<li class="wide">
<div class="input-box">
<input type="text" name="street[]" value="<?php echo $this->escapeHtml($this->getFormData()->getStreet($_i)) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Street Address %s', $_i)) ?>" id="street_<?php echo $_i ?>" class="input-text <?php echo $_streetValidationClass ?>" />
</div>
</li>
<?php endfor; ?>
I try to add this 'street_1' => $order->getBillingAddress()->getData('street'),
But always street_1 address value is blank. I try to get all fields in the order success page.
OUTPUT:
<li class="wide">
<label class="required" for="street_1">
<div class="input-box">
<input id="street_1" class="input-text required-entry" type="text" title="Street Address" value="address1 address2" name="street[]">
</div>
</li>
<li class="wide">
<div class="input-box">
<input id="street_2" class="input-text " type="text" title="Street Address 2" value="" name="street[]">
</div>
</li>
$order->getBillingAddress()->getData('street') will return an array.
So you can access the data with:
$billingStreet = $order->getBillingAddress()->getData('street');
$billingStreet1 = $billingStreet[0];
$billingStreet2 = $billingStreet[1];
$billingStreet3 = $billingStreet[2];
edit: the code above is to get the billing address of an order, what I think you want now is actually the new address, the data set in the form. So this should give you the information you're after, as long as you put it before the $data = new Varien_Object(); line:
$streetArray = $data['street'];
$street1 = $streetArray[0];
$street2 = $streetArray[1];
so your function will be:
public function updateFormData() {
$data = $this->getData('form_data');
if (is_null($data)) {
/** #var array $formData */
$formData = Mage::getSingleton('customer/session')->getCustomerFormData(true);
$order = $this->getOrder();
$address = $order->getShippingAddress();
$streetArray = $data['street'];
$street1 = $streetArray[0];
$street2 = $streetArray[1];
$customerData = [
'email' => $order->getCustomerEmail(),
'firstname' => $order->getCustomerFirstname(),
'lastname' => $order->getCustomerLastname(),
'city' => $order->getBillingAddress()->getCity(),
'country' => $order->getBillingAddress()->getCountry(),
'telephone' => $order->getBillingAddress()->getTelephone(),
'company' => $order->getBillingAddress()->getCompany(),
'street_1' => $street1,
'street_2' => $street2
];
$data = new Varien_Object();
if ($formData) {
$data->addData($formData);
$data->setCustomerData(1);
}
$data->addData($customerData);
if (isset($data['region_id'])) {
$data['region_id'] = (int)$data['region_id'];
}
$this->setData('form_data', $data);
}
return $data;
}
Hi I am new with ajax and having problem. I am trying to make a shopping cart using ajax.I don't know what is wrong with my code please help me out.
When I click the add button an alert comes 'No success' and nothing happen , I am not able add items to cart.
Thanks for helping me.
This is my view
<html>
<head>
<title>Codeigniter cart class</title>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link href='http://fonts.googleapis.com/css?family=Raleway:500,600,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/style.css">
<script type="text/javascript">
$(document).ready(function() {
$("#myform").submit(function(event) {
event.preventDefault();
var insert_data= $(this).serializeArray();
$.ajax({
url: "<?php echo base_url(); ?>" + "index.php/shopping2/add",
dataType: 'json',
//type: "POST",
data: insert_data,
success:function(response) {
//if (response){
//var res = JSON.parse(response);
var res = response;
if(res.status == 200){
window.location.href="http://127.0.0.1/codeigniter_cart2/index.php/shopping2";
}else{
alert(res.msg);
}
//}
//else{
// alert('sorry');
//}
}
});
});
});
</script>
</head>
<body>
<div id='content'>
<div class="row">
<div class="col-sm-5">
<h2 align="center">Items</h2>
<?php
?>
<table id="table" border="0" cellpadding="5px" cellspacing="1px">
<?php
foreach ($products as $product) {
$id = $product['serial'];
$name = $product['name'];
$price = $product['price'];
?>
<tr class="well">
<td style="padding-left:15px;"><?php echo $name; ?></td>
<td>
Rs. <?php echo $price; ?></td>
<?php
?>
<?php
echo form_open('',array('id' => 'myform'));
echo form_hidden('id', $id);
echo form_hidden('name', $name);
echo form_hidden('price', $price);
?> <!--</div>-->
<?php
$btn = array(
'class' => 'fg-button teal',
'value' => 'Add',
'name' => 'action',
'id' => 'add_button'
);
?>
<td>
<?php
// Submit Button.
echo form_submit($btn);
echo form_close();
?>
</td>
</tr>
<?php } ?>
</table>
</div>
<div class="col-sm-7">
<!-- <div id="cart" >-->
<h2 align="center">Items on Cart</h2>
<div>
<?php $cart_check = $this->cart->contents();
if(empty($cart_check)) {
echo 'To add products to your shopping cart click on "Add" Button';
} ?> </div>
<table id="table" border="0" cellpadding="5px" cellspacing="1px">
<?php
// All values of cart store in "$cart".
if ($cart = $this->cart->contents()): ?>
<div id="addcart">
<tr id= "main_heading" class="well">
<td style="padding-left:15px;"><?>Name</td>
<td>Price(Rs)</td>
<td>Qty</td>
<td>Amount</td>
<td>Remove</td>
</tr>
<?php
// Create form and send all values in "shopping/update_cart" function.
echo form_open('shopping2/update_cart');
$grand_total = 0;
$i = 1;
foreach ($cart as $item):
echo form_hidden('cart[' . $item['id'] . '][id]', $item['id']);
echo form_hidden('cart[' . $item['id'] . '][rowid]', $item['rowid']);
echo form_hidden('cart[' . $item['id'] . '][name]', $item['name']);
echo form_hidden('cart[' . $item['id'] . '][price]', $item['price']);
echo form_hidden('cart[' . $item['id'] . '][qty]', $item['qty']);
?>
<tr class="well" id="addcart">
<td style="padding-left:15px;">
<?php echo $item['name']; ?>
</td>
<td>
<?php echo number_format($item['price'], 2); ?>
</td>
<td>
<?php echo form_input('cart[' . $item['id'] . '][qty]', $item['qty'], ' type="number" max="99" min="1" value="1" style="width:50px;"'); ?>
</td>
<?php $grand_total = $grand_total + $item['subtotal']; ?>
<td>
Rs <?php echo number_format($item['subtotal'], 2) ?>
</td>
<td>
<?php
// cancle image.
$path = "<img src='http://127.0.0.1/codeigniter_cart2/images/cart_cross.jpg' width='25px' height='20px'>";
echo anchor('shopping/remove/' . $item['rowid'], $path); ?>
</td>
<?php endforeach; ?>
</tr>
<tr>
<td style="padding-left:30px;"><b>Order Total: Rs <?php
//Grand Total.
echo number_format($grand_total, 2); ?></b></td>
<td colspan="5" align="right"><input type="button" class ='fg-button teal' value="Clear cart" onclick="window.location = 'shopping2/remove'">
<?php //submit button. ?>
<input type="submit" class ='fg-button teal' value="Update Cart">
<?php echo form_close(); ?>
</td>
</tr></div>
<?php endif; ?>
</table>
</div>
<!-- <div id="products_e" align="center">-->
<!--</div>-->
<!-- </div>-->
</div>
</div>
</body>
</html>
This is my controller
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Shopping2 extends CI_Controller {
public function __construct()
{
parent::__construct();
//load model
$this->load->model('billing_model');
$this->load->library('cart');
}
public function index()
{
$data['products'] = $this->billing_model->get_all();
$this->load->view('shopping_view2', $data);
}
function add()
{
$insert_data = array(
'id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'price' => $this->input->post('price'),
'qty' => 1
);
$this->cart->insert($insert_data);
//$success = $this->cart->insert($insert_data);
$cart_check = $this->cart->contents();
if(!empty($cart_check)){
//$this->cart->contents(insert_data);
$res = array('status' => 200, 'msg' => 'success');
}else{
$res = array('status' => 500, 'msg' => 'No success');
}
echo json_encode($res);
//echo $data[0]['value'];
//redirect('shopping2');
}
function remove($rowid) {
// Check rowid value.
if ($rowid==="all"){
$this->cart->destroy();
}else{
$data = array(
'rowid' => $rowid,
'qty' => 0
);
$this->cart->update($data);
}
redirect('shopping2');
}
function update_cart(){
// Recieve post values,calcute them and update
$cart_info = $_POST['cart'] ;
foreach( $cart_info as $id => $cart)
{
$rowid = $cart['rowid'];
$price = $cart['price'];
$amount = $price * $cart['qty'];
$qty = $cart['qty'];
$data = array(
'rowid' => $rowid,
'price' => $price,
'amount' => $amount,
'qty' => $qty
);
$this->cart->update($data);
}
redirect('shopping2');
}
}
Please help me how to add items in the cart using ajax.
Since u managed to get a response, the problem should lie in your Model or data insertion.
Few suggestions:
It's also recommended that you use $.post() instead of $.ajax().
"<?php echo base_url(); ?>" + "index.php/shopping2/add" can be changed to "<?php echo site_url('shopping2/add"'); ?>"
Use the built in Codeignter output class to output your JSON. See here: https://www.codeigniter.com/userguide3/libraries/output.html
My model is not inserting my modify and access data that I select on my view.If my select option on my view is select option 1 it inserts value 0 even though 1 selected. Not sure why on model or view its not inserting the correct one?
Each controller has its own modify and access select option i would like to be able to insert if select 1 for enable or 0 disbale?
How can I make the both select options work with my model so inserts correct what have chosen.
Model Function
<?php
class Model_user_group extends CI_Model {
public function addUserGroup() {
$name = $this->input->post('name');
$controllers = $this->input->post('controller');
$access = $this->input->post('access');
$modify = $this->input->post('modify');
for($i=0;$i<count($controllers);$i++) {
$data = array(
'name' => strtolower($name),
'controller' => $controllers[$i],
'access' => $access,
'modify' => $modify
);
$this->db->insert($this->db->dbprefix . 'user_group', $data);
}
}
View
<?php echo Modules::run('admin/common/header/index');?>
<div id="wrapper">
<?php echo Modules::run('admin/common/menu/index');?>
<div id="page-wrapper" >
<div id="page-inner">
<div class="panel panel-default">
<div class="panel-heading clearfix">
<div class="pull-left" style="padding-top: 7.5px"><h1 class="panel-title"><?php echo $title;?></h1></div>
<div class="pull-right">
Cancel
<button type="submit" onclick="submit()" class="btn btn-primary">Save</button>
</div>
</div>
<div class="panel-body">
<?php echo validation_errors('<div class="alert alert-warning text-center">', '</div>'); ?>
<?php $data = array('class' => 'form-horizontal', 'id' => 'form-users-group');?>
<?php echo form_open_multipart('admin/users_group_add', $data);?>
<div class="form-group">
<?php $data = array('class' => 'col-sm-2');?>
<?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' => set_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 ucfirst(str_replace("_"," ", $controller));?>
<input type="hidden" name="controller[]" value="<?php echo $controller;?>" />
</td>
<td>
<select name="access" class="form-control">
<option value="0">Disabled</option>
<option value="1">Enabled</option>
</select>
</td>
<td>
<select name="modify" class="form-control">
<option value="0">Disabled</option>
<option value="1">Enabled</option>
</select>
</td>
</tr>
</tbody>
<?php } ?>
</table>
<?php echo form_close();?>
</div>
</div>
</div><!-- # Page Inner End -->
</div><!-- # Page End -->
</div><!-- # Wrapper End -->
<?php echo Modules::run('admin/common/footer/index');?>
Your view file seems has access and modify option for each controller. But your input name is same.So the value of $access and $modify is the last input value. You can solve this way.Give access and modify input name as array
<select name="access[]" class="form-control">
...
<select name="modify[]" class="form-control">
Now your model
$name = $this->input->post('name');
$controllers = $this->input->post('controller');
$accesses = $this->input->post('access');
$modifies = $this->input->post('modify');
for($i=0;$i<count($controllers);$i++) {
$data = array(
'name' => strtolower($name),
'controller' => $controllers[$i],
'access' => $accesses[$i],
'modify' => $modifies[$i]
);
$this->db->insert($this->db->dbprefix . 'user_group', $data);
}
Hope you understand and solves your problem
On my form I have some check boxes. And they are in an array. When I submit my form for some reason it clears the Modify check boxes. And then when go back to look not checked.
I have know what's causing issue is $this->form_validation->set_rules('permission[modify]', '', 'callback_modify_check_edit')
It does not seem to like, permission[modify] or permission[modify][] on the set_rules
How am I able to solve this?
Controller Edit Function:
public function edit() {
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'User Group Name', 'required');
$this->form_validation->set_rules('user_group_id', 'User Group Id', 'required');
$this->form_validation->set_rules('permission[modify]', '', 'callback_modify_check_edit');
if ($this->form_validation->run($this) == FALSE) {
$this->getForm();
} else {
$this->load->model('admin/user/model_user_group');
$this->model_user_group->editUserGroup($this->uri->segment(4), $this->input->post());
$this->db->select('permission');
$this->db->from($this->db->dbprefix . 'user_group');
$this->db->where('user_group_id', $this->session->userdata('user_group_id'));
$user_group_query = $this->db->get();
$permissions = unserialize($user_group_query->row('permission'));
$this->session->set_userdata($permissions);
$this->session->set_flashdata('success', 'Congratulations you have successfully modified' .' '. "<strong>" . ucwords(str_replace('_', ' ', $this->router->fetch_class())) .' '. $this->input->post('name') . "</strong>");
redirect('admin/users_group');
}
}
public function modify_check_edit() {
if (!in_array('users_group', $this->session->userdata('modify'))) {
$this->form_validation->set_message('modify_check_edit', 'You do not have permission to edit' );
}
}
View Form:
<?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' => 'users_group');?>
<?php echo form_open('admin/users_group/add', $data);?>
<?php } else { ?>
<?php $data = array('class' => 'form-horizontal', 'id' => '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
$data_user_name = array(
'id' => 'name',
'name' => 'name',
'class' => 'form-control',
'value' => $name
)
;?>
<?php echo form_input($data_user_name);?>
</div>
</div>
<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php echo form_label('User Group Id', 'user_group_id', $data);?>
<div class="col-sm-10">
<?php
$data_user_group_id = array(
'id' => 'user_group_id',
'name' => 'user_group_id',
'class' => 'form-control',
'value' => $user_group_id
)
;?>
<?php echo form_input($data_user_group_id);?>
</div>
</div>
<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php echo form_label('Access Permission', 'permission_access', $data);?>
<div class="col-sm-10">
<div class="well well-sm" style="height: 200px; overflow: auto;">
<?php foreach ($permissions as $permission) { ?>
<div class="checkbox">
<label>
<?php if (in_array($permission, $access)) { ?>
<?php
$data_checked = array(
'name' => 'permission[access][]',
'id' => 'permission_access',
'value' => $permission,
'checked' => TRUE,
);
echo form_checkbox($data_checked);
?>
<?php echo $permission; ?>
<?php } else { ?>
<?php
$data_not_checked = array(
'name' => 'permission[access][]',
'id' => 'permission_access',
'value' => $permission,
'checked' => FALSE,
);
echo form_checkbox($data_not_checked);
?>
<?php echo $permission; ?>
<?php } ?>
</label>
</div>
<?php } ?>
</div>
<a onclick="$(this).parent().find(':checkbox').prop('checked', true);">Select All</a> / <a onclick="$(this).parent().find(':checkbox').prop('checked', false);">Unselect All</a></div>
</div>
<div class="form-group">
<?php $data = array('class' => 'col-sm-2 control-label');?>
<?php echo form_label('Modify Permission', 'permission_modify', $data);?>
<div class="col-sm-10">
<div class="well well-sm" style="height: 200px; overflow: auto;">
<?php foreach ($permissions as $permission) { ?>
<div class="checkbox">
<label>
<?php if (in_array($permission, $modify)) { ?>
<?php
$data = array(
'name' => 'permission[modify][]',
'id' => 'permission_modify',
'value' => $permission,
'checked' => TRUE,
);
echo form_checkbox($data);
?>
<?php echo $permission; ?>
<?php } else { ?>
<?php
$data = array(
'name' => 'permission[modify][]',
'id' => 'permission_modify',
'value' => $permission,
'checked' => FALSE,
);
echo form_checkbox($data);
?>
<?php echo $permission; ?>
<?php } ?>
</label>
</div>
<?php } ?>
</div>
<a onclick="$(this).parent().find(':checkbox').prop('checked', true);">Select All</a> / <a onclick="$(this).parent().find(':checkbox').prop('checked', false);">Unselect All</a></div>
</div>
<?php echo form_close();?>
After a good half hour trying to figure it out it was mostly part of the call back not to working with th set_rules() for permission[modify]
Just fixed code here for call back and seems to be working now.
public function modify_check_edit() {
$this->load->library('form_validation');
if (is_array($this->session->userdata('modify'))) {
$permission = !in_array('users_group', $this->session->userdata('modify'));
if ($permission) {
$this->form_validation->set_message('modify_check_edit', 'You do not have permission to one' .' '. "<strong>" . ucwords(str_replace('_', ' ', $this->router->fetch_class())) .' '. $this->input->post('name') . "</strong>");
return FALSE;
} else {
return TRUE;
}
return TRUE;
} else {
$this->form_validation->set_message('modify_check_edit', 'You do not have permission to one' .' '. "<strong>" . ucwords(str_replace('_', ' ', $this->router->fetch_class())) .' '. $this->input->post('name') . "</strong>");
return FALSE;
}
}
i have some problem with update in CI 2.1
I followed the user guide "mini-tut" for create e news, but i cant understand how to update a record with a form.
my update models is:
// update dei record
public function update_news($id)
{
$data = array(
'title' => $this->input->post('title'),
'slug' => $this->input->post('slug'),
'text' => $this->input->post('text')
);
$this->db->where('id', $id);
$this->db->update('news', $data);
}
how can i make the controller for update?? i try:
public function update($id)
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Update an intem';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/update');
$this->load->view('templates/footer');
}
else
{
$this->news_model->update_news($id);
$this->load->view('news/success');
}
}
but i display a 404() page...
the views for update is:
<h2>Update an item</h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/update') ?>
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="slug">Slug</label>
<input type="input" name="slug" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Update an item" />
</form>
any one can help me how do a "simnple" update for understand a CI logic?
If I understand what you want, then you should just pass the $this->input->post(x) to the model from your controller.
I personally have been using it like this:
Controller:
$data = array(
'title' => $this->input->post('title'),
'text' => $this->input->post('text'),
'slug' => $this->input->post('slug'),
);
if($this->my_model->exists($id)) {
$this->my_model->update($id, $data);
} else {
$this->my_model->insert($data);
}
And your Model should look like:
// update dei record
public function update($id, $data)
{
$this->db->where('id', $id);
$this->db->update('news', $data);
}
Your controller uses the first segment as an argument of the controller method:
public function update($id)
You can also try using
$id = $this->uri->segment(3);
//Controller
public function update($id)
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Update an intem';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');
$data['news'] = $this->news_model->get_news_by_id($id);
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/update', $data);
$this->load->view('templates/footer');
}
else
{
$this->load->helper('url');
$this->news_model->update_news($id);
redirect('/news', 'refresh');
}
}
//Model
public function update_news($id)
{
$data = array(
'title' => $this->input->post('title'),
'slug' => $this->input->post('title'),
'text' => $this->input->post('text')
);
$this->db->where('id', $id);
$this->db->update('news', $data);
}
View
<label for="title">Title</label>
<input type="input" name="title" value="<?php echo $news[0]['title'];?>" /><br />
<input type = "hidden" name="id" value="<?php echo $news[0]['id'];?>" />
<label for="text">Text</label>
<textarea name="text"><?php echo $news[0]['text'];?></textarea><br />
<input type="submit" name="submit" value="Create news item" />
To add a page in code igniter
I will give you the example which i have done
Model:
function add()
{
$name=$_POST['name'];
$department=$_POST['dept'];
$degree=$_POST['degree'];
$mark=$_POST['mark'];
$this->db->query("INSERT INTO `simple`(name,department,degree,mark) VALUES('$name','$department','$degree','$mark')");
}
Controller:
public function add()
{
$this->load->model('modd');
$this->modd->add();
$this->load->view('add');
}
Views :
add.php
<form method="post" action="<?php base_url();?>index.php/contr/add">
<table>
<?php echo form_open('contr/add');?>
<tr>
<th>Name</th>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<th>Department</th>
<td><input type="text" name="dept" /></td>
</tr>
<tr>
<th>Degree</th>
<td><input type="text" name="degree" /></td>
</tr>
<tr>
<th>Marks</th>
<td><input type="text" name="mark" /></td>
</tr>
<tr>
<td><input type="submit" name="add" value="Add" /></td>
<?php echo form_close(); ?>
<td>
<input type="button" value="view" />
</td>
</tr>
</table>