2009-11-07 74 views
0

我有两张桌子。类别和产品。(采用笨)如何检索类别名称?

分类

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>&nbsp;</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; 
} 
+3

请提供您用来构建$ products集合的查询。 – 2009-11-07 17:01:06

+0

已更新。谢谢。 – shin 2009-11-07 18:19:04

回答

1

不知道在哪里$产品定义/在PHP脚本分配高,但通过与类似下面的查询的结果相关联,你可以得到的类别名称作为$列表的一部分:

SELECT P.*, C.Name AS CatName -- note the aliasing to avoid conflict with P.Name 
FROM products P 
LEFT JOIN categories C ON C.id = P.category_id 
--WHERE here for some optional where/order by clause etc.