Create a new file in a folder -> Controller
1 |
/application/controller/shop.php |
Write Code like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Shop extends CI_Controller { function index() { $this->load->model('products_mod'); $data['products'] = $this->products_mod->get_all(); //echo "<pre>"; //print_r($data['products']); $this->load->view('products_view', $data); } function add() { $this->load->model('products_mod'); $product = $this->products_mod->get($this->input->post('id')); $insert = array( 'id' => $this->input->post('id'), 'qty' => 1, 'price' => $product->price, 'name' => $product->name ); if ($product->option_name) { $insert['options'] = array( $product->option_name => $product->option_values[$this->input->post($product->option_name)] ); } $this->cart->insert($insert); redirect('shop'); } function remove($rowid) { $this->cart->update(array( 'rowid' => $rowid, 'qty' => 0 )); redirect('shop'); } } |
Create a new file in a folder -> Models
1 |
/application/models/products_mod.php |
Write Code like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Products_mod extends CI_Model { function get_all() { $results = $this->db->get('products')->result(); foreach ($results as $result) { if ($result->option_values) { $result->option_values = explode(',',$result->option_values); } } return $results; } function get($id) { $results = $this->db->get_where('products', array('id' => $id))->result(); $result = $results[0]; if($result->option_values) { $result->option_values = explode(',',$result->option_values); } return $result; } } |
Create a new file in a folder -> Views
1 |
/application/models/products_view.php |
Write Code like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
<!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>Shop</title> <style type="text/css"> body { font: 13px Arial; } #products { text-align: center; float: left; } #products ul { list-style-type: none; margin: 0px; } #products li { width: 150px; padding: 4px; margin: 8px; border: 1px solid #ddd; background-color: #eee; -moz-border-radius: 4px; -webkit-border-radius: 4px } #products .name { font-size: 15px; margin: 5px; } #products .price { margin: 5px; } #products .option { margin: 5px; } #cart { padding: 4px; margin: 8px; float: left; border: 1px solid #ddd; background: #eee; -moz-border-radius: 4px; -webkit-border-radius: 4px } #cart table { width: 320px; border-collapse: collapse; text-align: left; } #cart th { border-bottom: 1px solid #aaa; } #cart caption { font-size: 15px; height: 30px; text-align: left; } </style> </head> <body> <div id="products"> <ul> <?php foreach ($products as $product): ?> <li> <?php echo form_open('shop/add'); ?> <div class="name"><?php echo $product->name; ?></div> <div class="thumb"> <?php echo img(array( 'src' => 'images/' . $product->image, 'class' => 'thumb', 'alt' => $product->name )); ?> </div> <div class="price">$ <?php echo $product->price; ?></div> <div class="option"> <?php if ($product->option_name) : ?> <?php echo form_label($product->option_name, 'option_'. $product->id); ?> <?php echo form_dropdown( $product->option_name, $product->option_values, NULL, 'id="option_' . $product->id .'"' ); ?> <?php endif; ?> </div> <?php echo form_hidden('id', $product->id); ?> <?php echo form_submit('action', 'Add to Cart'); ?> <?php echo form_close(); ?> </li> <?php endforeach; ?> </ul> </div> <?php if ($cart = $this->cart->contents()) : ?> <div id="cart"> <table> <caption>Shopping Cart</caption> <thead> <tr> <th>Item name</th> <th>Option</th> <th>Price</th> </tr> </thead> <tbody> <?php foreach ($cart as $item) : ?> <tr> <td><?php echo $item['name']; ?></td> <td> <?php if($this->cart->has_options($item['rowid'])) { foreach ($this->cart->product_options($item['rowid']) as $option => $value) { echo $option . ": <em>" . $value . "</em>"; } } ?> </td> <td><?php echo $item['subtotal']; ?></td> <td class="remove"><?php echo anchor('shop/remove/'.$item['rowid'], 'X'); ?></td> </tr> <?php endforeach; ?> <tr class="colspan"> <td colspan="2"><strong>Total</strong></td> <td>$ <?php echo $this->cart->total(); ?></td> </tr> </tbody> </table> </div> <?php endif; ?> </body> </html> |