2014-05-05 284 views
0

我已經創建了一個mysql數據庫中的事件列表,我希望用戶能夠使用下拉列表按位置進行過濾。我已經設法將下拉框排序,並且該表顯示數據,但是此刻選擇一個選擇什麼都不做。下拉選擇列表從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>&nbsp;</p> 
</form> 
</body> 
</html> 
<?php 
mysql_free_result($rs_blog); 

mysql_free_result($rs_events); 

mysql_free_result($rs_location); 
?> 
+0

「我有多個具有相同值的位置,因此相同的位置在下拉菜單中多次出現」,請在SQL查詢中的SELECT後面添加DISTINCT以檢索位置。 –

+0

關於選擇不做任何事,你是什麼意思?這是否意味着當您從保管箱中選擇某些內容時,它不會顯示您點擊的內容?如果是這種情況,請通過右鍵單擊Dropbox然後檢查元素來檢查您的html語法,或者如果不可用,請查看源代碼並查看dropbox html是否正確 –

回答

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具有名稱爲位置的輸入字段。目前提供的代碼中的表單使用名稱select。

+0

感謝您的回覆。似乎無法得到DISTINCT位工作,我確定我記得以前使用過,所以會再次檢查。這是正確的重命名錶格?... user3606041