idh1337 idh1337 - 2 months ago 8
PHP Question

PHP Mysql query to output a list in HTML

I know the title of my question is maybe a bit stupid but my programming and english skills are literally not the best.

Im trying to echo some Data out of my SQL but one of my queries is always missing some output.

If you look at the table screenshot there shoud be another row in the HTML but it doesnt output.

My question is: where is the bug that the script sometimes does not output all of the data

echo '<ul>';
$bestellangaben = $pdoCon->query("SELECT * FROM `bestellangaben` WHERE SeitenID = $currentSeitenID ORDER BY ID DESC");
$printline = '';
$currentGrouping = -1;
foreach ($bestellangaben as $eintrag) {
if($currentGrouping == $eintrag['grouping']) {
$printline = $printline . ' - ' . $eintrag['bestlellangaben'];
}else{
if($printline != ''){
echo '<li>' . $printline . '</li>';
}
$printline = $eintrag['bestlellangaben'];
$currentGrouping = $eintrag['grouping'];
}
}
echo '</ul>';


Table of Data:

The Table

Output of product 187:

HTML of Product 187

Answer

I think you miss to output the last $printline. When you reach the last dataset with the same grouping as the previous dataset, there is no output anymore. It just adds the content to $printline. You do it as the following:

    echo '<ul>';
$bestellangaben = $pdoCon->query("SELECT * FROM `bestellangaben` WHERE SeitenID = $currentSeitenID ORDER BY ID DESC");
$printline = '';
$currentGrouping = -1;
foreach ($bestellangaben as $eintrag) {
    if($currentGrouping == $eintrag['grouping']) {
        $printline = $printline . ' - ' . $eintrag['bestlellangaben'];
    }else{
        if($printline != ''){
            echo '<li>' . $printline . '</li>';
        }
        $printline = $eintrag['bestlellangaben'];
        $currentGrouping = $eintrag['grouping'];
    }
}

if($printline != ''){
    echo '<li>' . $printline . '</li>';
}
echo '</ul>';