Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP count rows from foreach loop

Tags:

foreach

php

count

I'm using a "foreach" loop in PHP to create tables from my MySQL data

What I'm trying to achieve is to count the amount of rows being returned by the loop

The loop is creating new tables for each "machine" and fills the "contracts" as new rows but whenever I try to count the rows it returns the count from all tables together instead of only the a single one.

Here's my code:

<?php

foreach ($this->datatitle as $head) {

    $cards = 1;

    echo '<table id="cards" class="cards">';

    echo '<tr>';

    foreach ($this->datacount as $datacount) {

        echo '<td>' . $head->machine_text . ' ' . $head->machine_name . ' [' . $datacount->count . ']</td>';

    }

    echo '</tr>';
    echo '<tr>';
    echo '<td>';
    echo '<ul id="sortable" class="connectedSortable">';

    foreach ($this->data as $body) {

        if ($head->machine_text == $body->machine_text) {

            echo '<li class="ui-state-default">Auftrag: ' . $body->aufnr;
            echo '<br>' . $body->matnr . ' ' . $body->matxt;
            echo '<br>Menge ' . $body->gamng;
            echo '<br><br>';
            echo 'Start: ' . $body->gstrp;
            echo '<br>Ende: ' . $body->ssavd . '</li>';

            if ($cards++ == 10) {
                break;
            }

        } else {

        }

    }

    echo '</td>';
    echo '</tr>';
    echo '</table>';

}

?>

The $cards defines the amount of rows want to display, but i want to count the rows which aren't displayed aswell.

tl;dr create tables with foreach, want to count rows from single table

like image 278
B0oMi Avatar asked Aug 17 '14 23:08

B0oMi


People also ask

How do you count foreach data?

foreach is a php construct, and doesn't have any items - arrays do. using count($array) returns the number of elements in it.

How to use count in for loop in PHP?

The "right" way The count is now assigned to the variable $j so the function is only called once. The assignment $j = count($array) is part of the for loop, separated by a comma from the $i = 0 assignment. It is only called once at the start of the loop.

How do I add a counter in foreach?

Use a for loop instead of foreach, which has a count for you: for(int i = 0; i < array. length; i++) { var item = array[i]; Console. WriteLine("Current count: {0}", i + 1); //Do your thing. }


1 Answers

Above youre foreach loop, define a counter.

$count = 0

Then in your foreach loop:

$count = $count + 1

After your foreach loop:

Echo $count

Example:

<?php

foreach ($this->datatitle as $head) {

$count = 0;
$cards = 1;

echo '<table id="cards" class="cards">';

echo '<tr>';

foreach ($this->datacount as $datacount) {
    $count = $count + 1;
    echo '<td>' . $head->machine_text . ' ' . $head->machine_name . ' [' . $datacount->count . ']</td>';

}

echo '</tr>';
echo '<tr>';
echo '<td>';
echo '<ul id="sortable" class="connectedSortable">';

foreach ($this->data as $body) {

    if ($head->machine_text == $body->machine_text) {

        echo '<li class="ui-state-default">Auftrag: ' . $body->aufnr;
        echo '<br>' . $body->matnr . ' ' . $body->matxt;
        echo '<br>Menge ' . $body->gamng;
        echo '<br><br>';
        echo 'Start: ' . $body->gstrp;
        echo '<br>Ende: ' . $body->ssavd . '</li>';

        if ($cards++ == 10) {
            break;
        }

    } else {

    }

}

echo '</td>';
echo '</tr>';
echo '</table>';
echo $count;
}
?>
like image 130
Nick Prozee Avatar answered Sep 23 '22 02:09

Nick Prozee