Limiting rows of table and appending rows.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • harman30
    New Member
    • Sep 2015
    • 19

    Limiting rows of table and appending rows.

    In this code I am getting posts from database in table, table displays posts in three columns, now I want to add some jQuery to limit the number of rows and add a button which on clicking appends few rows to table I am not a professional programmer may be something like slice should be used to limit number of rows.

    Code:
    $sql = "SELECT * FROM posts";
    $query = $db->prepare($sql);
    $query->execute();
    
    <table>
    <tr>
    <?php do { //horizontal looper?>
    <td>
    <div>id</div>          
    <div>title</div>
    <div>body</div>          
    <div>date</div>
    </td>
    <?php
    $row = $query->fetch(PDO::FETCH_ASSOC);
    if (!isset($nested_List)) {
    $nested_List= 1;
    }
    if (isset($row) && is_array($row) && $nested_List++%3==0) {
    echo "</tr><tr>";
    }
    } while ($row); //end horizontal looper 
    ?>
    </table>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    it depends if you want to fetch the complete table through PHP and (un)hide parts of it or if you want to dynamically append rows.

    for the latter you will need to use AJAX, which tends to be a JavaScript question. additionally, if you limit the results in PHP, you’d do that in SQL using the LIMIT clause (there’s no point fetching thousands of rows when you want to only display ten)

    Comment

    Working...