error in updating records using codeigniter - php

i'm trying to update my records which are stored in the database, but unfortunately i'm getting errors like (given below) this please can any one help me in this it would be help full for me
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$id
Filename: views/display_about_us1.php
Line Number: 44
Backtrace:
File: C:\xampp\htdocs\CodeIgniter_try\application\views\display_about_us1.php
Line: 44
Function: _error_handler
File: C:\xampp\htdocs\CodeIgniter_try\application\controllers\Home.php
Line: 107
Function: view
File: C:\xampp\htdocs\CodeIgniter_try\index.php
Line: 315
Function: require_once
Controller
public function updatedata()
{
$id=$this->input->get('id');
$result['data']=$this->Contact_model->displayrecordsById($id);
$this->load->view('update_about_us1',$result);
if($this->input->post('update'))
{
// Basic validation
$this->form_validation->set_rules('first_content', 'First content', 'required');
$this->form_validation->set_rules('second_content', 'Second content', 'required');
$this->form_validation->set_rules('third_content', 'Third content', 'required');
$this->form_validation->set_rules('fourth_content', 'Fourth content', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('update_about_us1');
}
else
{
$id = $this->input->post('id');
$data = array();
$data['first_content']=$this->input->post('first_content');
$data['second_content']=$this->input->post('second_content');
$data['third_content']=$this->input->post('third_content');
$data['fourth_content']=$this->input->post('fourth_content');
$this->Contact_model->updaterecords($id, $data);
redirect("Home/About_us_display");
}
}
}
Model
function displayrecordsById($id)
{
$query=$this->db->query("select * from about_us1 where id='".$id."'");
return $query->result();
}
function updaterecords($id,$data)
{
$this->db->where('id', $id);
$this->db->update('about_us1', $data);
}
View
<!DOCTYPE html>
<html>
<head>
<title>About_us1 page</title>
</head>
<body>
<?php
$i=1;
foreach($data as $row)
{
?>
<form action="Home/updatedata" method="get">
<table width="600" border="1" cellspacing="5" cellpadding="5">
<tr>
<td width="230">First content </td>
<td width="329"><textarea rows="4" cols="50" name="first_content" value="<?php echo $row->first_content; ?>"/></textarea></td>
<span><?php echo form_error("first_content");?></span>
</tr>
<tr>
<td>Second content </td>
<td><textarea rows="4" cols="50" name="second_content" value="<?php echo $row->second_content; ?>"/></textarea></td>
<span><?php echo form_error("second_content");?></span>
</tr>
<tr>
<td>Third content </td>
<td><textarea rows="4" cols="50" name="third_content" value="<?php echo $row->third_content; ?>"/></textarea></td>
<span><?php echo form_error("third_content");?></span>
</tr>
<tr>
<td>Fourth content </td>
<td><textarea rows="4" cols="50" name="fourth_content" value="<?php echo $row->fourth_content; ?>"/></textarea></td>
<span><?php echo form_error("fourth_content");?></span>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="update" value="Update Records"/></td>
</tr>
</table>
</form>
<?php } ?>
</body>
</html>
// display record view page
<!DOCTYPE html>
<html lang="en">
<head>
<title>display</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<br><br>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading text-center">Contact Us Details</div>
</div>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>First Content</th>
<th>Second Content</th>
<th>Third Content</th>
<th>Fourth Content</th>
<th>Update Content</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$i=1;
foreach($data as $row)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$row->first_content."</td>";
echo "<td>".$row->second_content."</td>";
echo "<td>".$row->third_content."</td>";
echo "<td>".$row->fourth_content."</td>";
echo "<td><a href='updatedata?id=".$row->id."'>Update</a></td>";
echo "</tr>";
$i++;
}
?>
</tr>
</tbody>
</table>
</div><br><br>
<button type="button" class="btn btn-success">Back</button>
</div>
</body>
</html>

