Jump to content

function loop help


drisate

Recommended Posts

hey guys i have an unlimited category systeme and i need to print out the path of a specifique id ...

 

Ex:

Canada -> Montreal -> Comercial -> Shop -> T-Shirts

 

The SQL has a normal unlimited category structure with a top_id to know the parent category.

id,  nom,  type,  nom_en,  top_id,  ordre 

 

My code so fare looks like this

function filigrame($categ, $nom){ 

    $filigrame = @mysql_fetch_assoc(@mysql_query("SELECT * FROM page_categ WHERE id = '$categ'"));
    $nom = $nom." -> $filigrame[nom]";
    
    if ($filigrame[top_id]!="0"){
        filigrame($filigrame[top_id], $nom);
    }else{
       return "Accueil ".$nom; 
    }

}

 

Right now it only print out categories that has no parent id (top_id=0). Looks like for some reason it's not relooping at the if $filigrame[top_id]!="0" part ...

 

Any help would be very appreciated. Thx in advance.

Link to comment
Share on other sites

Nothing jumps out at me as being wrong, my best guess it that since you are surpressing errors, you are encountering an error and don't know ones occuring. Try changing...

 

$filigrame = @mysql_fetch_assoc(@mysql_query("SELECT * FROM page_categ WHERE id = '$categ'"));

...to...

 

$sql = "SELECT * FROM page_categ WHERE id = '$categ'";
$result = mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR);
$filigrame = mysql_fetch_assoc($result);

NB: This does require error_reporting being on, which I hope it is, if not then replace the trigger_error with die and remove the , E_USER_ERROR part.

Link to comment
Share on other sites

No errors

 

function filigrame($categ, $nom){ 

    $sql = "SELECT * FROM page_categ WHERE id = '$categ'";
    $result = mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR);
    $filigrame = mysql_fetch_assoc($result);
    $nom = $nom." -> $filigrame[nom]";
    
    if ($filigrame[top_id]!="0"){
        filigrame($filigrame[top_id], $nom);
    }else{
       return "Accueil ".$nom; 
    }

}

 

This is my table

 

