-4

I need to display the num of the row (tr) using php, but I don't know how to do this because I am using quotes.

    $data['formation'] = '
    <div class="table-responsive">
        <table class="table table-bordered" style="width: auto !important;">
         <thead style="font-weight: bold;">
           <tr>
              <td>N</td>
              <td>Intitulé</td>
              <td>Organisme</td>
              <td>Date</td>
              <td>Durée (en heures)</td>
              <td>Eval. à chaud / à froid</td>
              <td>Dispositif utilisé</td>
            </tr>
          </thead>
          <tbody class="table-striped">
            <tr>
                <td>
                    **HERE NUM OF THE ROW**
                </td>
                <td>
                    <input type="text" class="form-control" name="form_intitule" id="form_intitule" readonly>
                </td>
                <td>
                    <input type="text" class="form-control" name="form_organisme" id="form_organisme" readonly>
                </td>
                <td>
                    <input type="text" class="form-control" name="form_date" id="form_date" readonly>
                </td>
                <td>
                    <input type="text" class="form-control" name="form_duree" id="form_duree" readonly>
                </td>
                <td></td>
                <td></td>
            </tr>
          </tbody>
        </table>
    </div>';
}

This is my code:

foreach($html->find('table') as $table){ 
    // returns all the <tr> tag inside $table $all_trs = $table->find('tr'); 
    $count = count($all_trs); echo $count; 
}
vard
  • 4,057
  • 2
  • 26
  • 46
Lucas Frugier
  • 277
  • 1
  • 4
  • 12

3 Answers3

0

Try this:

$count = substr_count($data['formation'], "<tr>");
echo "Total rows: ".$count;
Shailesh Katarmal
  • 2,757
  • 1
  • 12
  • 15
0

Though it's not a good solution but still simple to use. You can use substr_count

echo substr_count($data['formation'], '<tr>'); // 2
Al Amin Chayan
  • 2,460
  • 4
  • 23
  • 41
0

This is a bit of a workaround. But it works just as well.

print_r(array_count_values(str_word_count($data['formation'], 1)) );

What the above code does is count all words in the $data['formation'] string. Then, it will devide this into groups and put the results in an array.

It will then print the array and you can see how much times <tr> occurs.

Peter
  • 8,776
  • 6
  • 62
  • 95