Sorting MySQL data in a GRID


/ Published in: PHP
Save to your folder(s)

Here is a working example on how to sort data in a grid.


Copy this code and paste it in your HTML
  1. <?php
  2. //basic mysql connection, you'd want to use a DB class or something
  3. $connect = @mysql_connect("localhost","username","password") or die("Could not connect to server.");
  4. $db = @mysql_select_db("database") or die(mysql_error());
  5. $query = "SELECT * FROM table";
  6. $result = @mysql_query($query) or die("Could not execute query");
  7. $check = mysql_num_rows($result);
  8. if ($check == 0) {
  9. echo "Table is empty.";
  10. } else {
  11. ?>
  12. <table>
  13. <?php
  14. $i = 1;
  15. while ($row = mysql_fetch_array($result)) {
  16. if ($i == 1) {
  17. ?>
  18. <tr>
  19. <?php
  20. }
  21. ?>
  22. <td><?php echo $row['column']; ?></td>
  23. <?php
  24. if ($i == 7) { /* here we have 7 columns per row, if you want 10 columns just change this value to 10 instead of 7 */
  25. $i = 0;
  26. ?>
  27. </tr>
  28. <?php
  29. }
  30. $i++;
  31. }
  32. ?>
  33. </tr>
  34. </table>
  35. <?php
  36. }
  37. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.