CREATE TABLE `page_categ` (
  `id` int(9) NOT NULL auto_increment,
  `nom` varchar(250) NOT NULL,
  `type` int(9) NOT NULL,
  `nom_en` varchar(250) NOT NULL,
  `top_id` int(9) NOT NULL,
  `ordre` int(10) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=150 DEFAULT CHARSET=utf8 AUTO_INCREMENT=150 ;

-- 
-- Contenu de la table `page_categ`
-- 

INSERT INTO `page_categ` (`id`, `nom`, `type`, `nom_en`, `top_id`, `ordre`) VALUES (97, 'Répertoire des commerces', 6, '', 0, 0),
(81, 'Vêtements hommes', 4, '', 79, 0),
(80, 'Vêtements femmes', 4, '', 79, 0),
(98, 'Carte de  l'artère', 2, '', 97, 0),
(79, 'Ma plaza mode', 2, '', 78, 0),
(78, 'Ma plaza', 6, '', 0, 0),
(99, 'Liste par addresse', 2, '', 97, 0),
(82, 'Chaussures', 4, '', 79, 0),
(83, 'Accessoires, lingerie & maillots', 4, '', 79, 0),
(84, 'Ma plaza gourmande', 2, '', 78, 0),
(85, 'Alimantation spécialisée', 4, '', 84, 0),
(86, 'Restaurant', 4, '', 84, 0),
(87, 'Ma plaza divertissante', 3, '', 78, 0),
(89, 'Ma plaza mariage ', 2, '', 78, 0),
(90, 'Nouvelles', 4, '', 89, 0),
(91, 'Répertoire', 4, '', 89, 0),
(92, 'Portraits', 4, '', 89, 0),
(93, 'Promotion', 4, '', 89, 0),
(94, 'Témoignages', 4, '', 89, 0),
(95, 'Ma plaza, mes services', 2, '', 78, 0),
(96, 'Ma plaza kitch', 3, '', 78, 0),
(100, 'Liste par catégorie', 2, '', 97, 0),
(101, 'Liste par ordre alphabétique', 2, '', 97, 0),
(102, 'Actualités', 6, '', 0, 0),
(103, 'Nouvelles', 4, '', 102, 0),
(104, 'Évènement', 4, '', 102, 0),
(105, 'Nouveaux commerces', 4, '', 102, 0),
(106, 'Promotions', 4, '', 102, 0),
(107, 'Photos', 4, '', 102, 0),
(108, 'Emplois', 4, '', 102, 0),
(109, 'À propos de la plaza', 6, '', 0, 0),
(110, 'Historique', 2, '', 109, 0),
(112, '59 à 79', 3, '', 110, 0),
(113, '59 à 89', 3, '', 110, 0),
(114, '59 à 2009', 3, '', 110, 0),
(115, 'Accessibilité', 3, '', 109, 0),
(116, 'Stationnements', 3, '', 109, 0),
(117, 'Heures d'ouvertures', 3, '', 109, 0),
(118, 'FAQ', 4, '', 109, 0),
(119, 'Médias', 3, '', 109, 0),
(120, 'Photos et vidéos', 2, '', 109, 0),
(121, 'Photos', 4, '', 120, 0),
(122, 'Vidéos', 4, '', 120, 0),
(123, 'La SDC', 6, '', 0, 0),
(124, 'Mission', 3, '', 123, 0),
(125, 'Conseil d'aministration', 3, '', 123, 0),
(126, 'Équipe', 3, '', 123, 0),
(127, 'Partenaires', 3, '', 123, 0),
(128, 'Contact', 3, '', 123, 0),
(129, 'Annoncer ici', 3, '', 0, 0),
(130, 'Membres SDC', 6, '', 0, 0),
(131, 'Services', 3, '', 130, 0),
(132, 'Nouveautés', 4, '', 130, 0),
(133, 'Locaux à louer', 4, '', 130, 0),
(134, 'PR@M', 3, '', 130, 0),
(135, 'Intranet', 2, '', 130, 0),
(136, 'Modifier votre profile', 3, '', 135, 0),
(137, 'Communiqués', 3, '', 135, 0),
(138, 'Règlements', 3, '', 135, 0),
(139, 'Assemblées générales', 3, '', 135, 0),
(140, 'Dates importantes', 3, '', 135, 0),
(141, 'Info - Plaza', 3, '', 135, 0),
(142, 'FAQ - Membres', 3, '', 135, 0),
(143, 'Liens utiles', 3, '', 135, 0),
(144, 'Photos et vidéos', 2, '', 130, 0),
(145, 'Photos', 4, '', 144, 0),
(146, 'Vidéos', 4, '', 144, 0),
(147, 'Contact', 0, '', 0, 0),
(148, 'Accueil', 1, '', 0, 0),
(149, 'Cyberbuletin', 0, '', 0, 1);

 

As you can see, Restaurant is id 86 and that category is in top_id 84. 84 is Ma plaza gourmande and thats in top_id 78. 78 is Ma plaza and has 0 as top_id so that would be the top of the categories.

 

That should show

Ma plaza -> Ma plaza gourmande -> Restaurant

 

But it's not returning anything

 

my php code to call the function looks like this:

//[...]
$mysql = "(page.titre like '%$recherche%' or page.texte1 like '%$recherche%' or page.texte2 like '%$recherche%' or page.texte3 like '%$recherche%' or page.description_photo like '%$recherche%' or page.entreprise like '%$recherche%' or page.adresse like '%$recherche%') and page.categ=page_categ.id"; //and page_categ.id=page.categ and page_categ.type!='5'
//[...]
$mselect = mysql_query("SELECT * FROM page, page_categ where $mysql limit $offset, $nombre_par_page") or die(mysql_error());
while ($list = mysql_fetch_array($mselect)) {
//[...]
echo filigrame($list[categ], "");
//[...]
}
//[...]

Link to comment
Share on other sites

Got it ... it needed a return ...

 

function filigrame($categ, $nom){ unset($filigrame);

    $sql = "SELECT * FROM page_categ WHERE id = '$categ'";  
    $filigrame = @mysql_fetch_assoc(@mysql_query("$sql"));
    
    $nom = $nom." -> $filigrame[nom]";
    
    if ($filigrame[top_id]!="0"){
        return filigrame($filigrame[top_id], $nom);
    }else{
        return "Accueil ".$nom; 
    }

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.