Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Un {section} dans un {section} ?...

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: French
View previous topic :: View next topic  
Author Message
Phenol
Smarty Rookie


Joined: 17 Apr 2007
Posts: 6

PostPosted: Sun Nov 30, 2008 12:42 am    Post subject: Un {section} dans un {section} ?... Reply with quote

Bonjour,

Je voudrais lister des Produits par Catégories comme par exemple :

* Catégorie 1
----- Produit 1
----- Produit 2
----- Produit 3
* Catégorie 2
----- Produit 4
----- Produit 5

etc.

Pour lister mes Catégories j'ai utilisé un :
Code:
$sql_cats = 'select * from cat';
$tpl->assign('cats', $db->getAll($sql_cats));


avec côté Template un :
Code:
{section name=c loop=$cats}
<p>{$cats[c].name}</p>
{/section}


Je ne sais pas comment lister les Produits de chaque Catégorie dans mon template...

Quelqu'un pourrait m'aider ?

Merci par avance !
Back to top
View user's profile Send private message
Gowser
Smarty Pro


Joined: 19 Feb 2008
Posts: 104
Location: Nantes (France)

PostPosted: Thu Sep 10, 2009 2:54 pm    Post subject: Reply with quote

Bonjour,

Le plus simple serait de creer des objet pour les categories et les produits, ce qui vous permettrait d'avoir sur un objet categorie une fonction qui s'appellerai par exemple 'listerProduitParCategorie' qui pourrait etre appelé dans un tpl

Exemple simple:

Classe Categorie.php
Code:

<?php

require_once 'Produit.php';

class Categorie{
   private $id;
   private $name;
   
   public function getId(){
      return $this->id;
   }
   
   public function setId($value){
      $this->id = $value;
   }
   
   public function getName(){
      return $this->name;
   }
   
   public function setName($value){
      $this->name = $value;
   }
   
   /*
    * Fonction permettant de recupere un tableau d'objet categorie
    */
   public function listerCategorie(){
      $sql = 'SELECT id, name FROM cat';
      
      $result = mysql_query($sql);
      
      while($row = mysql_fetch_array($result)){
         $categorie = new Categorie();
         $categorie->setId($row['id']);
         $categorie->setName($row['name']);
         
         $retour[] = $categorie;
      }
      
      return $retour;
   }
   
   /*
    * Fonction permettant de recupere un tableau d'objet produit par apport a une categorie
    */
   public function listerProduitParCategorie(){
      $sql = 'SELECT id, name FROM produit WHERE cat_id='.self::getId();
      
      $result = mysql_query($sql);
      
      while($row = mysql_fetch_array($result)){
         $produit = new Produit();
         $produit->setId($row['id']);
         $produit->setName($row['name']);
         
         $retour[] = $produit;
      }
      
      return $retour;
   }
}

?>


Classe Produit.php
Code:

<?php

class Produit{
   private $id;
   private $name;
   
   public function getId(){
      return $this->id;
   }
   
   public function setId($value){
      $this->id = $value;
   }
   
   public function getName(){
      return $this->name;
   }
   
   public function setName($value){
      $this->name = $value;
   }
}

?>


Ce qui permettrait d'avoir dans votre tpl :
Code:

{php}
   $categorie = new Categorie();
   
   $this->assign('categorieListe', $categorie->listerCategorie())
{/php}
{section name=indexCat loop=$categorieListe}
<p>
   {$categorieListe[indexCat]->getName()}
   {assign var='produitListe' value=$categorieListe[indexCat]->listerProduitParCategorie()}
   {section name=indexProduit loop=$produitListe}
   <br/>
   {$produitListe[indexProduit]->getName()}
   {/section}
</p>   
{/section}


J'espère que ca répondra à votre question

Voilou, ++
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: French All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP