Currently, I have an articles page that reads all the articles in my database table and prints each one on the page, starting with the most recent.
However, the system I'm making will be in use for a long time (student project for a small company) and there will be a lot of articles. I don't want the user nor the server to have to load all the articles at once on the web page. I have ideas how to do this, but I wanted to run the idea by other people first.
Conceptually, I was thinking of doing is loading a few articles on the page. At the bottom, there would be a "Show more" button or link that would read and display a few more articles from the database (hopefully without reloading the page, but I highly doubt that can work).
Practically, it would probably go something like this: Use a form, store number of displayed articles in hidden field, on submit "Show more" reload the page and use $_POST and a while loop to display the stored number + a few more articles. Here's a simplified example below.
<form method="post" action="articlePage.php">
<?php
if(isset($_POST["articleCount"])){
$articleCount = $_POST["articleCount"] + 5;
}
else{
$articleCount = 5
}
//Assuming we open the connect, execute the query and then close the connection
$count = 0;
while($result = mysqli_fetch_array($selectNews) && $count <= $articleCount){
echo $result["articleName"] . "<br/>";
count = count + 1;
}
echo "<input type='hidden' value='" . $articleCount . '" name='articleCount'/>";
?>
<input type="submit" value="Show more"/>
</form>
My questions are:
Any tips or bits of information are very appreciated! I'm a bit new at asking questions so if there is anything missing let me know
You can place your PHP code in a separate file and call it using AJAX. You could do something like this:
function getArticleCount(){
if(isset($_POST["articleCount"])){
$articleCount = $_POST["articleCount"] + 5;
}
else{
$articleCount = 5
}
//Assuming we open the connect, execute the query and then close the connection
$count = 0;
while($result = mysqli_fetch_array($selectNews) && $count <= $articleCount){
echo $result["articleName"] . "<br/>";
count = count + 1;
}
echo "<input type='hidden' value='" . $articleCount . '" name='articleCount'/>";
}
getArticleCount();
Here is an example of using AJAX and PHP together to give you a feel for it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With