1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Table from SQL

Discussion in 'PHP' started by Fiverscripts, Jul 6, 2013.

  1. #1
    Hi all,

    im trying to manipulate a table from sql database via php.. i have managed to return the data and now my question is how do i limit that data to say 25 records.. and then have the option to go to next 25 rows and back?

    Here is my current code this returns the table with the data (but all of it) so will continue if more then 25 rows etc..

    
                include('db_connect.php');
     
                $sql = "SELECT * FROM `records`";
                $result = mysql_query($sql);
                echo "<table border='1' padding='2'>
                    <tr>
                    <th>ID</th>
                    <th>Test</th>
                    <th>Test 2</th>
                    <th>Test 3</th>
                    <th>Test 4</th>
                    <th>Test 5</th>
                    <th>Test 6</th>
                    <th>Test 7</th>
                    <th>Test 8</th>
                    <th>Test 9</th>
                    <th>Test 10</th>
                    </tr>";
                while ($row = mysql_fetch_array($result))
                    {
                        echo "<tr>";
                        echo "<td>" . $row['ID'] . "</td>";
                        echo "<td>" . $row['Test 1'] . "</td>";
                        echo "<td>" . $row['Test 2'] . "</td>";
                        echo "<td>&#163;" . $row['Test 3'] . "</td>";
                        echo "<td>&#163;" . $row['Test 4'] . "</td>";
                        echo "<td>&#163;" . $row['Test 5'] . "</td>";
                        echo "<td>&#163;" . $row['Test 6'] . "</td>";
                        echo "<td>&#163;" . $row['Test 7'] . "</td>";
                        echo "<td>" . $row['Test 8'] . "</td>";
                        echo "<td>" . $row['Test 9'] . "</td>";
                        echo "<td>&#163;" . $row['Test 10'] . "</td>";
                        echo "</tr>";
                    }
                echo "</table>";
            
    PHP:
    Thats my current code.. it connects and returns all records what's the best way to return 25 of the records and then be able to filter forward and back (per 25)

    thanks,

    Matt
     
    Fiverscripts, Jul 6, 2013 IP
  2. ryan_uk

    ryan_uk Illustrious Member

    Messages:
    3,983
    Likes Received:
    1,022
    Best Answers:
    33
    Trophy Points:
    465
    #2
    You will need to use offset in your query (either with offset itself or part of your limit). Just calculate the number of queries to skip based upon paging.

    domain.com/interesting_date/?page=2
    Code (markup):
    So then you would know you need to skip 25 records.

    $offset = ($page_number - 1) * 25;
    $limit = 25;
    Code (markup):
    Then your query would be:

    SELECT useful_names FROM useful_table WHERE  useful = "yes" LIMIT $limit,$offset;
    Code (markup):
    Or:

    SELECT useful_names FROM useful_table WHERE  useful = "yes" LIMIT $limit OFFSET $offset;
    Code (markup):
     
    ryan_uk, Jul 6, 2013 IP