Hope this will help you :
Arguments mismatch with updaterecords method in controller with model
Should be like this :
Call updaterecords method in your controller like this :
$this->Contact_model->updaterecords($id, $data);
Your model method should be like this :
function updaterecords($id,$data,)
{
$this->db->where('id', $id);
$this->db->update('about_us1', $data);
}
UPDATE :
your controller code should be like this :
if($this->input->post('update'))
{
$data = array();
$id = $this->input->post('id');
$data['first_content']=$this->input->post('first_content');
$data['second_content']=$this->input->post('second_content');
$data['third_content']=$this->input->post('third_content');
$data['fourth_content']=$this->input->post('fourth_content');
$this->Contact_model->updaterecords($id, $data);
redirect("Home/About_us_display");
}

Related

how to get post data in ci

i want to print post data in my login function with $this->input->post['username'];
I have a home controller given below :
<?php
class Home extends CI_Controller{
public function index(){
$this->load->view("home/home_view");
}
public function Login(){
echo $this->input->post['username'];
}
}
?>
but its showing me null result but if i write print_r($_POST) instead of $this->input->post['username'] its showing me all data
this is my home view code
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>assets/css/bootstrap.css">
</head>
<body>
<div class="col-lg-2"></div>
<div class="col-lg-8" style="min-height: 500px;box-shadow: 5px 5px 15px #000; margin-top: 50px;">
<form method="post" action="<?php echo base_url(); ?>index.php/Home/Login">
<table class="table table-stripped">
<tr>
<th colspan="2">Login form</th>
</tr>
<tr>
<td>Username</td>
<td><input type="text" class="form-control" name="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" class="form-control"></td>
</tr>
<tr>
<th colspan="2"><input type="submit" value="Submit" class="btn btn-primary"></th>
</tr>
</table>
</form>
</div>
<div class="col-lg-2"></div>
</body>
</html>
i had auto loaded form helper and url helper
Hope this will help you :
Use $this->input->post('username'); instead of $this->input->post['username'] :
Your Login function should be like this :
public function Login()
{
/* to print all field name do like this*/
print_r($this->input->post());
/* to print a particular field name do like this*/
echo $this->input->post('username');
echo $this->input->post('password');
}
Use site_url or base_url in form like this ( good practice) :
<form method="post" action="<?php echo site_url('Home/Login'); ?>">
........
</form>
for more : https://www.codeigniter.com/user_guide/libraries/input.html
Convert your HTML from into CI form helper way although i have updated your view file for your reference see form helper [https://www.codeigniter.com/userguide3/helpers/form_helper.html][1]
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>assets/css/bootstrap.css">
</head>
<body>
<div class="col-lg-2"></div>
<div class="col-lg-8" style="min-height: 500px;box-shadow: 5px 5px 15px #000; margin-top: 50px;">
<?php
echo form_open('home/login');
?>
<table class="table table-stripped">
<tr>
<th colspan="2">Login form</th>
</tr>
<tr>
<td>Username</td>
<td>
<?php
$data = array(
'name' => 'username',
'id' => 'username'
);
echo form_input($data);
?>
<input type="text" class="form-control" name="username">
</td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" class="form-control">
<?php
$pwd = array(
'name' => 'password',
'id' => 'password'
);
echo form_password($pwd);
?>
</td>
</tr>
<tr>
<?php echo form_submit('submit', 'Submit'); ?>
</th>
</tr>
</table>
<?php echo form_close();?>
</div>
<div class="col-lg-2"></div>
</body>
</html>

Can't seem to add simple user input to my DB via a form

This is very quick.
I have a form that when submitted should add user input into my DB. I have had a similar form working well but for some reason this one is not working.
Please excuse my sloppy code but here is the whole page. For some reason 'observation' will not take the user input and display it on the next page (showOne.php). The next page simply shows a blank space for observation and I checked the DB and there was nothing in the observation field of course.
Is it something simple that I can't see? I've been at this for days since it is identical to another form I have that works well on another page.
Thank you for any insight.
<?php
//including the database connection file
include("config.php");
$activePage = "start.php";
if(isset($_POST['update'])) {
$id = $_POST['id'];
$observation = $_POST['observation'];
$result = mysqli_query($mysqli, "UPDATE tasks SET observation='$observation' WHERE id=$id");
header("Location: showOne.php?id=$id");
}
//getting id from url
$id = $_GET['id'];
//selecting data associated with this particular id
$result2 = mysqli_query($mysqli, "SELECT * FROM users WHERE id=$id ORDER BY id ASC");
$result = mysqli_query($mysqli, "SELECT * FROM tasks ORDER BY taskNumber ASC");
include_once "nav.php";
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pathfinder - A User Testing Dashboard</title>
<meta name="description" content="Pathfinder - A User Testing Dashboard">
<meta name="author" content="James Stables - Thurs April 13th 2017">
<link rel="stylesheet" href="core.css">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,600,700" rel="stylesheet">
<!--[if lt IE 9]>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.js"> </script>
<![endif]-->
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
</head>
<body>
<header>
<h1 class="logo"><img src="images/logo.png">Pathfinder</h1>
<h4>User Testing Dashboard</h4>
</header>
<section class="nonav">
<div class="showAllUsers">
<table border='0'>
<tr>
<?php while($res = mysqli_fetch_array($result2))
{ ?>
<td class="widthHundred no-border left"><h2><?php echo $res['nameFirst']; ?></h2></td>
<?php } ?>
</tr>
</table>
<table border='0'>
<thead>
<tr>
<th>Image</th>
<th>Category</th>
<th>Task Number</th>
<th>Task Name</th>
<th>Observation</th>
<td> </td>
<td> </td>
<td> </td>
</tr>
</thead>
<tbody>
<form name="start" method="post" action="start.php" enctype="multipart/form-data">
<tr>
<td><a href="<?php echo $res['$image'];?>" data-lightbox="example-1" data-title="Task Number <?php echo $res['taskNumber'];?>">
<img id="imageResize" src="<?php echo $res['image'];?>" /></a></td>
<td><?php echo $res['category'];?></td>
<td><?php echo $res['taskNumber'];?></td>
<td><?php echo $res['taskName'];?></td>
<td><input type="text" name="observation" value="<?php echo $res['observation'];?>"></td>
<td> </td>
<td> </td>
<td> </td>
<?php } ?>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td><input type="hidden" name="id" value="<?php echo $_GET['id'];?>"><input type="submit" class="buttonUpdate right" name="update" value="Update"></td>
<td><a class="buttonCancel right" href="tasks.php" onClick="return confirm('Cancel and go back?')">Cancel</a></td>
</tr>
</form>
</tbody>
</table>
</div>
</section>
<script src="js/lightbox-plus-jquery.min.js"></script>
<script>
lightbox.option({
'resizeDuration': 200,
'showImageNumberLabel': false,
'fitImagesInViewport': true
})
</script>
</body>
</html>

