2010-07-26 98 views
0

我想動態添加或連接多個sql查詢。如何動態添加多個sql查詢到paginaiton?

我正在創建一個分頁腳本,我試圖在這種情況下添加一些過濾器按鈕或鏈接,當用戶點擊鏈接時,它會將該ID添加到sql查詢中,然後顯示結果。

爲了更好地理解我的意思,我會告訴你我工作的腳本:

下面你將看到,我有三個環節;市場營銷,汽車和體育。這些鏈接代表過濾器選擇,因此當點擊「營銷」時,sql查詢將插入'category = $ ids'的市場營銷。這工作正常,除了當我想添加多個選擇,如選擇「市場營銷」和「體育」而不是'汽車'顯示。

是否有一種簡單的方法來添加多個ID到SQL連接而不是一個?

我希望這是有道理的,如果你需要任何進一步的解釋讓我知道。感謝您的幫助。

<?php 
    $ids=$_GET['id']; 
    echo $ids; 

    $pageN = $_GET['page']; 
    echo $pageN; 
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>SUPERMAN</title> 
<link href="../style.css" rel="stylesheet" type="text/css" /> 


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script> 
<script type="text/javascript" > 

$(document).ready(function(){ 

// Filter content.  
    $("a.category").click(function() { 

     var this_id = $(this).attr('id'); 
     $("#pagination").load('index.php?&id='+ this_id+'&page=1'); 

    }); 
}); 
</script> 


<style> 
.paginate { 
font-family:Arial, Helvetica, sans-serif; 
    padding: 3px; 
    margin: 3px; 
} 

.paginate a { 
    padding:2px 5px 2px 5px; 
    margin:2px; 
    border:1px solid #999; 
    text-decoration:none; 
    color: #666; 
} 
.paginate a:hover, .paginate a:active { 
    border: 1px solid #999; 
    color: #000; 
} 
.paginate span.current { 
    margin: 2px; 
    padding: 2px 5px 2px 5px; 
     border: 1px solid #999; 

     font-weight: bold; 
     background-color: #999; 
     color: #FFF; 
    } 
    .paginate span.disabled { 
     padding:2px 5px 2px 5px; 
     margin:2px; 
     border:1px solid #eee; 
     color:#DDD; 
    } 

    li{ 
     padding:4px; 
     margin-bottom:3px; 
     background-color:#BBBECB; 
     list-style:none;} 

    ul{margin:6px; 
    padding:0px;} 

</style> 
</head> 

<body> 
<p>Fitler Results</p> 
<br /> 
<a href="#" class="category" id="marketing">Marketing</a> 

<a href="#" class="category" id="automotive">Automotive</a> 

<a href="#" class="category" id="sports">Sports</a> 

<br /> 
<br /> 


<?php 
    include('connect.php'); 

    $tableName="explore";  
    $targetpage = "index.php"; 
    $limit = 2; 

    $query = "SELECT COUNT(*) as num FROM $tableName WHERE category='$ids'"; 
    $total_pages = mysql_fetch_array(mysql_query($query)); 
    $total_pages = $total_pages[num]; 

    $stages = 3; 
    $page = mysql_escape_string($_GET['page']); 
    if($page){ 
     $start = ($page - 1) * $limit; 
    }else{ 
     $start = 0; 
     } 

    // Get page data 
    //$query1 = "SELECT * FROM $tableName LIMIT $start, $limit"; 
    $query1 = "SELECT * FROM explore WHERE category='$ids' ORDER BY category LIMIT $start, $limit"; 
    $result = mysql_query($query1); 

    // Initial page num setup 
    if ($page == 0){$page = 1;} 
    $prev = $page - 1; 
    $next = $page + 1;       
    $lastpage = ceil($total_pages/$limit);  
    $LastPagem1 = $lastpage - 1;      


    $paginate = ''; 
    if($lastpage > 1) 
    { 




     $paginate .= "<div class='paginate'>"; 
     // Previous 
     if ($page > 1){ 
      $paginate.= "<a href='$targetpage?page=$prev'>previous</a>"; 
     }else{ 
      $paginate.= "<span class='disabled'>previous</span>"; } 



     // Pages  
     if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up 
     { 
      for ($counter = 1; $counter <= $lastpage; $counter++) 
      { 
       if ($counter == $page){ 
        $paginate.= "<span class='current'>$counter</span>"; 
       }else{ 
        $paginate.= "<a href='$targetpage?&id=$ids&page=$counter'>$counter</a>";}     
      } 
     } 
     elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few? 
     { 
      // Beginning only hide later pages 
      if($page < 1 + ($stages * 2))  
      { 
       for ($counter = 1; $counter < 4 + ($stages * 2); $counter++) 
       { 
        if ($counter == $page){ 
         $paginate.= "<span class='current'>$counter</span>"; 
        }else{ 
         $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}      
       } 
       $paginate.= "..."; 
       $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>"; 
       $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";  
      } 
      // Middle hide some front and some back 
      elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2)) 
      { 
       $paginate.= "<a href='$targetpage?page=1'>1</a>"; 
       $paginate.= "<a href='$targetpage?page=2'>2</a>"; 
       $paginate.= "..."; 
       for ($counter = $page - $stages; $counter <= $page + $stages; $counter++) 
       { 
        if ($counter == $page){ 
         $paginate.= "<span class='current'>$counter</span>"; 
        }else{ 
         $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}      
       } 
       $paginate.= "..."; 
       $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>"; 
       $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";  
      } 
      // End only hide early pages 
      else 
      { 
       $paginate.= "<a href='$targetpage?page=1'>1</a>"; 
       $paginate.= "<a href='$targetpage?page=2'>2</a>"; 
       $paginate.= "..."; 
       for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++) 
       { 
        if ($counter == $page){ 
         $paginate.= "<span class='current'>$counter</span>"; 
        }else{ 
         $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}      
       } 
      } 
     } 

       // Next 
     if ($page < $counter - 1){ 
      $paginate.= "<a href='$targetpage?page=$next'>next</a>"; 
     }else{ 
      $paginate.= "<span class='disabled'>next</span>"; 
      } 

     $paginate.= "</div>";  


} 
echo $total_pages.' Results'; 
// pagination 
echo $paginate; 
?> 

<ul id="pagination"> 

<?php 


     while($row = mysql_fetch_array($result)) 
     { 

     echo '<li>'.$row['site_name'].'</li>'; 

     } 

    ?> 
</ul> 


</body> 
</html> 

回答

3

我不知道什麼$ IDS包含,但MySQL有一個IN()運算符,它就像一個或聲明IE:

...WHERE category IN(1,2,3,4,5)...

所以$ IDS將需要一個逗號分隔列表,無論是實際標題還是標識。希望這可以幫助你弄清楚你需要做什麼。

+0

$ ids包含鏈接id。營銷,汽車或體育。點擊時插入到查詢中。 – DonDraper 2010-07-26 20:42:44

1

您可以使用:

SELECT COUNT(*) as num FROM $tableName WHERE category IN ($id1, $id2, $id3) 

你也真的需要尋找到了mysql_real_escape_string()函數,所以有人不破壞你的數據庫。

+0

+1用於提示'mysql_real_escape_string()' – 2010-07-26 20:45:41