2014-05-02 4 views
0

J'ai une table qui a 4 colonnes de catégories, la suivante retourne le premier category_id. J'ai 3 autres appelés category_id2, category_id3 et Category_id4.Inclure plusieurs colonnes avec SQL et PHP

$sql_ca = "SELECT * FROM tour_category ORDER BY sort"; 
         $result_ca = mysql_query($sql_ca); 
         $categories = $Manager->fetchAssoc($result_ca); 
         if(!empty($categories)){ 
          foreach($categories as $cat){ 

           $sql_tour = "SELECT t.* FROM tour t 
           WHERE 
            t.touronline = 'yes' 
            AND t.category_id = {$cat['id']} 
            AND t.location_id = {$city['id']} 
            and t.type = 'P' 
           ORDER BY t.tourcode     
           "; 
           $result_tour = mysql_query($sql_tour); 
           $tours = $Manager->fetchAssoc($result_tour); 

J'ai essayé de mettre en

AND t.category_id, t.category_id2, t.category_id3,t.category_id4 = {$cat['id']} 

Mais je ne peux pas obtenir que cela fonctionne, des conseils?

+0

Utiliser '= {$ cat [ 'id']} 'pour chaque colonne. – Mihai

+0

essayer d'écho requête et le copier et le vérifier manuellement –

Répondre

0

Vous avez besoin d'une condition WHERE pour chaque colonne. Si vous voulez que toutes les colonnes de catégorie pour correspondre à $cat['id'] il ressemblerait à ceci:

WHERE 
    t.touronline = 'yes' 
    AND t.category_id = {$cat['id']} 
    AND t.category_id2 = {$cat['id']} 
    AND t.category_id3 = {$cat['id']} 
    AND t.category_id4 = {$cat['id']} 
    AND t.location_id = {$city['id']} 
    AND t.type = 'P' 

Si vous voulez une colonne de catégorie pour correspondre $cat['id'] il ressemblerait à ceci:

WHERE 
    t.touronline = 'yes' 
    AND (t.category_id = {$cat['id']} 
     OR t.category_id2 = {$cat['id']} 
     OR t.category_id3 = {$cat['id']} 
     OR t.category_id4 = {$cat['id']}) 
    AND t.location_id = {$city['id']} 
    AND t.type = 'P' 
+0

Oui, cela a fonctionné parfaitement. Merci – user3477311

Questions connexes