WhiteShadow
WhiteShadow

Reputation: 323

Display user defined number of MySql results with dynamicly updating content [php]

I'm trying to limit the number of rows that the page shows according to the users selection from a drop down(think like a store page "show number of items per page")

At the moment I'm using php to call MySql and then echo out my results. I know that php cant do anything after the page loads. The next thing that comes to mind is java script. However I have no experience with java script, I'm fair normal with java.

What are other options that you would suggest?
note: I want to limit my while loop not the mysql result.

Here is my code as it stands now:

<form name="input" action="EditPartyP.php" method="post">
<tr> 
    <td>Party ID</td><td>Party Name</td>
    <td>Start Date</td><td>End Date</td><td>Sales</td><td>VIEW: 

The drop down selection options that are being offered.

<select>
   <option value="10">10</option>
   <option value="20">20</option>
   <option value="40">40</option>
   <option value="80">80</option>
</select>

   </td>

The results from the query being displayed, I under stand that I will have to change this because the php is done when the user sees the output html.

<?php
     $ID = $_SESSION['ID'];
     $result = PartyData::PartyLookupByID($_SESSION['ID'], "DESC");

This loop now just runs until the result runs out of data. I need to make it stop when the users number is reached as well.

     while ($row = mysqli_fetch_array($result)) {
     echo "<tr>";
     echo '<form name="input" action="EditPartyP.php" method="post">';

    echo "<td>" . $row['PartyID'] . "</td>";
    echo "<td>" . $row['PartyName'] . "</td>";
    echo "<td>" . $row['sDate'] . "</td>";
    echo "<td>" . $row['eDate'] . "</td>";
    echo "<td>" . $row['PartyOrderTotal'] . " </td>";
    echo '<input type="hidden" value=" ' . $row['PartyID'] . '" name="PartyID">';

    echo "<td>" . '<button type="submit" value="Edit" name="Action">Edit</button>' . "</td>";

    echo '</form>';
    echo "</tr>";
    }
?>
</table>

Thank you for reading and a comment helps more than a down vote.

Upvotes: 0

Views: 65

Answers (3)

John Guan
John Guan

Reputation: 744

You might want to look into https://datatables.net if you want features such as per page row diaplay or so called pagination.

Upvotes: 2

Dumindu Madushanka
Dumindu Madushanka

Reputation: 504

You can use SQL Limit command to retrieve specific number of data.

SELECT column_name(s) FROM table_name LIMIT number;

Upvotes: 0

Stefan
Stefan

Reputation: 1258

Based on the selected page (via a $_GET['var'] and the users choice of number of records per page, you have to adjust your query with the limit statement.

SELECT * FROM table LIMIT 10, 50

Would start at the 10th record and will select 50 records max.

Upvotes: 0

Related Questions