我有以下下拉框,它使用PHP查询来获取它的数据。它创建了一个包含3个条目的下拉列表:CARS,TRUCKS,TRAINS。选择一个选项后,它会执行GET
并使用所选类别的PHP更新。我添加了一个带有文本VIEW BY CATEGORY的静态OPTION。如果我选择一个类别,然后回到VIEW BY CATEGORY,我希望所有结果(CARS,TRUCKS和TRAINS)再次显示。
<form name="form1" action="" method="GET">
<select name="category" onchange="this.form.submit();">
<option value="">View By Category...</option>
<?php while ($rows = mysql_fetch_array($query_category)) { ?>
<?php if(isset($_GET['category'])) { ?>
<option value="<?php echo $rows['category']; ?>" <?php echo $rows['category'] == $category ? 'selected' : '' ?> ><?php echo $rows['category'] ?></option>
<?php } else {?>
<option value="<?php echo $rows['category']; ?>"><?php echo $rows['category'] ?></option>
<?php } ?>
<?php } ?>
</select>
</form>
*示例 - 当我选择选项CARS时,我的网址将为http://localhost/edit.php?category=Cars
。当我选择VIEW BY CATEGORY时,它变为http://localhost/edit.php?category=
。然后我的所有结果都消失了。而不是消失,我想要显示所有结果。
<?php
//Establishing Connection with Server
$connection = mysql_connect("localhost", "em", "em");
//Selecting Database
$db = mysql_select_db("em", $connection);
//This checks if variable defined
if(isset($_GET['category'])) {
//If it is, will run query with variable
$category = $_GET['category'];
$query = mysql_query("SELECT * FROM tblClients WHERE tblclients.package = 'standard' AND tblclients.category = '$category' ", $connection);
} else {
//If NOT, will run query without variable
$query = mysql_query("SELECT * FROM tblClients WHERE tblclients.package = 'standard' ", $connection);
}
//Other Queries
$query_featured = mysql_query("SELECT * FROM tblClients WHERE tblclients.package = 'featured'", $connection);
$query_category = mysql_query("SELECT * FROM tblCategory", $connection);
?>
答案 0 :(得分:3)
有人认为我发现的是未申报的$ category变量,我认为它是$ _GET [&#39; category&#39;] 您可能希望在此处更好地使用用户输入。
要在用户选择VIEW BY CATEGORY
并因此导致网址变为http://localhost/edit.php?category=
时显示记录,您应该根据$_GET['category
是否为空来设置选择记录的测试。
即:
<?php
if( $_SERVER['REQUEST_METHOD']=='GET' ){
if( isset( $_GET['category'] ) ) {
if( !empty( $_GET['category'] ) ){
/* sql: query using known category from url */
} else {
/* sql: query to get all records as no category defined */
}
}
}
?>
<form name='form1' action='' method='GET'>
<select name='category' onchange='this.form.submit();'>
<option value=''>View By Category...</option>
<?php
while ( $rows = mysql_fetch_array( $query_category ) ) {
if( isset( $_GET['category'] ) ) {
/* There appeared to be an undeclared variable $category here */
$category=trim( $_GET['category'] );
$selected=$rows['category'] === $category ? 'selected' : '';
echo "<option value='{$rows['category']}' {$selected}>{$rows['category']}</option>";
} else {
echo "<option value='{$rows['category']}'>{$rows['category']}</option>";
}
}
?>
</select>
</form>
更新:自从发布db逻辑和sql以来,上面的一些内容可以简化为
<?php
//Establish Connection with Server
$connection = mysql_connect("localhost", "em", "em");
$db = mysql_select_db( "em", $connection );
/* If category is defined in url, filter and assign as a variable - otherwise it is false */
$category = isset( $_GET['category'] ) && !empty( $_GET['category'] ) ? strip_tags( filter_input( INPUT_GET, 'category', FILTER_SANITIZE_STRING ) ) : false;
$query_featured = mysql_query("SELECT * FROM `tblClients` c WHERE c.`package` = 'featured'", $connection);
$query_category = mysql_query("SELECT * FROM `tblCategory`", $connection);
if( $_SERVER['REQUEST_METHOD']=='GET' ){
if( $category ){
/* sql: query using known category from url */
$sql="SELECT * FROM `tblClients` c WHERE c.`package`='standard' AND c.`category` = '$category'";
} else {
/* sql: query to get all records as no category defined */
$sql="SELECT * FROM `tblClients` c WHERE c.`package`='standard'";
}
$query=mysql_query( $sql );
}
?>
应该注意@Objective_d - 你应该使用mysqli或PDO,因为它们通过利用预处理语句提供了更大的保护,免受可怕的SQL注入。祝你好运 - 圣诞快乐
答案 1 :(得分:1)
试试这个:
//This checks if variable defined
if(isset($_GET['category'])&&$_GET['category']!='')
问题是GET变量已设置,因此条件的计算结果为true。您需要考虑要设置的变量但是等于没有(它是)。
按照建议执行此操作意味着您将恢复运行第二个查询而不是运行第一个查询,并接收整个项目列表。
答案 2 :(得分:0)
@if (true){<span>true text from partial</span>}
使用if条件匹配类别,如果匹配,则将属性<select name="category" onchange="this.form.submit();">
<option value="">View By Category...</option>
<?php while ($rows = mysql_fetch_array($query_category)) { ?>
<option value="<?php echo $rows['category']; ?>" <?php if($_GET['category'] == $rows['category']) { echo "selected='selected'"; } ?> ><?php echo $rows['category'] ?></option>
<?php } ?>
</select>
添加到selected='selected'
代码中。