Php error encounter codeigniter - php

I am trying to select and update the data from database. But when I click on update, it show message "Undefined variable: data". What is the problem regarding $data. ? I am not able to link with testing3.php to update my data.
//views/testing2.php
<?php foreach ($data as $row): ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->email; ?></td>
<td><?php echo anchor('testing3/view', 'Update'); ?></td>
</tr>
<?php endforeach; ?>
//views/testing3.php
<html>
<h1>Update</h1>
<form method="post" >
<?php foreach ($data as $row): ?>
<input type="hidden" name="id" value ="<?php echo $row->id; ?>">
<label>ID: </label><br>
<input type="text" name="name" value ="<?php echo $row->name; ?>">
<label>ID: </label><br>
<input type="text" name="email" value ="<?php echo $row->email; ?>">
<label>ID: </label><br>
<input type="submit" id="submit" name="submit" value ="update">
<label>ID: </label><br>
<?php endforeach; ?>
</form>
</html>
//controllers/testing3.php
<?php
//page name
class Testing3 extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('testing3_model');
}
function showid()
{
$data['data']=$this->testing3_model->getsitemodel();
$data['data']=$this->testing3_model->showid($id);
$this->load->vars($data);
$this->load->view('testing3', $data);
$this->load->helper('html');
$this->load->helper('url');
$this->load->helper('form');
}
function updateid()
{
$data=array(
'id'=>$this->input->post('id'),
'name'=>$this->input->post('name'),
'name'=>$this->input->post('name')
);
$this->testing3_model->updateid($id, $data);
$this->showid();
redirect('testing');
}
public function view($page = 'testing3') //about us page folder name
{
if ( ! file_exists('application/views/testing3/'.$page.'.php')) //link
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = 'Testing3';
//$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->helper('html');
$this->load->helper('url');
$this->load->helper('form');
$this->load->view('templates/header', $data);
$this->load->view('testing3/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
//models/testing3_model.php
<?php
class Testing3_model extends CI_Model{
public function __construct()
{
$this->load->database(); // to authorize connection to the database
}
//fetch record
public function getsitemodel()
{
$data = $this->db->get('testing'); //pass data to query
return $data->result();
}
//fetch selected record
public function showid($data)
{
$this->db->select('*');
$this->db->from('testing');
$this->db->where('id', $data);
$data = $this->db->get();
return $data->result;
}
//update record
public function updateid($id, $data)
{
$this->db->where('id', $id);
$this->db->update('testing', $data);
}
}
?>

Change this:
function updateid()
{
$data=array(
'id'=>$this->input->post('id'),
'name'=>$this->input->post('name'),
'name'=>$this->input->post('name')
);
$this->testing3_model->updateid($id, $data);
$this->showid();
redirect('testing');
}
to this:
function updateid()
{
$data=array(
'id'=>$this->input->post('id'),
'name'=>$this->input->post('name'),
'name'=>$this->input->post('name')
);
$this->db->where('id', $this->input->post('id'));
$this->db->update('yourtablename', $data);
redirect('testing');
}

Related

codeigniter: Update table from another table with session

I'm having some challenge updating table a form joined with session userdata from another table. Each time I run, it displays undefined function. Please let me know where I'm getting it wrong so could fix it.Please find time to review it
I'm a newbie.
Here's the controller
public function index()
{
if ( $this->session->userdata('logged_in') )
{
$id = $this->session->userdata('id');
$this->load->model('Userinfo_model');
$data['result'] = $this->Userinfo_model->get_users($id);
$this->load->view("usermain_info", $data);
}
}
public function update_user (){
$data = $this->input->post('userId');
$id = array(
'balance' => $this->input->post('balance'),
'id' => $this->input->post('id')
);
$this->Userinfo_model->update_user($id, $data);
$this->index();
}
Model
function get_users($userid)
{
if (isset($this->session->userdata['logged_in'])) {
$userid = ($this->session->userdata['logged_in']['id']);
} else {
header("location: nwpgroup/nwp2/index.php");
}
/* all the queries relating to the data we want to retrieve will go in here. */
$query = $this->db->query("SELECT * FROM walletbalance WHERE userId='$userid'");
return $query;
}
function update_user($id,$data){
if (isset($this->session->userdata['logged_in'])) {
$data = ($this->session->userdata['logged_in']['id']);
} else {
header("location: nwpgroup/nwp2/index.php");
}
$this->db->where('userId', $data);
$this->db->update('walletbalance', $id);
}
View
<form method="post" action="<?php echo base_url() . "index.php/userinfo/update_user"?>">
<?php if($result->num_rows() == 0){
echo 'No user found';
}
else {
foreach ( $result->result_array() as $new_user ){ ?>
<h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" /> </h4><br />
<h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text"/> </h4><br/>
<h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden"/> </h4>
<input type="submit" id="submit" name="dsubmit" value="Update">
<?php }
}
?>
Error message
Message: Undefined property: Userinfo::$Userinfo_model
and
Message: Call to a member function update_user() on null
Thanks, I'm grateful
change your view as follows :
<form method="post" action="<?php echo base_url() . "index.php/userinfo/update_user"?>">
<?php if($result->num_rows() == 0){
echo 'No user found';
}
else {
foreach ( $result->result_array() as $new_user ){ ?>
<h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" name="balance" /> </h4><br />
<h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text" name="id" /> </h4><br/>
<h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden" name="userId"/> </h4>
<input type="submit" id="submit" name="dsubmit" value="Update">
<?php }
}
?>
</form>
form will send data to server only if the element has a name
and you cannot submit a form multiple times. The above code will create update button for each row. So if you want to update all the records in a single updation, use update_batch() in codeigniter. and change view as follows :
foreach ( $result->result_array() as $new_user ){ ?>
<h4>Your name:<input value=" <?php echo $new_user['balance'] ?>" type="text" name="balance" /> </h4><br />
<h4>Your name:<input value=" <?php echo $new_user['id'] ?>" type="text" name="id" /> </h4><br/>
<h4>Your name: <input value="<?php echo $new_user['userId'] ?>" type="hidden" name="userId"/> </h4>
<?php } ?>
<input type="submit" id="submit" name="dsubmit" value="Update">
for reference : https://www.codeigniter.com/userguide3/database/query_builder.html#updating-data
In your update_user function you didn't include the usermain_info model. That is why the error is coming.
Please create constructor function and include the model in it. I have updated your code. Try with this.
<?php
function __construct() {
parent::__construct();
$this->load->library('session');
$this->load->model('Userinfo_model');
}
public function index(){
if ( $this->session->userdata('logged_in') ){
$id = $this->session->userdata('id');
$data['result'] = $this->Userinfo_model->get_users($id);
$this->load->view("usermain_info", $data);
}
}
public function update_user (){
$data = $this->input->post('userId');
$id = array(
'balance' => $this->input->post('balance'),
'id' => $this->input->post('id')
);
$this->Userinfo_model->update_user($id, $data);
$this->index();
}
?>

Codeigniter filter by category id for product table

I have this code which pulls out all the values from a table in my database and it works fine however, I don't want all the the products showing. I want to filter by category_id. The name of the table is called products. Here is the view file that shows all the products:
<?php foreach($products as $product) : ?>
<div class="col-md-4 game">
<div class="game-price"><?php echo $product->price; ?></div>
<a href="<?php echo base_url(); ?>products/details/<?php echo $product->id; ?>">
<img src="<?php echo base_url(); ?>assets/images/products/<?php echo $product->image; ?>" />
</a>
<div class="game-title">
<?php echo $product->title; ?>
</div>
<div class="game-add">
<form method="post" action="<?php echo base_url(); ?>cart/add">
QTY: <input class="qty" type="text" name="qty" value="1" /><br>
<input type="hidden" name="item_number" value="<?php echo $product->id; ?>" />
<input type="hidden" name="price" value="<?php echo $product->price; ?>" />
<input type="hidden" name="title" value="<?php echo $product->title; ?>" />
<button class="btn btn-primary" type="submit">Add To Cart</button>
</form>
</div>
</div>
<?php endforeach; ?>
And here is a screenshot of my table:
Database Screenshot
How can I edit the code above so that it displays results for a specific category?
Here is my controller:
<?php
class Products extends CI_Controller{
public function index(){
//Get All Products
$data['products'] = $this->Product_model->get_products();
//Load View
$data['main_content'] = 'products';
$this->load->view('layouts/main', $data);
}
public function details($id){
//Get Product Details
$data['product'] = $this->Product_model->get_product_details($id);
//Load View
$data['main_content'] = 'details';
$this->load->view('layouts/main', $data);
}
public function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->model('Search_model');
}
public function execute_search()
{
// Retrieve the posted search term.
$search_term = $this->input->post('search');
// Use a model to retrieve the results.
$data['results'] = $this->Search_model->get_results($search_term);
// Pass the results to the view.
$this->load->view('products',$data);
}
}
Here is my model:
<?php
class Product_model extends CI_Model{
/*
* Get All Products
*/
public function get_products(){
$this->db->select('*');
$this->db->from('products');
$query = $this->db->get();
return $query->result();
}
/*
* Get Single Product
*/
public function get_product_details($id){
$this->db->select('*');
$this->db->from('products');
$this->db->where('id', $id);
$query = $this->db->get();
return $query->row();
}
/*
* Get Categories
*/
public function get_categories(){
$this->db->select('*');
$this->db->from('categories');
$query = $this->db->get();
return $query->result();
}
/*
* Get Most Popular Products
*/
public function get_popular(){
$this->db->select('P.*, COUNT(O.product_id) as total');
$this->db->from('orders AS O');
$this->db->join('products AS P', 'O.product_id = P.id', 'INNER');
$this->db->group_by('O.product_id');
$this->db->order_by('total', 'desc');
$query = $this->db->get();
return $query->result();
}
/*
* Add Order To Database
*/
public function add_order($order_data){
$insert = $this->db->insert('orders', $order_data);
return $insert;
}
}
This is the part that actually gets the products in my model and the part I think I have to edit:
public function get_products(){
$this->db->select('*');
$this->db->from('products');
$query = $this->db->get();
return $query->result();
Pass one optional parameter to
public function index($category_id = false){
then same parameter pass to model's method
$data['products'] = $this->Product_model->get_products($category_id);
In model, you would make an check if id is valid value (i.e. positive int and then query DB with that clause, otherwise query them all)
public function get_products($category_id){
$this->db->select('*');
$this->db->from('products');
if ((int)$category_id > 0) {
$this->db->where('category_id', $category_id);
}
$query = $this->db->get();
return $query->result();
}
Something like this, but you should make some check for case when integer is passed but that cat id doesn't exist.

Cannot update product table getting You must use the "set" method to update an entry

im still learning codeigniter and have come up with a problem. That is
You must use the "set" method to update an entry.
I am trying to update a table : product which contains product_name, product_desc etc columns below are my controller ,view and model .. i cannot figure out what am i doing wrong .. I always checked out print_r($array); it gives me an empty array ... although i do get product details displayed in the input fields on the link : ci/index.php/admin/product/1 ..
posting half code for view as the view code is long ..
Controller :
//**** For editing product ****//
public function edit_products(){
$data = array ('product_name'=>$this->input->post('product_name'),
'product_desc'=>$this->input->post('product_desc'),
'product_stock'=>$this->input->post('product_stock'),
'product_sku'=>$this->input->post('product_sku'),
'inventory_date'=>$this->input->post('inventory_date'),
'product_price'=>$this->input->post('product_price'),
'featured_product'=>$this->input->post('featured_product'),
'best_seller'=>$this->input->post('best_seller'),
'brand_id'=>$this->input->post('brand_id'),
);
$upd_pr = $this->mainmodel->update_product($_POST);
if($upd_pr){
$this->session->set_flashdata('messages', 'Updated Sucessfully');
redirect('admin/editing_products');
}else{
$this->session->set_flashdata('error', 'Updation Failed');
redirect('admin/editing_products');
}
}
public function editing_products(){
$data['product']=$this->mainmodel->set_pro2();
//$data['images'] = $this->products->images($id);
$data['content']='admin/edit_products';
$this->load->view('admin/main',$data);
}
Model :
/*for update Product*/
public function update_product($array){
extract($array);
//print_r($array);
//die;
$this->db->update('product',array('product_name'=>$product_name,'product_desc'=>$product_desc,'inventory_date'=>$inventory_date,'product_price'=>$product_price,'featured_product'=>$featured_product,'best_seller'=>$best_seller,'brand_id'=>$brand_id));
$this->db->update('product');
return ;
}
function set_pro2(){
$q = $this->db->query('SELECT * FROM product,product_image,category_product WHERE product.product_id = category_product.product_id AND product.product_id = 1 LIMIT 1');
if($q->num_rows() > 0){
foreach($q->result() as $row){
$data[] = $row;
}
return $data;
}
}
View :
<form class="form-horizontal form-row-seperated" enctype="multipart/form-data" action="<?php echo site_url('admin/edit_products') ?>" method="post">
<div class="form-group">
<?php foreach($product as $pro){ ?>
<label class="col-md-2 control-label">Name:<span class="required">
* </span>
</label>
<div class="col-md-10">
<input type="text" class="form-control" name="product_name" placeholder="" value="<?php echo $pro->product_name; ?>">
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Description: <span class="required">
* </span>
</label>
<div class="col-md-10">
<input type="text" class="form-control" name="product_desc" value="<?php echo $pro->product_desc; ?>">
</div>
</div>
In some cases codeigniter updates / inserts require this->db->set if in array. I also would not recommend placing two db->update to the same table as you have added on model.
Active record guide http://www.codeigniter.com/user_guide/database/active_record.html
Update
// $brand_id = 0, $data are from the edit function on controller
public function name($brand_id = 0, $data) {
$product_name = $this-input->post('product_name');
$product_desc = $this-input->post('product_desc');
$inventory_date = $this-input->post('inventory_date');
$product_price = $this-input->post('product_price');
$featured_product = $this-input->post('featured_product');
$best_seller = $this-input->post('best_seller');
$data = array(
'product_name'=> $product_name,
'product_desc'=> $product_desc,
'inventory_date'=>$inventory_date,
'product_price'=>$product_price,
'featured_product'=> $featured_product,
'best_seller'=> $best_seller,
'brand_id'=> $this->uri->segment(what_ever)
);
$this->db->set($data);
$this->db->update('product');
}
Or insert
$data = array(
'product_name'=> $product_name,
'product_desc'=> $product_desc,
'inventory_date'=>$inventory_date,
'product_price'=>$product_price,
'featured_product'=> $featured_product,
'best_seller'=> $best_seller,
'brand_id'=>$brand_id
);
$this->db->set($data);
$this->db->insert_id();
$this->db->insert('product');
Post Update
Set routes $route['function/edit/(:any)'] = 'folder/products_list/edit/$1';
If need to update a product you need to set a function called edit on the
Model Get
public function model_get_products() {
return $this->db->get($this->db->dbprefix . 'products')->result_array();
}
Controller for uri segment help http://www.codeigniter.com/user_guide/libraries/uri.html
public function edit() {
Form validation
$this->load->library('form_validation');
$this->form_validation->set_rules('what ever');
if ($this->form->validation->run() == TRUE) {
$this->load->model('folder/model_update_product');
//Example uri segment admin/product_list/edit/id_number $this->uri->segment('4')
$this->model_update_product->name($this->uri->segment('what_ever_is'), $this->input->post())
redirect('function/product_list');
} else {
// Load Your Form View i.e. $this->get_form();
}
}
public function index() {
$this->load->model('folder/model_get_products');
$results = $this->model_get_products->get();
$data['products'] = array();
//Example uri segment admin/product_list/edit/id_number $this->uri->segment('4')
// Add What Else You Need to here and then you can add it to view
foreach($results as $result) {
$data['products'][] = array(
'brand_id' => $result[brand_id],
'edit' => site_url('controller_name/edit'). '/' .$result[brand_id] // site_url must match what you set in routes for edit.
);
}
return $this->load->view('folder/products_list', $data);
}
public function get_form() {
//Example uri segment admin/product_list/edit/id_number $this->uri->segment('4')
// Make sure you set another model function in your get model where can get by id.
$product_info = $this->get_products_by_id->get($this->uri->segment('4'));
$product_name = $this->input->post('product_name');
if (isset($product_name)) {
$data['product_name'] = $product_name; // for name="" in input
} elseif (!empty($product_info)) {
$data['product_name'] = $product_info['product_name']; // for value="" in input
} else {
$data['product_name'] = '';
}
$this->load->view('folder/product_form', $data);
product form contents
}
view product list
<form>
<table>
<thead>
</thead>
<tbody>
<?php if ($products) { ?>
<?php foreach ($products as $product) { ?>
<tr>
<td><?php echo $product['brand_id'];?></td>
<td> Edit Product</td>
</tr>
<?php } ?>
<?php } ?>
</tbody>
</table>
</form>

Retrieving data from database to dropdownlist using codeigniter

I can already data into the database, but I cannot retrieve it from the database into the dropdownlist. Can anyone help me out?
My view:
<body>
<div id="container">
<?php echo form_open('form/myform'); ?>
<select class="form-control" >
<?php
foreach($groups as $row)
{
echo '<option value="'.$row->school.'">'.$row->school.'</option>';
}
?>
</select>
<div id="addother">
<?php echo form_input(array('id'=>'addother_input', 'name'=>'school', 'placeholder'=>'Enter name of school...')); ?>
<input type="submit" id="add" name="submit" value="+" />
</div>
<?php echo form_close(); ?>
</div>
My Controller:
function myform(){
$data['title'] = "myform";
$this->load->library('form_validation');
$this->load->model('school_model');
$sdata['school'] = $this->input->post('school');
$this->school_model->addItem($sdata);
$this->load->view('myform');
}
function drop() {
$this->load->model('school_model');
$data['groups'] = $this->school_model->getAll();
$this->load->view('myform', $data);
}
My Model:
function getAll() {
$query = $this->db->query('SELECT school FROM tblschool');
return $query->result();
}
function addItem($sdata){
return $this->db->insert('tblschool', $sdata);
}
I'm really confused on how to retrieve data from the database into the dropdownlist. I would really appreciate your help.
Are you accessing form/drop to generate the view? If you want to access this via form/myform you will have to change myform to include the call to $this->school_model->getAll();
public function myform()
{
$data['title'] = "myform";
$this->load->library('form_validation');
$this->load->model('school_model');
if($this->input->post()){
$sdata['school'] = $this->input->post('school');
$this->school_model->addItem($sdata);
}
$data['groups'] = $this->school_model->getAll();
$this->load->view('myform', $data);
}

Cannot retrieve data from database to drop down list

I am new to codeigniter and I'm having a problem in retrieving data from database to drop down list. Can anyone help me with this?
My View:
<?php echo form_open('form/myform'); ?>
<select id="addother" >
<option value="none" selected="selected"> ------Select School------ </option>
<?php foreach($groups as $row) {
echo '<option value="'.$row->id.'">'.$row->name.'</option>';
} ?>
</select>
<div id="addother">
<?php echo form_input(array('id'=>'addother_input', 'name'=>'school', 'placeholder'=>'Enter name of school...')); ?>
<input type="submit" id="add" name="submit" value="+" />
</div>
<?php echo form_close(); ?>
My Controller:
function myform(){
$data['title'] = "myform";
$this->load->library('form_validation');
$this->load->model('school_model');
if($this->input->post()){
$sdata['school'] = $this->input->post('school');
$this->school_model->addItem($sdata);
}
$data['groups'] = $this->school_model->getAll();
$this->load->view('myform', $data);
}
My Model:
function getAll() {
$query = $this->db->get('tblschool');
return $query->result();
}
function addItem($sdata){
return $this->db->insert('tblschool', $sdata);
}
I can't retrieve data from database into the drop down list. I would really appreciate your help. Thanks!
i think that you could be persist the select value when the form is submitted in the "myForm" method. So, in the same method, retrieve the new group values. Try this:
function myform(){
$data['title'] = "myform";
$this->load->library('form_validation');
$this->load->model('school_model');
if ($this->form_validation->run() == TRUE){
$sdata['school'] = $this->input->post('school');
$this->school_model->addItem($sdata);
}
$data['groups'] = $this->school_model->getAll();
$this->load->view('myform', $data);
}

Categories