2010-04-26 62 views
0

嘿,我想添加一個按鈕(鏈接),點擊時會過濾分頁結果。如何在此分頁中添加過濾器按鈕?

我是新來的PHP(和編程一般),想添加一個按鈕,像「汽車」,並點擊時,它在我的分頁腳本更新了2個MySQL查詢,在這裏看到:

正如你可以看到,汽車硬編碼類別中,我希望它是動態的,所以當單擊鏈接時,它會將任何id或類位於查詢的類別部分中。

1:

$record_count = mysql_num_rows(mysql_query("SELECT * FROM explore WHERE category='automotive'")); 

2:

$get = mysql_query("SELECT * FROM explore WHERE category='automotive' LIMIT $start, $per_page"); 

這是我使用的整個電流的PHP分頁腳本:

<?php 

    //connecting to the database 
    $error = "Could not connect to the database"; 
    mysql_connect('localhost','root','root') or die($error); 
    mysql_select_db('ajax_demo') or die($error); 

    //max displayed per page 
    $per_page = 2; 

    //get start variable 
    $start = $_GET['start']; 

    //count records 
    $record_count = mysql_num_rows(mysql_query("SELECT * FROM explore WHERE category='automotive'")); 

    //count max pages 
    $max_pages = $record_count/$per_page; //may come out as decimal 

    if (!$start) 
     $start = 0; 

    //display data 
    $get = mysql_query("SELECT * FROM explore WHERE category='automotive' LIMIT $start, $per_page"); 
    while ($row = mysql_fetch_assoc($get)) 
    { 
    // get data 
    $name = $row['id']; 
    $age = $row['site_name']; 

    echo $name." (".$age.")<br />"; 

    } 

    //setup prev and next variables 
    $prev = $start - $per_page; 
    $next = $start + $per_page; 

    //show prev button 
    if (!($start<=0)) 
      echo "<a href='pagi_test.php?start=$prev'>Prev</a> "; 

    //show page numbers 

    //set variable for first page 
    $i=1; 

    for ($x=0;$x<$record_count;$x=$x+$per_page) 
    { 
    if ($start!=$x) 
     echo " <a href='pagi_test.php?start=$x'>$i</a> "; 
    else 
     echo " <a href='pagi_test.php?start=$x'><b>$i</b></a> "; 
    $i++; 
    } 

    //show next button 
    if (!($start>=$record_count-$per_page)) 
      echo " <a href='pagi_test.php?start=$next'>Next</a>"; 

    ?> 

回答

0

實施例用2鏈接/類:

<a href='script.php?category=automotive'>automotive</a> <a href='script.php?category=sports'>sports</a> 

內的script.php:

$category = mysql_real_escape_string($_GET['category']); 
$record_count = mysql_num_rows(mysql_query("SELECT * FROM explore WHERE category='$category'")); 
... 
$get = mysql_query("SELECT * FROM explore WHERE category='$category' LIMIT $start, $per_page"); 
+0

這類作品。它會進入第一頁,但是當我點擊另一個頁碼如[2]時,什麼都不顯示。有想法該怎麼解決這個嗎? – ClarkSKent 2010-04-26 02:27:48

+0

nvm,我添加category = $ category&url後點擊的項目。謝謝您的幫助! – ClarkSKent 2010-04-26 02:33:23

0

編輯:我只張貼了這個答案響應OP,說明他是新的節目。良好的衛生習慣在一開始就很容易學會,變得習慣,或者很久以後,很難養成習慣。

http://us2.php.net/manual/en/security.database.sql-injection.php

也請閱讀這篇文章的安全數據庫。將您的查詢編寫爲:

$start = mysql_real_escape_string($_GET['start']); 
settype($start, 'integer'); 
$category = mysql_real_escape_string($_GET['category']); 
$record_count = mysql_num_rows(mysql_query("SELECT * FROM explore WHERE category='%s'", $category)); 
... 
$get = mysql_query("SELECT * FROM explore WHERE category='%s' LIMIT %d, %d", $category, $start, $per_page); 

即使只是爲了練習,編寫額外的安全代碼總是值得的。數據庫的基本安全規則是:永遠不要信任用戶輸入,始終檢查生成的輸出。由於輸入來自查詢字符串並且針對數據庫運行,因此必須對其進行過濾。

+0

感謝您的信息,我一定會牢記這一點。 – ClarkSKent 2010-04-26 22:35:26

+0

總是很高興分享安全信息,他們多年來一直打敗我。如果我能幫助一個人避免頭痛,那麼我已經成功了。 – Gabriel 2010-04-27 06:49:19

相關問題