JOIN über drei Tabellen - PHP Ausgabe zerbricht mir den Schädel

n8active

Grünschnabel
Liebe Forenmitglieder,


ich hab mir eine tolle SQL-Abfrage zusammengebastelt, die soweit auch prima funktioniert. Allerdings bereitet mir die Ausgabe via PHP großes Kopfzerbrechen.

Zur Projektinfo:
Ich muss eine Art Dashboard erstellen, welches alle Bestellungen inklusive Tisch-Nummer, Anzahl, Artikel, Preis etc. als "Quittung" bzw "Bon" anzeigt.

Hier die SQL-Abfrage:
SQL:
SELECT 
                                (coalesce(b.TISCHNR, '')) as TISCHNR, 
                                b.BESTELLZEIT,
                                a.IDBESTELLUNG, 
                                a.ANZAHL, 
                                a.BEZEICHNUNG AS artikel1, 
                                a.PREIS, 
                                a.GANG, 
                                a.HAUPTARTIKELBEZEICHNUNG, 
                                z.BEZEICHNUNG AS zusatz1
FROM
                                                        monitorbestellung b left outer join
                                                        monitorbestellungartikel a on a.lfdnrbest = b.lfdnrbest left outer join
                                                        monitorbestellungzusatz z on z.lfdnrbestartikel = a.lfdnrbestartikel left OUTER JOIN
                                                        raum r on r.raumnr = b.raumnr left outer join
                                                        tisch t on t.nr = b.tischnr
                      
                            GROUP BY
                           

                                b.TISCHNR, 
                                b.BESTELLZEIT,  
                                a.IDBESTELLUNG, 
                                a.ANZAHL, 
                                a.BEZEICHNUNG, 
                                a.PREIS, 
                                a.GANG, 
                                a.HAUPTARTIKELBEZEICHNUNG,
                                z.BEZEICHNUNG                               
                               
                           
                            ORDER BY
                                b.TISCHNR

Die Ausgabe via PHP schaut so aus:

PHP:
        while ($row = ibase_fetch_object($sth)) {
            $tablenr = $row->TISCHNR;
            $time =    $row->BESTELLZEIT;              
            $ticket = $row->IDBESTELLUNG; 
            $artcount = $row->ANZAHL; 
            $article = $row->ARTIKEL1; 
            $price = $row->PREIS; 
            $course = $row->GANG; 
            $maincat = $row->HAUPTARTIKELBEZEICHNUNG; 
            $sideart = $row->ZUSATZ1; 
            

           
                       
           echo '<table class="table">
                                <thead>
                                    <tr class="head">
                                        <td class="head">';
            echo                                 date('H:i', strtotime($time))." ";
            echo '                            </td>
                                        <td>Gang: '. $course .'
                                        </td>

                                        <td>Table:'. $tablenr .'</td>
                                    </tr>
                                </thead>

                                    <tr>
                                        <td></td>
                                        <td>'. $maincat .'</td>
                                        <td></td>
                                    </tr>

                                    <tr>
                                        <td>'. $artcount .'x</td>
                                        <td>'. $article .'<br>'. $sideart .'</td>
                                        <td>'. $artcount * $price .' &euro;</td>
                                    </tr>

                                    <tr>
                                        <td colspan="3">
                                        something here
                                        </td>
                                       
                                    </tr>       
                            </table>
                ';

    }

Soweit ok. Nun möchte ich aber die Ausgabe so gestalten, dass alle Bestellungen zur jeweiligen Tisch-Nummer zugeordnet sind. Momentan werden alle - ich nenne sie mal Artikel-Bons - einzeln angezeigt.

Ich hab schon mit Schleifen rumgefuchtelt, komme aber zu keinem vernünftigen Ergebnis.

Bin absoluter PHP-Neuling. Hat jemand einen Tipp wie man das sinnvoll lösen kann?

Vielen Dank.
 
Da die Frage ja für die PHP-Auswertung ist, wäre ein Beispiel des SQL-Resultates noch ganz interessant.
 
