สร้าง Controller แรกสำหรับดึงข้อมูลมาจากตาราง products ในฐานข้อมูล

ไฟล์ที่เกี่ยวข้อง
app/controllers/products_controller.php
app/models/product.php
app/views/products    <-- อันนี้เป็น Folder

-------------------------------------------------------------
app/controllers/products_controller.php

<?php
class ProductsController extends AppController {

var $name='Products';

function index(){
$products=$this->Product->find('all'); //เรียกข้อมูล product ทุก record จากตาราง products
$this->set('products',$products); //ส่งค่าให้ view โดยเก็บใส่ตัวแปร products แบบ array

}

}

-----------------------------------------------------------
app/models/product.php

<?php
class Product extends AppModel{
var $name='Product';
}

-----------------------------------------------------------
app/views/products/index.ctp

<?php
//print_r($products); //แสดงค่าออกมา ทดสอบน่ะคับ
?>
<table>
<tr><td>ชื่อสินค้า</td></tr>
<?php
foreach $products as $product:
?>
<tr><td><?php echo $product['Product']['name'];?></td></tr>
<?php
endforeach;
?>
</table>

จริงๆ ยังไม่เรียบร้อยดีนะครับ เดี๋ยวจะค่อยๆ ปรับให้มันหยิบใส่ตระกร้า, มีภาพ และ ราคา ด้วยนะครับ