我有兩張桌子。類別和產品。(採用笨)如何檢索類別名稱?
分類
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`shortdesc` varchar(255) NOT NULL,
`longdesc` text NOT NULL,
`status` enum('active','inactive') NOT NULL,
`parentid` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
INSERT INTO `categories` (`id`, `name`, `shortdesc`, `longdesc`, `status`, `parentid`) VALUES
(1, 'shoes', 'Shoes for boys and girls.', '', 'active', 7),
(2, 'shirts', 'Shirts and blouses!', '', 'active', 7),
...
...
產品
CREATE TABLE IF NOT EXISTS `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`shortdesc` varchar(255) NOT NULL,
`longdesc` text NOT NULL,
`thumbnail` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL,
`grouping` varchar(16) DEFAULT NULL,
`status` enum('active','inactive') NOT NULL,
`category_id` int(11) NOT NULL,
`featured` enum('true','false') NOT NULL,
`price` float(4,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;
INSERT INTO `products` (`id`, `name`, `shortdesc`, `longdesc`, `thumbnail`, `image`, `grouping`, `status`, `category_id`, `featured`, `price`) VALUES
(1, 'Game 1', 'This is a very good game.', 'What a product! You''ll love the way your kids will play with this game all day long. It''s terrific!', 'images/dummy-thumb6.jpg', 'images/dummy-main6.jpg', 'fun', 'active', 6, '', 19.95),
(2, 'Game 2', 'This is a very good game.', 'What a product! You''ll love the way your kids will play with this game all day long. It''s terrific!', 'images/dummy-thumb5.jpg', 'images/dummy-main5.jpg', 'fun', 'active', 6, '', 19.95),
...
...
Categor_id的產品是類的ID。
我有以下的PHP來顯示產品表。這隻顯示category_id。 我想顯示類別的名稱而不是id。
任何人都可以幫助我如何做到這一點?
...
...
echo "<table border='1' cellspacing='0' cellpadding='3' width='700'>\n";
echo "<tr valign='top'>\n";
echo "<th> </th><th>ID</th>\n<th>Name</th><th>Grouping</th><th>Status</th><th>Category ID</th><th>Featured</th><th>Price</th><th>Actions</th>\n";
echo "</tr>\n";
foreach ($products as $key => $list){
echo "<tr valign='top'>\n";
echo "<td align='center'>".form_checkbox('p_id[]',$list['id'],FALSE)."</td>";
echo "<td>".$list['id']."</td>\n";
echo "<td>".$list['name']."</td>\n";
echo "<td>".$list['grouping']."</td>\n";
echo "<td align='center'>".$list['status']."</td>\n";
echo "<td>".$list['category_id']."</td>\n";
//I want to show category name instead of category_id.
echo "<td>".$list['featured']."</td>\n";
echo "<td>".$list['price']."</td>\n";
echo "<td align='center'>";
echo anchor('admin/products/edit/'.$list['id'],'edit');
echo " | ";
echo anchor('admin/products/delete/'.$list['id'],'delete');
echo "</td>\n";
echo "</tr>\n";
}
echo "</table>";
和$產品在控制器/ products.php
...
...
$data['title'] = "Manage Products";
$data['main'] = 'admin_product_home';
$data['products'] = $this->MProducts->getAllProducts();
$data['allcategories'] = $this->MCats->getAllCategories();
$data['categories'] = $this->MCats->getCategoriesDropDown();
$this->load->vars($data);
$this->load->view('dashboard');
定義,這是getAllProducts()
function getAllProducts(){
$data = array();
$Q = $this->db->get('products');
if ($Q->num_rows() > 0){
foreach ($Q->result_array() as $row){
$data[] = $row;
}
}
$Q->free_result();
return $data;
}
請提供您用來構建$ products集合的查詢。 – 2009-11-07 17:01:06
已更新。謝謝。 – shin 2009-11-07 18:19:04