php
  • mysql
  • 2015-04-30 20 views 2 likes 
    2

    我有一個分頁腳本鏈接到一個MySQL查詢,我可以過濾。當結果未被過濾並且所有行都顯示時,我有下一個鏈接,指出「?pg = 2」等等(取決於多少結果等)。一旦我過濾了我想要顯示的內容,我的URL包含其他參數,如?certifications = VALUE & state = VALUE。獲取所有URL參數除了一個

    這些行處理我的分頁鏈接。

    echo "<a href='?pg=".($pg-1)."' class='paginationButton'>PREVIOUS</a> \n"; 
    
    echo "<a href='?pg=".($pg+1)."' class='paginationButton'>NEXT</a> \n"; 
    

    我向每個添加了$ _SERVER ['QUERY_STRING']以嘗試將url參數添加到鏈接。它的工作原理,但它也每次被點擊時(基本上如果下一個被點擊了兩次它呢?認證= VALUE &狀態= VALUE & PG = 3 & PG = 2。

    我怎樣才能使增加的?PG參數如果& pg已經設置,它不會在URL中複製它?

    ============================ ==================================================

    FULL代碼在這裏

    $start=0; 
    $limit=3; 
    
    if(isset($_GET['pg'])) 
    { 
    $pg=$_GET['pg']; 
    $start=($pg-1)*$limit; 
    } 
    else { 
    $pg = 1; 
    } 
    $sql = mysql_query($query); 
    
    $conditions = "SELECT * FROM pilotOperators WHERE 1=1"; 
    # append condition for signage (if required) 
    if(isset($_GET['signage'])) { 
    $conditions .= " AND signage='1'"; 
    } 
    # append condition for certifications (if required) 
        if(isset($_GET['certifications'])) { 
        $certifications = mysqli_real_escape_string($conn,$_GET['certifications']); 
        $conditions .= " AND certifications='$certifications'"; 
    } 
    # append condition for state (if required) 
    if(isset($_GET['state'])) { 
    if($_GET['state'] != "Select State") { 
        $state = mysqli_real_escape_string($conn,$_GET['state']); 
        $conditions .= " AND state='$state'"; 
    } 
    } 
    $conditions .= " Limit $start, $limit"; 
    $result = mysqli_query($conn, $conditions); 
    
    while($row = mysqli_fetch_array($result)) 
    { 
    echo "\n <table border='0' class='resultTable' width='75%'> \n"; 
    echo "<tr> \n"; 
    echo "<td width='120px'>ID: </td> \n"; 
    echo "<td>" . $row['id'] . "</td> \n"; 
    echo "</tr> \n"; 
    echo "<tr> \n"; 
    echo "<td>Name: </td> \n"; 
    echo "<td>" . $row['name'] . "</td> \n"; 
    echo "</tr> \n"; 
    echo "<tr> \n"; 
    echo "<td>Phone: </td> \n"; 
    echo "<td>" . $row['phone'] . "</td> \n"; 
    echo "</tr> \n"; 
    echo "<tr> \n"; 
    echo "<td>Alt. Phone: </td> \n"; 
    echo "<td>" . $row['alt_phone'] . "</td> \n"; 
    echo "</tr> \n"; 
    echo "<tr> \n"; 
    echo "<td>Fax: </td> \n"; 
    echo "<td>" . $row['fax'] . "</td> \n"; 
    echo "</tr> \n"; 
    echo "<tr> \n"; 
    echo "<td>Email: </td> \n"; 
    echo "<td>" . $row['email'] . "</td> \n"; 
    echo "</tr> \n"; 
    echo "<tr> \n"; 
    echo "<td>Website: </td> \n"; 
    echo "<td><a href='" . $row['website'] . "' target='_blank'>" . $row['website'] . "</a></td> \n"; 
    echo "</tr> \n"; 
    echo "<tr> \n"; 
    echo "<td>City: </td> \n"; 
    echo "<td>" . $row['city'] . "</td> \n"; 
    echo "</tr> \n"; 
    echo "<tr> \n"; 
    echo "<td>State: </td> \n"; 
    echo "<td>" . $row['state'] . "</td> \n"; 
    echo "</tr> \n"; 
    echo "<tr> \n"; 
    echo "<td>Certifications: </td> \n"; 
    echo "<td>" . $row['certifications'] . "</td> \n"; 
    echo "</tr> \n"; 
    echo "<tr> \n"; 
    echo "<td>Top Sign: </td> \n"; 
    echo "<td>"; 
    if($row['signage'] = 1) { 
    echo "Has Top Sign"; 
    } 
    else { 
    echo "Top Sign Not Listed"; 
    } 
    echo "</td> \n"; 
    echo "</tr> \n"; 
    echo "</table> \n\n"; 
    } 
    
    $countconditions = "SELECT * FROM pilotOperators WHERE 1=1"; 
    # append condition for signage (if required) 
    if(isset($_GET['signage'])) { 
        $countconditions .= " AND signage='1'"; 
    } 
    # append condition for certifications (if required) 
    if(isset($_GET['certifications'])) { 
        $certifications = mysqli_real_escape_string($conn,$_GET['certifications']); 
        $countconditions .= " AND certifications='$certifications'"; 
    } 
    # append condition for state (if required) 
    if(isset($_GET['state'])) { 
    if($_GET['state'] != "Select State") { 
        $state = mysqli_real_escape_string($conn,$_GET['state']); 
        $countconditions .= " AND state='$state'"; 
    } 
    } 
    $rows = mysqli_num_rows(mysqli_query($conn, $countconditions)); 
    
    $total=ceil($rows/$limit); 
    echo "<div id='paginationLinks'> \n"; 
    if($pg>1) 
    { 
    echo "<a href='?pg=".($pg-1)."&".$_SERVER['QUERY_STRING']."' class='paginationButton'>PREVIOUS</a> \n"; 
    } 
    if($pg!=$total) 
    { 
    echo "<a href='?pg=".($pg+1)."&".$_SERVER['QUERY_STRING']."' class='paginationButton'>NEXT</a> \n"; 
    } 
    
    echo "<ul class='page'> \n"; 
    for($i=1;$i<=$total;$i++) 
    { 
    if($i==$pg) { echo "<li class='current'>".$i."</li> \n"; } 
    
    else { echo "<li><a href='?id=".$i."'>".$i."</a></li> \n"; } 
    } 
    echo "</ul> \n"; 
    echo "</div> \n"; 
    mysqli_close($con); 
    
    +0

    看看'parse_str'它是一個PHP函數,用於將querystrings解析爲關聯數組。當你有這個時,你可以簡單地替換數組中的pg條目,然後使用'http_build_query'將其字符串化回查詢字符串。看看: https://php.net/parse_str – EJTH

    回答

    4
    $q = http_build_query(array_merge($_GET, ["pg" => $pg+1])); 
    
    <a href="?$q">..</a> 
    
    +0

    簡單的解決方案,像魅力一樣工作。謝謝! –

    0

    嘗試更換你:喜歡的東西

    href='?pg=".($pg+1)."&".$_SERVER['QUERY_STRING']."' 
    

    $allParams = $_GET; 
    $allParams['pg'] = $pg+1; 
    $queryString = '?'; 
    foreach ($allParams as $param=>$param_val) { 
        if ($queryString != '?') $queryString .= '&'; 
        $queryString .= $param.'='.urlencode($param_val); 
    } 
    ... 
    echo "<a href='$queryString' class=... 
    
    +0

    剛剛嘗試過。當我點擊鏈接的URL參數變成這樣:?pg =?pg = 1 –

    +0

    堅持我的錯誤讓我嘗試別的。我留下了一些不應該是的網址:) –

    0

    您可以從您的查詢字符串刪除PG,然後再更換。如果它不在那裏,你不會刪除它,但仍然可以取代它。

    preg_replace('/(^|&)pg=[0-9]+&/','\1',$_SERVER['QUERY_STRING'].'&').'pg='.($pg+1) 
    

    這樣做是:

    1. 貼&在查詢字符串的結尾結束 - 我們需要這一點。
    2. 查找PG = ###以下字符串的開頭或&
    3. 刪除在PG = $ PG + 1查詢字符串的結束時,PG = ###
    4. 粘性。

    爲什麼要檢查&之前和之後?我不想無意中替換一個像'apg = 54'的變量

    相關問題