我如何用PHP中的按钮删除LIMIT?

Ath*_*hax 1 php mysqli

现在我想创建一个按钮,从我的PHP代码中删除(LIMIT 3),所以显示所有行而不是限制3

$query = "SELECT * FROM artikler ORDER BY 1 DESC LIMIT 3"; 
$result = $mysqli->query($query);


while ($row = $result->fetch_array(MYSQLI_ASSOC)){
    $titel = $row['art_titel'];
    $indhold = $row['art_indhold'];

echo "<article class='article-box'>";
echo "<div class='art-title'>$titel</div>";
echo "<div class='art-content'>$indhold</div>";
echo "<div class='art-view'><a href='pages/e-artikel.php?id=$row[art_id]'>Vis</a></div>";
echo "<div class='art-date'>".date_format(date_create($row["art_dato"]),'d/m/Y')."</div>";
echo "</article>";
}
Run Code Online (Sandbox Code Playgroud)

我一直在考虑制作一个IF ELSE声明,但我不确定如何才能做到这一点,请详细解释我对php很新.

Pra*_*sar 6

JS代码:

<script>
    function getRecords(type){
        location.href= 'action.php?limited='+type;
    }
</script>
Run Code Online (Sandbox Code Playgroud)

HTML代码:

<input type='button' onclick="getRecords('all')" id='submit_button' name='showall' value='Show All'/>
<input type='button' onclick="getRecords('limited')" id='submit_button' name='showlimited' value='Show Top 3'/>
Run Code Online (Sandbox Code Playgroud)

PHP代码:

$limited = $_GET['limited'];//As per code change it to GET
$condition = '';
if($limited == 'limited'){
    $condition = ' LIMIT 3';
}
$query = "SELECT * FROM artikler ORDER BY 1 DESC $condition";
Run Code Online (Sandbox Code Playgroud)

  • 建议:不要使用`$ _REQUEST`,最好使用特定的全局数组(在本例中为`$ _GET`) (2认同)