Etwa so kann man das lösen. Innerhabe der Schleife vergleichen, ob ein neuer Tisch beginnt.
PHP:
$lastTableNr = 0;
while ($row = ibase_fetch_object($sth)) {
    $tablenr = $row->TISCHNR;
    $time =    $row->BESTELLZEIT;              
    $ticket = $row->IDBESTELLUNG; 
    $artcount = $row->ANZAHL; 
    $article = $row->ARTIKEL1; 
    $price = $row->PREIS; 
    $course = $row->GANG; 
    $maincat = $row->HAUPTARTIKELBEZEICHNUNG; 
    $sideart = $row->ZUSATZ1;

    if($lastTableNr != $tablenr){
        echo "<table class='table'>\n";
        echo "<thead>\n";
        echo "<tr class='head'>\n";
        echo "<td class='head'>".date('H:i', strtotime($time))."</td>\n";
        echo "<td>Gang: {$course}</td>\n";
        echo "<td>Table:{$tablenr}</td>\n";
        echo "</tr>\n";
        echo "</thead>\n";

        echo "<tr>\n";
        echo "<td></td>\n";
        echo "<td>{$maincat}</td>\n";
        echo "<td></td>\n";
        echo "</tr>\n";

    }

    echo "<tr>\n";
    echo "<td>{$artcount}</td>\n";
    echo "<td>{$article}<br>{$sideart}</td>\n";
    echo "<td>". $artcount * $price ." &euro;</td>\n";
    echo "</tr>\n";


    if($lastTableNr != $tablenr) echo "</table>\n";
    $lastTableNr = $tablenr;

}
 
Hallo Yaslaw,

danke - das macht Sinn! Komisch nur, dass jetzt trotzdem nicht die Tabelle vollständig erstellt wird. Folgende Ausgabe bekomme ich bei diesem Code:

PHP:
$lastTableNr = 0;
while ($row = ibase_fetch_object($sth)) {
    $tablenr = $row->TISCHNR;
    $time =    $row->BESTELLZEIT;              
    $ticket = $row->IDBESTELLUNG;
    $artcount = $row->ANZAHL;
    $article = $row->ARTIKEL1;
    $price = $row->PREIS;
    $course = $row->GANG;
    $maincat = $row->HAUPTARTIKELBEZEICHNUNG;
    $sideart = $row->ZUSATZ1;

    if($lastTableNr != $tablenr){
        echo "<table border='1'>";
       
        echo "<tr>";
        echo "<td>".date('H:i', strtotime($time))."</td>";
        echo "<td>Gang: {$course}</td>";
        echo "<td>Table:{$tablenr}</td>";
        echo "</tr>";
       

        echo "<tr>";
        echo "<td></td>";
        echo "<td>{$maincat}</td>";
        echo "<td></td>";
        echo "</tr>";

    }

    echo "<tr>";
    echo "<td>{$artcount}</td>";
    echo "<td>{$article}<br>{$sideart}<br>";
    echo $artcount * $price ." &euro;</td>";
    echo "<td></td>";
    echo "</tr>";


    if($lastTableNr != $tablenr) echo "</table>";
    $lastTableNr = $tablenr;

}

Wäre es bei dieser Aufgabe eventuell sinniger das ganze mit jQuery auseinander zu pflücken und hinterher wieder zusammenzusetzen? Jemand eine Idee oder einen Rat?

Vielen Dank und Grüße in die Suisse!
 

Anhänge

  • table.jpg
    table.jpg
    46,8 KB · Aufrufe: 3
Yaslaw,

danke für den Tipp! Ich habe die Lösung gefunden.

PHP:
<?php
$tisch_first = 0;
$tisch_last = 0;
while ($row = ibase_fetch_object($sth)) {
    $tablenr = $row->TISCHNR;
    $time =    $row->BESTELLZEIT;              
    $ticket = $row->IDBESTELLUNG;
    $artcount = $row->ANZAHL;
    $article = $row->ARTIKEL1;
    $price = $row->PREIS;
    $course = $row->GANG;
    $maincat = $row->HAUPTARTIKELBEZEICHNUNG;
    $sideart = $row->ZUSATZ1;

    $tisch_first = $tablenr;
   
    if($tisch_last != $tablenr){
        echo "<table border='1'>";
       
        echo "<tr>";
        echo "<td>".date('H:i', strtotime($time))."</td>";
        echo "<td>Gang: {$course}</td>";
        echo "<td>Table:{$tablenr}</td>";
        echo "</tr>";
       

        echo "<tr>";
        echo "<td></td>";
        echo "<td>{$maincat}</td>";
        echo "<td></td>";
        echo "</tr>";

 }  

    echo "<tr>";
    echo "<td>{$artcount}</td>";
    echo "<td>{$article}<br>{$sideart}<br>";
    echo $artcount * $price ." &euro;</td>";
    echo "<td></td>";
    echo "</tr>";


    if($tablenr != $tablenr) echo "</table>";
   
    $tisch_last = $tisch_first;

}
?>
 

Neue Beiträge

Zurück