0% found this document useful (0 votes)
17 views

Assignment

The document defines a Products model class that interacts with a database table to retrieve product data. It also defines a Products controller class that loads the model and views. The controller contains index and view methods - index displays all products, view displays a single product. Corresponding view files display products and a single product page.

Uploaded by

Christine Bacud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Assignment

The document defines a Products model class that interacts with a database table to retrieve product data. It also defines a Products controller class that loads the model and views. The controller contains index and view methods - index displays all products, view displays a single product. Corresponding view files display products and a single product page.

Uploaded by

Christine Bacud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Products_model

<?php
class Products_model extends CI_Model{
public function __construct(){
parent::__construct();
$this->load->database();
}

public function get_products($id=null){


if ($id===null){
$rs=$this->db->get('tblproducts');
return $rs->result_array();
}else{
$rs=$this->db->get_where('tblproducts', array('product_id' => $id));
return $rs->row_array();
}
}
}
?>

Products(Controller)

<?php
class Products extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('Products_model');
}

public function index(){


$data["title"] = 'Product Listings';
$data['tblproducts'] = $this->Products_model->get_products();

$this->load->view('pages/products', $data);
}

public function view($id = NULL){


$data['product']= $this->Products_model->get_products($id);

if (empty($data['product'])){
show_404();
}

$data['title'] = $data['product']['name'];
$this->load->view('pages/product', $data);
}
}
?>

products(View)

<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $title; ?></h1>
<table>
<thead>
<tr>
<th>Product_ID</th>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach($tblproducts as $product): ?>
<tr>
<td><?php echo $product["product_id"]; ?></td>
<td><?php echo $product["name"]; ?></td>
<td><?php echo $product["description"]; ?></td>
<td><?php echo $product["price"]; ?></td>
<td><a href="<?php echo base_url('products/view/' .
$product['product_id']); ?>">View</a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</body>
</html>

product(View)

<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $title; ?></h1>
<strong>ID</strong>: <?php echo $product['product_id']; ?> <br />
<strong>Name</strong>: <?php echo $product['name']; ?> <br />
<strong>Description</strong>: <?php echo $product['description']; ?> <br />
<strong>Price</strong>: <?php echo $product['price']; ?> <br />
<td><a href="<?php echo base_url('products/'); ?>">Back</a></td>

</body>
</html>

You might also like