2015-02-09 58 views
1

現在我想提出一個按鈕,可以消除我的PHP代碼(LIMIT 3),所以所有的行顯示具有限制的,而不是在3如何在php中刪除一個按鈕的限制?

$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>"; 
} 

我一直在思考作出IF ELSE聲明,但即時通訊不確定我如何能做到這一點,請做出詳細的解釋我很新的PHP。

+0

我的想法是不是隻有他們三個有一個按鈕,上面寫着顯示所有快速舉報通道。 – Athax 2015-02-09 12:28:07

+0

使用$ _GET或$ _POST,執行限制請求 - >檢查請求被給出並且包含x或沒有限制的限制=>基本的if語句 – donald123 2015-02-09 12:31:41

+0

errrm,兩個按鈕? – Mawg 2015-02-09 12:31:45

回答

6

JS代碼:

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

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'/> 

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"; 
+2

建議:不要使用'$ _REQUEST',最好使用特定的全局數組(在本例中爲'$ _GET') – Phate01 2015-02-09 12:37:10

1

添加HTML提交按鈕:

<form method="post"> 
    <input type="submit" value="showAll" name="showAll" /> 
</form> 

在PHP中得到按下按鈕代碼if/else

if(isset($_POST['showAll'])){ 
    $query = "SELECT * FROM artikler ORDER BY 1 DESC"; 

}else{ 
    $query = "SELECT * FROM artikler ORDER BY 1 DESC LIMIT 3"; 
} 

$result = $mysqli->query($query); 
// rest of the code...