5

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

B0oMi
  • 53
  • 1
  • 1
  • 5

2 Answers2

4

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;
}
?>
mbomb007
  • 3,788
  • 3
  • 39
  • 68
Nick Prozee
  • 2,823
  • 4
  • 22
  • 49
  • Thanks for your reply! I'm only getting the row count from all tables together that way instead of the count for each table individually – B0oMi Aug 17 '14 at 23:15
  • try the example, maybe you need to move the Count+1 were you need it – Nick Prozee Aug 17 '14 at 23:21
  • okay now it's looking really good, is it possible to echo out the count in the table header? – B0oMi Aug 17 '14 at 23:28
  • Sure, just place the 'echo $count' in the header where you need it – Nick Prozee Aug 17 '14 at 23:29
  • hmm it isn't quite working, i want the count in here but it's returning 1 for each title ´foreach ($this->datacount as $datacount) { $count = $count + 1; echo '' . $head->machine_text . ' ' . $head->machine_name . ' [' . $count . ']'; }´ – B0oMi Aug 17 '14 at 23:33
  • The problem here is that you want to place the counter in the header after the count, while the header is defined before the count. take a look here: http://api.jquery.com/replacewith/ . Also please mark as answer if this is what youre looking for, ;) – Nick Prozee Aug 17 '14 at 23:35
  • this should also help: http://stackoverflow.com/questions/1309452/how-to-replace-innerhtml-of-a-div-using-jquery – Nick Prozee Aug 17 '14 at 23:37
  • Thank you Nick! The replacewith did the job! – B0oMi Aug 17 '14 at 23:44
  • 1
    What about `$count++;` – kiwixz Aug 30 '14 at 21:40
  • Is the same as i posted but i think $count = $count + 1 is more readable – Nick Prozee Aug 30 '14 at 21:42
1

i think this will help.

<?php

$count = 0;

foreach ($names as $name){
    echo  "<td>".$count."</td>
           <td>".$name."</td>";

    $count += 1;
}

?>
Typewar
  • 825
  • 1
  • 13
  • 28
Developer Kwame
  • 168
  • 1
  • 5
  • 14