2009-11-07 6 views
0

J'ai deux tables. Catégories et produits. (En utilisant CodeIgniter)Comment récupérer le nom de la catégorie?

Catégories

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), 
... 
... 

Produits

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 dans les produits est id de catégories.

J'ai le php suivant pour montrer la table de produits. Et cela montre seulement category_id. Je souhaite afficher le nom des catégories au lieu de l'identifiant.

Quelqu'un peut-il m'aider à le faire?

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

et produit $ est défini dans les contrôleurs/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'); 

et c'est 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

S'il vous plaît fournir la requête que vous utilisez pour construire la collection de produits $. –

+0

mis à jour. Merci. – shin

Répondre

1

Je ne sais pas où le produit de $ a été défini/attribué plus élevé dans le script php, mais en l'associant à la suite d'une requête comme suit, vous pouvez obtenir la catégorie nom en tant que partie de la liste de $:

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. 
Questions connexes