Philipp Nesterov Philipp Nesterov - 8 months ago 75
HTML Question

Adding an echo line after an end of each if cycle result

I have a database:

id | tableid | timein | itemdesc | qty
01 | 13 | 11:00 | apples | 4
02 | 13 | 11:00 | bread | 1
03 | 13 | 11:00 | chips | 40
01 | 8 | 17:00 | coffee| 1
02 | 8 | 17:00 | rocket| 1
03 | 8 | 17:00 | snickers | 180


I get information from it sorting by the same Column (tableid). And all the information puts inside a card. One individual card for each (tableid). So I write something like this:

$getsql = "SELECT * FROM 2dopuzzler ORDER BY tableid";
$itsresult = $conn->query($getsql);

// Good one...
$current_cat = null;
while($row = $itsresult->fetch_assoc()) {
if ($row["tableid"] != $current_cat) {
$current_cat = $row["tableid"]; // Table Number
$timein = $row["timein"]; // Placing Order Time
// PRINT THE ORDER CARD
echo "<div class='col s12 m6 l3'>";
echo "<div class='card'>";
echo "<div class='card-content '>";
echo "<div class='row'>";
echo "<div class='col s12 m4 l4 card-wtime'> <i class='tiny material-icons'>add</i><span class='card-wtimetext'>$timein</span> </div>";
echo "<div class='col s12 m4 l4'> <a class=' card-nday waves-effect waves-light red'> <span class='card-ndaytext'>$current_cat</span> </a></div>";
echo "<div class='col s12 m4 l4 card-alldone-btn'> <i class='medium material-icons'>check</i> </div>";
echo "</div>";
echo "<div class='row'>";
}
echo "<div class='card-line'>";
echo " -- ". $row["itemdesc"]." <span class='card-line-q'> ". $row["qty"] . "</span>";


But the result isn't quite good. It's 'cause the structure has addtional divs at the end of each cycle... So here is how it should work:

while (i get lines from db) {
if ($row["tableid"] != $current_cat) {
// here open divs
}
// here it cycles through db lines adding to card header text

// (!) here I need closed divs witch adding NOT at an end of each db line
// BUT when $current_cat is changing!


}
//here over all endig divs...


So please help me to understad how to write such a cycle witch brings, first of all, the header (that contains two vars -- these vars are common for the rest of an info card), second -- lines witch looks like a list (item + quantity), and the last thing witch closes an info card is appendix.

Please, can you help me? )

Here a desired html output:

<div class="col s12 m6 l3">
<!-- Card 1 -->
<div class="card">
<div class="card-content ">
<div class="row">
<div class="col s12 m4 l4 card-wtime"> <i class="tiny material-icons">add</i><span class="card-wtimetext ">13:00</span> </div>
<div class="col s12 m4 l4"> <a class=" card-nday waves-effect waves-light red"> <span class="card-ndaytext">13</span> </a></div>
<div class="col s12 m4 l4 card-alldone-btn"> <i class="medium material-icons">check</i> </div>

</div>

<div class="row">

<div class="card-line">
Here goes the header of an info card
<span class="card-line-q"> Here goes the sub-header of an info card</span>
</div>

(!) Here goes cycled information from database. Like that $NAME ; $quantity

// echo " -- ". $row["itemdesc"]." <span class='card-line-q'> ". $row["qty"] . "</span>";


// HERE starts html witch I can't add...

</div>


</div>
<div class="card-action">
// here an appendix of an info card
</div>
</div>



</div>


// HERE that ends...

Answer Source

The problem is that you're not ending the DIVs from the previous category when you start a new category. So each category is nested inside the previous one.

$getsql = "SELECT * FROM 2dopuzzler ORDER BY tableid";
$itsresult = $conn->query($getsql);

// Good one...
$current_cat = null;
while($row = $itsresult->fetch_assoc()) {   
    if ($row["tableid"] != $current_cat) {
        if ($current_cat) { // end previous card
            echo "</div></div></div></div>";
        }
        $current_cat = $row["tableid"]; // Table Number
        $timein = $row["timein"]; // Placing Order Time
        // PRINT THE ORDER CARD
        echo "<div class='col s12 m6 l3'>";
        echo "<div class='card'>";
        echo "<div class='card-content '>";
        echo "<div class='row'>";
        echo "<div class='col s12 m4 l4 card-wtime'> <i class='tiny material-icons'>add</i><span class='card-wtimetext'>$timein</span> </div>";
        echo "<div class='col s12 m4 l4'>   <a class='  card-nday waves-effect waves-light red'> <span class='card-ndaytext'>$current_cat</span> </a></div>";
        echo "<div class='col s12 m4 l4 card-alldone-btn'> <i class='medium material-icons'>check</i> </div>";
        echo "</div>";
        echo "<div class='row'>";
    } 
    echo "<div class='card-line'>";
    echo " -- ". $row["itemdesc"]." <span class='card-line-q'> ". $row["qty"] . "</span>";
    echo "</div>";
}
if ($current_cat) { // end last card
    echo "</div></div></div></div>";
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download