简体   繁体   中英

Showing Mysql table data in html form with php

I'm showing data from Mysql table with php inside HTML table, but i need 2 more things to accomplish to the table:

1- How to Alternate colors for table rows to use 4 different css classes, I am using now class='success' i have 3 more that i want to use and each one should apply to each table row, how to do it? any simple example like a loop or something?

2- Data showing from the oldest record in the table to the latest one, and i want to show the reverse, so the last record shows first in the html table.

My code for this table:

<?php echo "<table class='table'>
            <thead>
              <tr>
                <th>Order#</th>
                <th>Name</th>
                <th>Total</th>
                <th>Submitted On</th>
                <th>Status</th>
              </tr> </thead>"; 

                    while($row = mysqli_fetch_array($result))
                      {
                      echo "<tr class='success'>";
                      echo "<td>" . $row['lite_order_id'] . "</td>";
                      echo "<td>" . $row['lite_item_name'] . "</td>";
                      echo "<td>" . $row['lite_email'] . "</td>";
                      echo "<td>" . $row['lite_country'] . "</td>";
                      echo "<td>" . $row['lite_order_total'] . "</td>";
                      echo "</tr>"; }
             echo "</table>"; ?>

1 - Use id attribute to style individual elements. Check it out here

2 - In your MySQL query, use ORDER BY :

(I presume you have an id column here)

$query = "SELECT * FROM `yourtable` ORDER BY `id` ASC";

See here

要获取反向记录,可以使用mysql ORDER BY子句或PHP函数array_reverse()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM