我在mysql数据库中创建了一个事件列表,我希望用户能够使用下拉列表按位置进行过滤。我设法将下拉框排序,表格显示数据,但此刻选择一个选项无效。
在另一个问题上,我有多个相同值的位置,因此相同的位置在下拉菜单中多次出现...最好对其进行排序,但首先使其工作是优先级!
这是我的代码(我不确定要粘贴多少代码):
<?php require_once('Connections/united_hosting.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$maxRows_rs_blog = 3;
$pageNum_rs_blog = 0;
if (isset($_GET['pageNum_rs_blog'])) {
$pageNum_rs_blog = $_GET['pageNum_rs_blog'];
}
$startRow_rs_blog = $pageNum_rs_blog * $maxRows_rs_blog;
mysql_select_db($database_united_hosting, $united_hosting);
$query_rs_blog = "SELECT *, DATE_FORMAT(date, '%D %M %Y') AS format_date FROM blog_posts ORDER BY `date` DESC";
$query_limit_rs_blog = sprintf("%s LIMIT %d, %d", $query_rs_blog, $startRow_rs_blog, $maxRows_rs_blog);
$rs_blog = mysql_query($query_limit_rs_blog, $united_hosting) or die(mysql_error());
$row_rs_blog = mysql_fetch_assoc($rs_blog);
if (isset($_GET['totalRows_rs_blog'])) {
$totalRows_rs_blog = $_GET['totalRows_rs_blog'];
} else {
$all_rs_blog = mysql_query($query_rs_blog);
$totalRows_rs_blog = mysql_num_rows($all_rs_blog);
}
$totalPages_rs_blog = ceil($totalRows_rs_blog/$maxRows_rs_blog)-1;
$query_rs_blog = "SELECT *, DATE_FORMAT(date, '%D %M %Y')AS format_date FROM blog_posts ORDER BY `date` DESC";
$rs_blog = mysql_query($query_rs_blog, $united_hosting) or die(mysql_error());
$row_rs_blog = mysql_fetch_assoc($rs_blog);
$totalRows_rs_blog = mysql_num_rows($rs_blog);
mysql_select_db($database_united_hosting, $united_hosting);
$query_rs_news = "SELECT *, DATE_FORMAT(date_posted, '%D %M %Y')AS format_date FROM news_item ORDER BY date_posted DESC";
$rs_news = mysql_query($query_rs_news, $united_hosting) or die(mysql_error());
$row_rs_news = mysql_fetch_assoc($rs_news);
$totalRows_rs_news = mysql_num_rows($rs_news);
mysql_select_db($database_united_hosting, $united_hosting);
$query_rs_events = "SELECT * FROM events ORDER BY `date` ASC";
$rs_events = mysql_query($query_rs_events, $united_hosting) or die(mysql_error());
$row_rs_events = mysql_fetch_assoc($rs_events);
$totalRows_rs_events = mysql_num_rows($rs_events);
$colname_rs_location = "-1";
if (isset($_POST['location'])) {
$colname_rs_location = $_POST['location'];
}
mysql_select_db($database_united_hosting, $united_hosting);
$query_rs_location = sprintf("SELECT location FROM events WHERE location = %s ORDER BY `date` ASC", GetSQLValueString($colname_rs_location, "text"));
$rs_location = mysql_query($query_rs_location, $united_hosting) or die(mysql_error());
$row_rs_location = mysql_fetch_assoc($rs_location);
$totalRows_rs_location = mysql_num_rows($rs_location);
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post">
<p>
<label for="select">Select:</label>
<select name="select" id="select">
<?php
do {
?>
<option value="<?php echo $row_rs_events['location']?>"<?php if (!(strcmp($row_rs_events['location'], $_POST['']))) {echo "selected=\"selected\"";} ?>><?php echo $row_rs_events['location']?></option>
<?php
} while ($row_rs_events = mysql_fetch_assoc($rs_events));
$rows = mysql_num_rows($rs_events);
if($rows > 0) {
mysql_data_seek($rs_events, 0);
$row_rs_events = mysql_fetch_assoc($rs_events);
}
?>
</select>
<input type="submit" name="submit" id="submit" value="Submit">
</p>
<table width="600" border="1">
<tr>
<th scope="col">location</th>
<th scope="col">venue</th>
<th scope="col">date</th>
<th scope="col">tickets</th>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_rs_events['location']; ?></td>
<td><?php echo $row_rs_events['venue_name']; ?></td>
<td><?php echo $row_rs_events['date']; ?></td>
<td><?php echo $row_rs_events['tickets_remaining']; ?></td>
</tr>
<?php } while ($row_rs_events = mysql_fetch_assoc($rs_events)); ?>
</table>
<p> </p>
</form>
</body>
</html>
<?php
mysql_free_result($rs_blog);
mysql_free_result($rs_events);
mysql_free_result($rs_location);
?>
答案 0 :(得分:0)
SELECT DISTINCT location FROM events WHERE location = %s ORDER BY `date` ASC
DISTINCT 关键字可让您获得每个位置只有一个的结果集。
if (isset($_POST['location'])) {
$colname_rs_location = $_POST['location'];
}
确保将POST数据发送到此脚本的scipt具有一个名称为location的输入字段。目前,提供的代码中的表单使用名称select。