我是MySQL&的新手想按区域“过滤”结果集.我之前曾问过类似的问题,但我不知道我是想创建一个新问题还是继续那个问题,对不起,如果我错了.
我看过http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_database&认为这将是理想的,但我不知道如何使它工作,或我如何让它“更新”结果.
理想情况下,我认为该地区下方的下拉框看起来很整洁 – 但是,我没有取得任何成功 – 我真的完全受限于我的理解,任何人都可以帮忙吗? (谢谢以前帮助过第一部分的人!!)
这就是我到目前为止所做的一切,(以便了解我想要过滤的内容).
非常感谢scotia – 下面是regionBox.PHP文件
…
<script type="text/javascript">
function selectRegion(str)
{
var xmlhttp;
if (str=="")
{
document.getElementById("region").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("region").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","regionBox.PHP?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<P>
<table class="dbTable">
<tr>
<tr><th>commodity</th> <th><form action="regionBox.PHP">
<select name="region" onchange="selectRegion(this.value)">
<option value="">Select Region</option>
<option value="E. Midlands">E. Midlands</option>
<option value="Gtr. London">Gtr. London</option>
<option value="north East">north East</option>
<option value="north West">north West</option>
<option value="Scotland">Scotland</option>
<option value="South East">South East</option>
<option value="South West">South West</option>
<option value="W. Midlands">W. Midlands</option>
<option value="Wales">Wales</option>
</select>
</form></th> <th>Member</th> <th>Size</th> <th>Price</th> <th>Date Posted</th>
</tr>
<?PHP
$link = MysqL_connect('localhost', '', '');
$db_selected = MysqL_select_db('palegall_newTrader', $link);
if (!$db_selected) {
die ('cant find newTrader' . MysqL_error());
}
$region = MysqL_real_escape_string($_POST['region']);
$query = MysqL_query("SELECT * FROM `sell` WHERE `commodity` = 'paper' ORDER BY `price`")
or die( MysqL_error() );
echo '<table class="dbTable">';
while ($row = MysqL_fetch_assoc($query))
{
echo '<tr><td>'.$row['commodity'].'</td> <td>'.$row['region'].'</td> <td>'.
$row['member'].'</td> <td>'.$row['size'].'</td> <td>'.
$row['price'].'</td> <td>'.$row['posted'].'</td> </tr>';
}
echo "</table>";
?>
</body></html>
我已经剥离了一些东西.我希望这没关系.
解决方法:
更改此代码
$query = MysqL_query("SELECT * FROM `sell` WHERE `commodity`='Paper'
ORDER BY `price`")
or die( MysqL_error() );
$row=MysqL_fetch_assoc($query);
do
{
echo'<table class="dbTable">';
echo '<tr><td>'.$row['commodity'].'</td> <td>'.$row['region'].'</td> <td>'.
$row['member'].'</td> <td>'.$row['size'].'</td> <td>'.
$row['price'].'</td> <td>'.$row['posted'].'</td> </tr>';
}
while($row = MysqL_fetch_assoc($query));
echo "</table>"; ?>
成:
$region = MysqL_real_escape_string($_POST['region_Name']);
//For debugging:
echo $region
$query = MysqL_query("SELECT * FROM sell WHERE commodity = 'paper'
AND region = '$region' ORDER BY price")
or die( MysqL_error() );
echo '<table class="dbTable">';
//echo the rows in a while loop
while ($row = MysqL_fetch_assoc($query))
{
echo '<tr><td>'.$row['commodity'].'</td> <td>'.$row['region'].'</td> <td>'.
$row['member'].'</td> <td>'.$row['size'].'</td> <td>'.
$row['price'].'</td> <td>'.$row['posted'].'</td> </tr>';
}
echo "</table>";
?>