2

I have a custom table (ERA_Data) in my WP database, which I'd like to add pagination, column sort & search to. Currently the following code successfully grabs the data (over 4000 entries) & displays them;

<?php

  global $wpdb;
  $ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data;");

  echo "<table>";
  foreach($ERA_Data as $ERA_Data){
    echo "<tr>";
    echo "<td>".$ERA_Data->PartNo."</td>";
    echo "<td>".$ERA_Data->Make."</td>";
    echo "<td>".$ERA_Data->Carline."</td>";
    echo "<td>".$ERA_Data->Model."</td>";
    echo "<td>".$ERA_Data->Description."</td>";
    echo "<td>".$ERA_Data->Start_Year."</td>";
    echo "<td>".$ERA_Data->End_Year."</td>";
    echo "</tr>";
    }
  echo "</table>";

?>

Just so you know, this is all new to me - so I'm happy to read up on further info, but if anyone can provide some code examples that may help - I'd be extremely appreciative.

With regards to pagination, I think 50 or 100 items per page would do. I'd imagine sort order & search will be far more useful in any case.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jason
  • 73
  • 9

1 Answers1

0

I would juts work on this line for anything you need:

  $ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data;");

so change it like this for showing only a 100, 50, etc. results

  $ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data LIMIT 100;");

or

   $ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data LIMIT 50;");

and for pagination, this link would help so much What is the best way to paginate results in SQL Server

for sorting, depending on your column, use this syntax

   $ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data ORDER BY ***COLUMN NAME HERE***;");

Let me know if you have any questions!

Community
  • 1
  • 1
Mahyar Ebadi
  • 143
  • 1
  • 6
  • Hi Mahyar, many thanks for your quick response - I've decided that a search form (that then displays relevant data in a table) from that custom table - is the way to go. I'm hoping I can build something with Gravity Forms. – Jason Nov 14 '12 at 23:07