2016-02-05 31 views
1

我有兩個下拉菜單,第二個下拉菜單由第一個下拉條件填充。現在,我想要同時使用兩個下拉條件並將其應用於查詢,並通過HTML將結果顯示在表中。基於兩個下拉菜單運行查詢並顯示錶格PHP

這是我的代碼,

腳本代碼:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/jquery-ui.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
    $('.country').on('change', function() { 
    // Code to add country information in url 

    location.href = location.href.split('?')[0] 
     + ['?country', $(this).val()].join('='); 
    }); 

}); 

</script> 

PHP代碼的第一個下拉列表:

<?php 
$countries = mysqli_query($mysqli,"select source from nhws.masterkey group by source;"); 
echo "<select class='country' name='country' style='width:200px'>"; 
echo "<option size =30 ></option>"; 
while($row = mysqli_fetch_array($countries)){   
    echo "<option value='".$row['source']."'>".$row['source']."</option>"; 
} 
echo "</select>"; 
if (isset($_GET['country'])) { 
    $country = $_GET['country']; 
    echo "<p></p>"; 
    echo "$country is the selected data set"; 
    echo "<p></p>"; 
} 
?> 

PHP代碼第二個下拉:

<?php 
if (isset($_GET['country'])) { 
    echo "<h5>Choose variable</h5>"; 
    $variables = mysqli_query($mysqli,"select variable from nhws.num_all_{$country} group by variable;"); 
    echo "<select class='variable' name='variable' style=width:200px>"; 
    echo "<option size =30 ></option>"; 
    while($row = mysqli_fetch_array($variables)) {   
     echo "<option value='".$row['variable']."'>".$row['variable']."</option>"; 
    } 
    echo "</select>"; 
} 
?> 

現在,最後一個PHP代碼顯示結果表:

<?php 
if (isset($_GET['variable'])) { 
    $results = mysqli_query($mysqli,"select q1.variable, q1.numvalue, description, num_cases 
    from (select variable, numvalue, count(variable) as num_cases 
    from nhws.num_all_{$country} 
    where variable = '{$variable}' 
    group by variable, numvalue) q1 
    inner join (select * from nhws.luvalues where source = '{$country}' and variable = '{$variable}') 
    t2 on q1.numvalue=t2.numvalue;"); 
    echo "<h5>Counts</h5>"; 
    if ($results->num_rows > 0) { 
     echo "<table><tr><th>Variable</th><th>Numvalue</th><th>Description</th><th>Num Cases</th></tr>"; 
     // output data of each row 
     while($row = $results->fetch_assoc()) { 
       echo "<tr><td>" . $row["variable"]. "</td><td>" . $row["numvalue"]. "</td><td>" . $row["description"]. "</td><td>" . $row["num_cases"]. "</td></tr>"; 
     } 
     echo "</table>"; 
    } else {echo "0 results";} 
} 
?> 

一旦選擇了所有的下拉選項,什麼都不會發生。查詢甚至沒有運行。看起來它並沒有通過if (isset($_GET['variable']))聲明。

如何使它工作?

謝謝!

+0

你從來沒有設置'$ variable'或'$ country',所以它們是null,你在做'where variable =''' - 你可能正在讀一個愚蠢的/老的教程,它假設'register_globals '仍然存在......並且請注意,你幾乎肯定容易受到[sql注入攻擊](http://bobby-tables.com) –

+0

我沒有看到第二次下拉列表中有任何onChange事件。所以你第一次提交加載第二個,但第二個從未提交加載結果。 – Phil

+0

那麼'$ country'就會被設置,因爲第二個下拉菜單是基於第一個下拉菜單正確填充的。所以,我使用'$ variable'的相同方法。我嘗試創建第二個下拉列表的onChange事件,但我沒有成功。你將如何進行第二次onChange事件? – user3882752

回答

1
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/jquery-ui.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
    $('.country').on('change', function() { 
    // Code to add country information in url 

    location.href = location.href.split('?')[0] 
     + ['?country', $(this).val()].join('='); 
    }); 

    $('.variable').on('change', function() { 
    // Code to add country information in url 

    location.href = location.href.split('?')[0] 
     + ['?variable', $(this).val()].join('='); 
    }); 

}); 

</script> 

PHP代碼第一下拉:

<?php 
$countries = mysqli_query($mysqli,"select source from nhws.masterkey group by source;"); 
echo "<select class='country' name='country' style='width:200px'>"; 
echo "<option size =30 ></option>"; 
while($row = mysqli_fetch_array($countries)){   
    echo "<option value='".$row['source']."''>".$row['source']."</option>"; 
} 
echo "</select>"; 
if (isset($_GET['country'])) { 
    $_SESSION['country'] = $_GET['country']; 
    echo "<p></p>"; 
    echo $_SESSION['country'].' is the selected data set'; 
    echo "<p></p>"; 
} 
?> 

PHP代碼第二下拉:

<?php 
if (isset($_GET['country'])) { 
    echo "<h5>Choose variable</h5>"; 
    $variables = mysqli_query($mysqli,"select variable from nhws.num_all_{$country} group by variable;"); 
    echo "<select class='variable' name='variable' style=width:200px>"; 
    echo "<option size =30 ></option>"; 
    while($row = mysqli_fetch_array($variables)) {   
     echo "<option value='".$row['variable']."'>".$row['variable']."</option>"; 
    } 
    echo "</select>"; 
} 
?> 

現在,最後PHP代碼來顯示結果表:

<?php 
if (isset($_GET['variable'])) { 
    $_SESSION['variable'] = $_GET['variable']; 
    $results = mysqli_query($mysqli,"select q1.variable, q1.numvalue, description, num_cases 
    from (select variable, numvalue, count(variable) as num_cases 
    from nhws.num_all_{$country} 
    where variable = '{$variable}' 
    group by variable, numvalue) q1 
    inner join (select * from nhws.luvalues where source = '{$_SESSION['country']}' and variable = '{$_SESSION['variable']}') 
    t2 on q1.numvalue=t2.numvalue;"); 
    echo "<h5>Counts</h5>"; 
    if ($results->num_rows > 0) { 
     echo "<table><tr><th>Variable</th><th>Numvalue</th><th>Description</th><th>Num Cases</th></tr>"; 
     // output data of each row 
     while($row = $results->fetch_assoc()) { 
       echo "<tr><td>" . $row["variable"]. "</td><td>" . $row["numvalue"]. "</td><td>" . $row["description"]. "</td><td>" . $row["num_cases"]. "</td></tr>"; 
     } 
     echo "</table>"; 
    } else {echo "0 results";} 
} 
?> 

僅供參考...您不需要$(文檔).read y,on()方法仍然會附加只是在頁面上已經存在的選項之後放置javascript代碼。

+0

我這樣做就像你的答案,但從第二個下拉列表中選擇變量後。第三個PHP部分無法識別'$ country'。它顯示'$ country'爲未定義的變量。 – user3882752

+0

@ user3882752這是因爲如果你將它存儲在$ country中,它是未定義的。 PHP變量只在頁面的持續時間內設置。當您重新提交頁面時,將重新創建變量。這就是爲什麼你應該將它存儲在SESSION變量中。 $ _SESSION [['country']代替。至少在我看來。否則,你使用global關鍵字聲明你的變量。像全球$國家=任何...應該持續跨負荷我相信... – Phil

+0

全局變量和SESSION變量不起作用。它看起來像SCRIPT是問題。因爲一旦我點擊第二個下拉菜單,它就會進入onChange函數並關閉「$ country」。我的腳本與您的腳本相同,位於頁面頂部。有什麼建議麼? – user3882752