Ping selected IPs from checkbox on PHP-MySQL query

I have an idea and wonder if it can be or not on PHP since I'm new to PHP. I need to ping selected computer name or IP address and give me the status on ping status box as online or offline.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>Books</title>
</head>
<body>
<form action="Untitled-1.php" method="post">
<input type="text" name="search"/>
<input type="submit" value="search"/>
<input type="button"value="ping"/>
</form>
</form>
<br/>
<table border="1">
<thead>
<tr>
<th>username</th>
<th>desktop</th>
<th>ip address</th>
<th>select pc's</th>
<th>ping status</th>
</tr>
</thead>
<?php
include("db.php");
$word = isset ($_POST['search']) ? $_POST['search'] : "";
$result=mysql_query("SELECT * FROM pc WHERE desktop like '%$word%'");
while($test = mysql_fetch_array($result)) {
$id = $test['user_id'];
?>
<tr align='center'>
<td><font color='black'><?php echo $test['username'] ?></font></td>
<td><font color='black'><?php echo $test['desktop'] ?> </font></td>
<td><font color='black'><?php echo $test['ip_address'] ?></font></td>
<td><input name="selector[]" type="checkbox" value="<?php echo $id; ?>"></td>
<td><font color='black'><?php echo $test['ping_status'] ?></font></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
please help
Pass ip in this function it give status of host.
<?php
function ping_host($host){
exec("ping -c 4 " . $host, $output, $result);
return $result==0?"online":"offline";
}
echo ping("www.google.com"); // function call
?>
Note:Here 4 is number of ping you want you can change according to requirement in linux system if you not set this it will ping forever.
thanks alot Sunil for try to help me , i found the selution and it's work fine with me now
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<?php
require("db.php");
?>
<body>
<form method="post">
<table cellpadding="0" cellspacing="0" border="1" class="table table-striped table-bordered" id="example">
<div class="alert alert-info">
<strong><i class="icon-user icon-large"></i></strong>
</div>
<thead>
<tr>
<th>username</th>
<th>desktop</th>
<th>ip address</th>
<th>select</th>
<th>status</th>
</tr>
</thead>
<tbody>
<?php
$query=mysql_query("select * from pc")or die(mysql_error());
while($row=mysql_fetch_array($query)){
$id=$row['ip_address'];
?>
<tr>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['desktop'] ?></td>
<td><?php echo $row['ip_address'] ?></td>
<td>
<input name="selector[]" type="checkbox" value="<?php echo $id; ?>"> </td>
<td><?php echo $row['ping_status'] ?></td>
</tr>
<?php }
if (isset($_POST['submit'])){
if(!empty($_POST['selector'])){
foreach($_POST['selector'] as $id){
if (!$socket = #fsockopen($id, 80, $errno, $errstr, 30))
{ $status= "offline";
echo $status; }
else
{ $status= "online";
echo $status;
fclose($socket); }
$sql="UPDATE pc SET ping_status='$status' WHERE ip_address='$id'";
$query=mysql_query($sql);
header("Location:test.php");
}
}
}
?>
</tbody>
</table>
<button class="btn btn-success" name="submit" type="submit">
ping
</button>
</form>
</body>
</html>
please find the last solution ,
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<?php
require("db.php");
?>
<body>
<form method="post">
<table cellpadding="0" cellspacing="0" border="1" class="table table-striped table-bordered" id="example">
<div class="alert alert-info">
<strong><i class="icon-user icon-large"></i></strong>
</div>
<thead>
<tr>
<th>username</th>
<th>desktop</th>
<th>ip address</th>
<th>select</th>
<th>status</th>
</tr>
</thead>
<tbody>
<?php
$query=mysql_query("select * from pc")or die(mysql_error());
while($row=mysql_fetch_array($query)){
$id=$row['ip_address'];
?>
<tr>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['desktop'] ?></td>
<td><?php echo $row['ip_address'] ?></td>
<td>
<input name="selector[]" type="checkbox" value="<?php echo $id; ?>"> </td>
<td><?php echo $row['ping_status'] ?></td>
</tr>
<?php }
$sql1="UPDATE pc
SET ping_status = NULL
WHERE ping_status is not null";
$query1=mysql_query($sql1);
function pingAddress($id) {
$pingresult = exec("ping -n 1 $id && exit", $output, $result);
//echo $result. "<br/>";
global $status;
if (($result == 0)){
if(count(preg_grep('/Destination host unreachable/i', $output)) == 0){
$status="online <br/>";
echo $status;
}else
$status="offline <br/>";
echo $status;
}
elseif ($result == 1){
$status="offline <br/>";
echo $status;
}
}
if (isset($_POST['submit'])){
if(!empty($_POST['selector'])){
foreach($_POST['selector'] as $id){
echo $id."";
pingAddress($id);
$sql="UPDATE pc SET ping_status='$status' WHERE ip_address='$id'";
$query=mysql_query($sql);
header("Location:test.php");
}
}
}
?>
</tbody>
</table>
<button class="btn btn-success" name="submit" type="submit">
ping
</button>
</form>
</body>
</html>

Errors appear in searching if a record not found using searchtext

SearchText works if record is found but errors appear when record not found.
Here are the errors. Notice: Undefined variable: sample_list in C:\xampp\htdocs\viewsample\samplelist.php on line 166
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\ viewsample\samplelist.php on line 166
If I click the search button again, errors disappear.
Is it possible to return to home page if record not found?
I searched here a possible solution to my problem but I found none.
Please help.
Thank you and more power.
Here's the code.
Index.php
//Perform Search from our database
if(isset($_POST['action_type']))
{
echo 'Welcome';
if ($_POST['action_type'] == 'search')
{
$search = mysqli_real_escape_string($link, strip_tags($_POST['searchText']));
$sql = "select samp_id, daterecv, datecoll, modcoll, aperson, estabname, estabadd, gname, bname, prodcat, dform, dstrength, from sample
where gname like '%$search%' or bname like '%$search%' or estabname like '%$search%'";
$result = mysqli_query($link, $sql);
if(!$result)
{
echo mysqli_error($link);
exit();
}
//Loop through each row on array and store the data to $sample_list[]
while($rows = mysqli_fetch_array($result))
{
$sample_list[] = array('samp_id' => $rows['samp_id'],
'daterecv' => $rows['daterecv'],
'datecoll' => $rows['datecoll'],
'modcoll' => $rows['modcoll'],
'apers' => $rows['apers'],
'estabname' => $rows['estabname'],
'estabadd' => $rows['estabadd'],
'gname' => $rows['gname'],
'bname' => $rows['bname'],
'prodcat' => $rows['prodcat'],
'dform' => $rows['dform'],
'dstrength' => $rows['dstrength']);
}
include 'samplelist.php';
exit();
}
}
samplelist.php
<?php
session_start();
$role = $_SESSION['sess_userrole'];
if(!isset($_SESSION['sess_username']) && $role!="sampler"){
header('Location: index.php?err=2');
}
?>
<?php
include_once 'index.php';
?>
<!DOCTYPE html>
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function GotoHome(){
window.location = 'index.php';
}
{
function preventBack(){window.history.forward();}
setTimeout("preventBack()", 0);
window.onunload=function(){null};
}
</script>
</head>
<body>
<div class="wrapper">
<div class="content">
<p>Logout</p>
<p><center><b><font style="arial" color="black">SAMPLE COLLECTION</font></b></center></p>
<div style="margin-bottom: 10px;">
<form method="POST" action="index.php">
<input type="text" id="searchText" name="searchText" style="width:300px"/>
<input type="hidden" name="action_type" value="search" onfocus="Clear (this);"/>
<input type="submit" value="search"/>
</form>
</div>
<div style="max-height: 740px; overflow:auto; max-width: 1600; overflow:auto;">
<table class="pbtable">
<thead>
<tr>
<th>
Date Received
</th>
<th>
Date Collected
</th>
<th style = "display:none">
Mode of Collection
</th>
<th style = "display:none">
Assigned Personnel
</th>
<th>
Establishment Name
</th>
<th style = "display:none">
Establishment Address
</th>
<th>
Generic Name
</th>
<th>
Brand Name
</th>
<th>
Product Category
</th>
<th style = "display:none">
Dosage Form
</th>
<th style = "display:none">
Dosage Strength
</th>
</thead>
<tbody>
<?php foreach($sample_list as $collection) : ?>
<tr>
<td>
<?php echo $collection["daterecv"]; ?>
</td>
<td>
<?php echo $collection["datecoll"]; ?>
</td>
<td style = "display:none">
<?php echo $collection["modcoll"]; ?>
</td>
<td style = "display:none">
<?php echo $collection["aperson"]; ?>
</td>
<td>
<?php echo $collection["estabname"]; ?>
</td>
<td style = "display:none">
<?php echo $collection["estabadd"]; ?>
</td>
<td>
<?php echo $collection["gname"]; ?>
</td>
<td>
<?php echo $collection["bname"]; ?>
</td>
<td>
<?php echo $collection["prodcat"]; ?>
</td>
<td style = "display:none">
<?php echo $collection["dform"]; ?>
</td>
<td style = "display:none">
<?php echo $collection["dstrength"]; ?>
</td>
<form method="post" action="index.php">
<input type="hidden" name="ci"
value="<?php echo $collection["samp_id"]; ?>" />
<input type="hidden" name="action" value="edit" />
<input type="submit" value="Edit" />
</form>
<tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
If no records are found in DB the $sample_list array is empty.
So samplelist.php needs this check before foreach loop,
if (empty($sample_list))
{
header('Location: http://www.homesweet.home/');
exit;
}
foreach($sample_list as $collection) :
...
endforeach;

Codeigniter : Loading data from multiple Models to a view

I have the following view to List all products, and in that you can delete or edit a particular product, onclick of delete or edit it will call the controller,
<?php
$this->load->helper('url');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>diluks eCommerce - Home</title>
<link href="<?php
echo base_url();
?>Public/scripts/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form action="<?php echo base_url();?>index.php/productlist_controller" method="post">
<div class="container">
<?php
include 'header-adminpanel.php';
?>
<div class="level3 clearfix">
<?php
include 'product-sidebar.php';
?>
<div class="body-content">
<div class="items">
<h2>Product List</h2>
<table class="CSSTable" cellspacing="0px" cellpadding="0px">
<tr>
<td>Item Code</td><td>Item Name</td><td>Item Price</td><td>Edit</td><td>Delete</td>
</tr>
<?php foreach($products as $row): ?>
<form method="POST" action="<?php echo base_url();?>index.php/productlist_controller">
<tr>
<td><?php echo $row->itemcode; ?></td><td><?php echo $row->itemname; ?></td><td>$<?php echo $row->itemprice; ?></td><td><center><button name="btn_edit" class="link-button" value="<?php echo $row->itemcode; ?>" type="submit">Edit</button></center></td><td><center><button name="btn_delete" class="link-button" value="<?php echo $row->itemcode; ?>" type="submit">Delete</button></center></td>
</tr>
</form>
<?php endforeach ?>
</table>
</div>
</div>
</div>
<div style="clear:both"></div>
<div class="level4">
<div class="footer-area">
<div class="lined-space"></div>
<div class="site-map" align="left">
<table>
<tr>
<td class="footer-text">About Us</td>
<td class="footer-text">Facebook</td>
</tr>
<tr>
<td class="footer-text">Contact Us</td>
<td class="footer-text">Twitter</td>
</tr>
<tr>
<td class="footer-text">FAQs</td>
<td class="footer-text">Terms & Conditions</td>
</tr>
<tr>
<td class="footer-text">Help</td>
</tr>
</table>
</div>
<div class="developer-info">
<a class="developers-text">Designed & Developed By Diluks Software Solutions.</a>
</div>
</div>
</div>
</div>
</form>
</body>
</html>
So the controller to the above view look like belo
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Productlist_controller extends CI_Controller {
function __construct(){
parent::__construct();
}
public function index()
{
if(isset($_POST["btn_delete"])){
$pid = $_POST["btn_delete"];
$this->load->model('product_model');
$result = $this->product_model->deleteProduct($pid);
if($result==true){
$data = array();
$this->load->model('product_model');
$data['products'] = $this->product_model->availableProductList();
$this->load->view('admin_product_list_view',$data);
}
else{
echo "Oops! Error occured..!";
}
}
else if(isset($_POST["btn_edit"])){
$pid = $_POST["btn_edit"];
$data = array();
$this->load->model('product_model');
$data['product'] = $this->product_model->readProduct($pid);
//$this->load->model('category_model');
//$data['categories'] = $this->category_model->getCategories();
$this->load->view('admin_product_edit_view', $data);
}
}
}
"admin_product_edit_view" which the user will be redirected onclick of edit for an particular item is as follows,
<?php
$this->load->helper('url');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>diluks eCommerce - Home</title>
<link href="<?php
echo base_url();
?>Public/scripts/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form enctype="multipart/form-data" action="<?php
echo base_url();
?>index.php/addproduct_controller" method="post">
<div class="container">
<?php
include 'header-adminpanel.php';
?>
<div class="level3 clearfix">
<?php
include 'product-sidebar.php';
?>
<div class="body-content">
<div class="items">
<h2>Edit Product</h2>
<?php foreach($product as $row){ ?>
<table>
<tr>
<td class="captions">Product Code</td>
<td><input name="txt_pcode" type="text" readonly="true" value="<?php echo $row->itemcode; ?>"/></td>
</tr>
<tr>
<td class="captions">Product Name</td>
<td><input name="txt_pname" type="text" size="40" value="<?php echo $row->itemname; ?>" /></td>
</tr>
<tr>
<td class="captions">Product Price</td>
<td><input name="txt_pprice" type="text" value="<?php echo $row->itemprice; ?>" /></td>
</tr>
<tr>
<td class="captions">Product Category</td>
<td><select name="txt_pcategory">
<?php
foreach($categories as $row)
{
echo '<option value="'.$row->catname.'">'.$row->catname.'</option>';
}
?>
</select></td>
</tr>
<tr>
<td class="captions">Product Description</td>
<td><textarea name="txt_pdesc" style="width:300px;height:100px;"><?php echo $row->itemdesc; ?></textarea></td>
</tr>
<tr>
<td class="captions">Product Image</td>
<td><input type="file" name="userfile" size="20" /></td>
</tr>
<tr>
<td class="captions">Product Options</td>
<td><input name="txt_poptions" size="40" type="text" /><a class="hint"> (Separate by a "," comma)</a></td>
</tr>
<tr><td><input name="btn_add" class="grey-button" type="submit" value="Update" /></td></tr>
</table>
<?php } ?>
<br />
</div>
</div>
</div>
<div style="clear:both"></div>
<div class="level4">
<div class="footer-area">
<div class="lined-space"></div>
<div class="site-map" align="left">
<table>
<tr>
<td class="footer-text">About Us</td>
<td class="footer-text">Facebook</td>
</tr>
<tr>
<td class="footer-text">Contact Us</td>
<td class="footer-text">Twitter</td>
</tr>
<tr>
<td class="footer-text">FAQs</td>
<td class="footer-text">Terms & Conditions</td>
</tr>
<tr>
<td class="footer-text">Help</td>
</tr>
</table>
</div>
<div class="developer-info">
<a class="developers-text">Designed & Developed By Diluks Software Solutions.</a>
</div>
</div>
</div>
</div>
</form>
</body>
</html>
Now the problem is in the controller i need to load up categories (which I have commented) in order to show them in the edit view, but when I un-commented it, categories are loading, but Gives an Error in Item Description textarea saying
Message: Undefined property: stdClass::$itemdesc
With the category model loading line commented, it works fine except no categories will load up in the dropdownlist,Please someone suggest me a way to get rid of this.
Please use this in your view
if(isset($row->itemdesc)) echo $row->itemdesc;
I think this will solve your problem
I saw that you got an answer but i have a few suggestions for your code:
1. in your controller you load the model 3 times: you should load it once; you could do something like this:
if(!empty($_POST)){
$this->load->model('product_model');
if(isset($_POST["btn_delete"])){
//some code
}elseif(isset($_POST["btn_edit"])){
//some other code
}
}
2. in your views you load a helper: this should be loaded into the controller, right after the first verification from point 1.
3. in views you should check your variable for content, especially the ones that you are going to use in a loop, like $products

Categories