2016-03-20 24 views
0

Dies ist meine Kategorie & Produkte SQL/PHP-Schema.Kategorie - Produkte [PHP, MySQL PDO]

Ich brauche die <hr> für jedes meiner Kategorien für jede Produkte und nicht echo. Dies ist mein Code:

$sql = $db->query("SELECT * from cat,products where cat.cat_id = products.cat_id order by products.cat_id desc"); 
while ($row = $sql->fetch(PDO::FETCH_ASSOC)){ 
    $ID = $row["id"]; 
    $Title = $row["title"]; 
    $CatID = $row["cat_id"]; 

    $row["cat_id"] = $row["cat_name"]; 
    switch ($row["cat_id"]){ 
     case $row["cat_id"] == $row["cat_name"]: 
      echo "<hr>"; 
      break; 
     default: 

      break; 
    } 

    echo $row["cat_id"]; 
    echo "<a href='test.php?cat=$CatID&prod=$ID'>$Title</a><br/>"; 
} 

Image: enter image description here

Dank und sorry für mein schlechtes Englisch ...

+1

'echo "hr";' meinen Sie 'echo "


";' * richtig * –

+0

ja, ich kann? benutze das nicht von dieser Website becase "stackoverflow.com" verwende das für "HTML TAG" und render das ... – hasan

+1

Ich habe deine Frage mit 'hr' bearbeitet. Verwenden Sie nur geeignetes Styling. –

Antwort

0

Sie können die folgenden aufgrund der Tatsache, schreiben, dass Ihre SQL-Anweisung sortiert durch die Kategorien:

 $tmp_category = '';  

      $sql = $db->query("SELECT * from cat,products where cat.cat_id = products.cat_id order by products.cat_id desc"); 
       while ($row = $sql->fetch(PDO::FETCH_ASSOC)){ 


       $ID = $row["id"]; 
       $Title = $row["title"]; 
       $CatID = $row["cat_id"]; 

    if ($row["cat_id"] <> $tmp_category){ //you can use != instead of <> as well 

     $tmp_category = $row["cat_id"]; 
     echo "<hr>";   
     echo $row["cat_id"]; 
     echo "<a href='test.php?cat=$CatID&prod=$ID'>$Title</a><br/>"; 

    } 
    else 
    { 

     echo $row["cat_id"]; // this is optional, you can delete the cat ID from here and it will show you only the above link. 
     echo "<a href='test.php?cat=$CatID&prod=$ID'>$Title</a><br/>"; 
    } 


     } 
+0

danke es geht wahr, wirklich danke: *: **** **********: X – hasan

+0

haha! Kein Problem, Kumpel. – Greg