Reputation: 271
Im trying to create a button like the one in the youtube commentaries but i cant do it, I tried creating a static variable, so every time i press a button to load more the variable will be increased but it just can be increased once, maybe a for loop can help but i dont have any idea of how to implement it. (Im loading the ids i have saved in a mysql database but i will change the id for the commentaries i have in other database.) Screen shots:
<title>Pruebas</title>
<center>
<?php
require 'conexion.php';
class Ver_prublicaciones extends Conexion{
public function Ver_prublicaciones(){
parent::__construct();
}
public function Ver($num){
$sql="SELECT * FROM USUARIOS";
$resultNum=$this->con->query($sql)->rowCount();
$sql2="SELECT * FROM USUARIOS LIMIT 0,$num";
$resultCon=$this->con->query($sql2);
$array=$resultCon->fetchAll(PDO::FETCH_ASSOC);
foreach ($array as $value){
echo $value['ID'] . "<br>";
}
}
}
$numero;
$numero=2;
if(isset($_POST['cargar'])){
$numero++;
}
$prueba=new Ver_prublicaciones();
$prueba->Ver($numero);
?>
<form method="post">
<input type="submit" name="cargar" value="Cargar más resultados">
</form>
</center>
Upvotes: 0
Views: 680
Reputation: 13635
You need to pass the current value of $numero
to be able to increase it on every click.
PHP
// if we got a numero value posted, use that, otherwise set it to 2 as default
$numero = isset($_POST['numero']) ? $_POST['numero'] : 2;
if(isset($_POST['cargar'])){
$numero++;
}
HTML
<form method="post">
<input type="hidden" name="numero" value="<?= $numero ?>" />
<input type="submit" name="cargar" value="Cargar más resultados">
</form>
Here we added the current value of $numero
as a hidden field that gets passed on every click.
Upvotes: 1