Jeniiifer Posted March 6, 2011 Share Posted March 6, 2011 Bonjour, je connais peu à propos du php et j'ai besoin d'une fonction qui retourne une liste de catégories de la forme:id_category, name, description, nbr_childrenAvec nbr_children est le nombre de sous_catégories du catégorie mise en valeur.L'entête de la fonction est : static public function AffichCategories($id_lang,$id_parent, $active = true, $order = true){} Voilà ma requête sql, juste manque le nbr de sous catégories: $q="SELECT ps_category.id_category ,ps_category_lang.name, ps_category_lang.description FROM ps_category , ps_category_lang WHERE ps_category.id_category=ps_category_lang.id_category and ps_category_lang.id_lang ='".$_REQUEST['id_lang']."' and ps_category.active=1 and ps_category.id_parent='".$_REQUEST['id_parent']."'"; Il y a déjà une fonction getChildren($id_parent, $id_lang, $active = true) dans la classe catégorie, donc si pour chaque id_category on fait un count sur la liste fournie par getChildren, je crois que le problème sera résolu.J'ai juste un problème avec le code !!Aide svp !! Link to comment Share on other sites More sharing options...
BVince Posted March 6, 2011 Share Posted March 6, 2011 Bonjour,Pour renvoyer le nombre de catégories enfants d'une catégorie : public function getNbrChildrens($id_category, $id_lang, $active = true) { if($id_category=='' || $id_lang=='') return false; return Db::getInstance()->getRow(' SELECT count(c.id_category) as nbrChildrens, cl.`name`, cl.`description` FROM `'._DB_PREFIX_.'category` c LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON c.`id_category` = cl.`id_category` WHERE `id_lang` = '.intval($id_lang).' AND c.`id_parent` = '.intval($category) .($active ? 'AND `active` = 1' : '')); } Link to comment Share on other sites More sharing options...
Jeniiifer Posted March 6, 2011 Author Share Posted March 6, 2011 Désolée Partner mais ça n'a pas marché, et encore je crois pas que c'est la bonne requête !!Selon ce que tu as écrit, le nbr_children sera toujours égal à 1 !!! Link to comment Share on other sites More sharing options...
BVince Posted March 6, 2011 Share Posted March 6, 2011 Pas du tout, puisque la condition porte sur le id_parent, donc nombre de catégorie qui ont pour parent l'id_catégory demandé..... Link to comment Share on other sites More sharing options...
Jeniiifer Posted March 6, 2011 Author Share Posted March 6, 2011 Tu as fait un count sur l'id_category et puis group by sur le même attribut, donc ça va donner le nombre de répétions d'un même id_category dans la colonne id_category, or c'est un clé donc le résultat vaut toujours 1. Et pour la condition sur l'id_parent, ça n'a rien à faire avec le nbr_children, ça va juste limiter la liste en précisant le parent.En faite, tu l'a testé?!! Link to comment Share on other sites More sharing options...
BVince Posted March 6, 2011 Share Posted March 6, 2011 Je n'ai pas encore eu le temps de tester, je regarde en réalisant les tests et je te donnerai la requête correspondante.... Link to comment Share on other sites More sharing options...
Olecorre Posted March 6, 2011 Share Posted March 6, 2011 public function getNbrChildrens($id_lang, $id_category = 0, $active = true) { return Db::getInstance()->getRow(' SELECT c.`id_parent`, c.`level_depth`, count(c.`id_category`) as nbrChildrens, cl.`name`, cl.`description` FROM `'._DB_PREFIX_.'category` c LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_parent` = cl.`id_category` AND `id_lang` = '.intval($id_lang).') WHERE c.`id_parent`'.($id_categorie==0 ? '>0 ' : '='.$id_categorie).($active ? ' AND `active` = 1' : '').' GROUP BY c.`id_parent`, c.`level_depth`, cl.`name`, cl.`description` ORDER BY c.`level_depth` '); } Voici plutôt la fonction avec requête que je ferai.La fonction retournera, l'id_parent, le nombre de sous catégorie, le nom et la description de la catégorie parent, le tout classé pat levelSoit tu donnes à la fonction un $id_catégorie, soit si tu lui en donne pas alors elle te donnera la liste de catégorie ayant des sous catégories. Link to comment Share on other sites More sharing options...
Broceliande Posted March 6, 2011 Share Posted March 6, 2011 et en objet ? static public function AffichCategories($id_lang,$id_parent, $active = true) { $cat = new Category($id_parent); $subCats = $cat->getSubCategories($id_lang, $active); // pour le compte $nbSubCats = count($subCats); // tu as toutes tes sous-catégories dans $subCats avec toutes les propriétés dont tu as besoin ex : $subCats[x]['name'] // un test basic pour l'ex foreach($subCats as $subCat) { echo $subCat['name']." "; } } Link to comment Share on other sites More sharing options...
Jeniiifer Posted March 10, 2011 Author Share Posted March 10, 2011 Merci, j'ai trouvé la solution ... enfiiiiin <?php include('/config/config.inc.php'); require_once('init.php'); $q="SELECT ps_category.id_category ,ps_category_lang.name, ps_category_lang.description FROM ps_category , ps_category_lang WHERE ps_category.id_category=ps_category_lang.id_category and ps_category_lang.id_lang =2 and ps_category.active=1 and ps_category.id_parent='".$_REQUEST['id_parent']."' "; $r = Db::getInstance()->ExecuteS($q); foreach($r as $cat) { $categories = Category:: getChildren($cat['id_category'], 2, true); $res=count($categories); $row[]= array('id_category' => intval($cat['id_category']),'name' => $cat['name'],'description' => $cat['description'],'nbr_children' => intval($res)); } echo(json_encode($row )); ?> Ca me permet d'afficher les sous catégories d'une catégorie donnée avec chacune le nombre de sous catégories correspondants. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now