Перечислять числа в каждой строке HTML - PullRequest
0 голосов
/ 22 октября 2018

Может ли кто-нибудь помочь мне подсчитать количество строк в таблице HTML?Я загружаю таблицу из базы данных, и мне нужно получить столбец с номером строки.Это будет новый столбец перед идентификатором.

if ($result->num_rows > 0) {
    echo "<table id='table' class='tablesorter'>
    <thead>
    <tr>

    <th>ID</th>
    <th>Fecha (a/m/d) Hora </th>
    <th>Valor (Lux)</th>
    <th>Ubicacion</th>
    <th>Estado</th>
    </tr>
    </thead><tbody>";

    // output data of each row
    while($row = $result->fetch_assoc()) {

        echo "<tr>

        <td>". $row["id"]. "</td>
        <td>" . $row["date"]. "</td> 
        <td>" . $row["value"]. "</td>
        <td>" . $row["latitud"].  $row["longitud"]. "</td> 
        <td>" . $row["Estado"]. "</td>
        </tr>";

    }
    echo "</tbody></table>";
} else {
    echo "0 results";
}

$con->close();
?> 

</body>
</html>

Ответы [ 3 ]

0 голосов
/ 22 октября 2018

Попробуй это.

        <table id="example1" class="table table-bordered table-striped">
                  <thead>
                    <tr>
                     <th>ID</th>
                     <th>Fecha (a/m/d) Hora </th>
                     <th>Valor (Lux)</th>
                     <th>Ubicacion</th>
                     <th>Estado</th>
                    </tr>
                  </thead>
                    <?php $count = 1; ?>
                    <?php foreach ($rows as $row):?> 
                    <tr>
                      <td><?= $count ++; ?></td>
                      <td><?= $row["id"] ?></td>
                      <td><?= $row["date"] ?></td>
                      <td><?=  $row["value"] ?></td>
                      <td><?= $row["latitud"] ?></td>

                    </tr>
                   <?php endforeach ?>
         </table>
0 голосов
/ 24 октября 2018

Это помогло, спасибо!

$sql = "SELECT * FROM table";
$result = $con->query($sql);
$rows = $result->num_rows;


if ($result->num_rows > 0) {
    $row_number = 0;
    if (count($rows)) {

        echo "<table id='table' class='tablesorter'>
        <thead>
        <tr>
        <th>Count</th>
        <th>ID</th>
        <th>Fecha (a/m/d) Hora </th>
        <th>Valor (Lux)</th>
        <th>Ubicacion</th>
        <th>Estado</th>
        </tr>
        </thead><tbody>";

        // output data of each row
        while($row = $result->fetch_assoc()) {


                echo "<tr>
                <td>". ++$row_number. "</td>
                <td>". $row["id"]. "</td>
                <td>" . $row["date"]. "</td> 
                <td>" . $row["value"]. "</td>
                <td>" . $row["latitud"].  $row["longitud"]. "</td> 
                <td>" . $row["Estado"]. "</td>
                </tr>";

        }
    echo "</tbody></table>";
    }
} else {
    echo "0 results";
}

$con->close();
?> 
0 голосов
/ 22 октября 2018

Подход Php может выглядеть примерно так: создать переменную, увеличить ее и отобразить при выводе строк:

<?php
$row_number = 0;
if (count($rows)) {
    echo "<table>
    <thead>
    <tr>
        <th>Row no.</th>
        <th>ID</th>
        <th>Name</th>
    </tr>
    </thead><tbody>";
    foreach($rows as $row) {
        echo "<tr>
        <td>". ++$row_number. "</td>
        <td>". $row["id"]. "</td>
        <td>" . $row["name"]. "</td> 
        </tr>";
    }
    echo "</tbody></table>";
} else {
    echo "0 results";
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...