如何检索类别名称?

时间:2009-11-07 16:58:39

标签: php mysql

我有两张桌子。类别和产品。(使用codeigniter)

分类

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>";

$ product在controllers / 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; 
}

2 个答案:

答案 0 :(得分:1)

它被称为内部联接http://www.google.com/search?q=mysql+inner+join+example此网站在教授SQL语法方面也非常出色:http://sqlzoo.net/3b.htm

答案 1 :(得分:1)

不确定PHP脚本中$ product的定义/分配更高,但通过将其与查询结果相关联,您可以将类别名称作为$ list的一部分获取:

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.