controller
<?php
class delete_ctrl extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('delete_model');
}
// Function to Fetch selected record from database.
function show_product_id() {
$id = $this->uri->segment(3);
$data['products'] = $this->delete_model->show_products();
$data['single_product'] = $this->delete_model->show_product_id($id);
$this->load->view('deletepro_view', $data);
}
// Function to Delete selected record from database.
function delete_product_id() {
$id = $this->uri->segment(3);
$this->delete_model->delete_product_id($id);
$this->show_product_id();
}
}
model
<?php
class delete_model extends CI_Model{
// Function to select all from table name students.
function show_products(){
$query = $this->db->get('product');
$query_result = $query->result();
return $query_result;
}
// Function to select particular record from table name students.
function show_product_id($data){
$this->db->select('*');
$this->db->from('product');
$this->db->where('id', $data);
$query = $this->db->get();
$result = $query->result();
return $result;
}
// Function to Delete selected record from table name students.
function delete_product_id($id){
$this->db->where('id', $id);
$this->db->delete('product');
}
}
?>
view
<?php
?>
<!DOCTYPE html>
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Marcellus' rel='stylesheet' type='text/css'> <!--google fonts-->
<link href="<?php echo base_url()?>./css/delete.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="container">
<div id="wrapper">
<h1>Delete Product From Database</h1>
<div id="menu">
<p>Click the product to be deleted</p>
<!--====== Displaying Fetched Names from Database in Links ========-->
<ol>
<?php
foreach ($products as $product): ?>
<li><a href="<?php echo base_url() . "index.php/delete_controller/show_product_id/" . $product->id; ?>"><?php echo $product->name; ?></a>
</li><?php endforeach; ?>
</ol>
</div>
<div id="detail">
<!--====== Displaying Fetched Details from Database ========-->
<?php foreach ($single_product as $product): ?>
<!--====== Delete Button ========-->
<a href="<?php echo base_url() . "index.php/delete_contrller/delete_product_id/" . $product->id; ?>">
<button>Delete</button></a>
<?php endforeach; ?>
</div>
</div>
</div>
</body>
</html>
I am having this error
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: products
Filename: views/deletepro_view.php
Line Number: 19
Backtrace:
File: C:\wamp\www\BakeBookProject\application\views\deletepro_view.php
Line: 19
Function: _error_handler
File: C:\wamp\www\BakeBookProject\application\controllers\Welcome.php Line: 23
Function: view
File: C:\wamp\www\BakeBookProject\index.php
Line: 315
Function: require_once