[英]How to make a “Sort By” dropdown list with MYSQL data
Hey guys I've been stuck on this for a while now and really need help with this. 大家好,我已经坚持了一段时间,确实需要帮助。 It is to the point where I`m sure it is easy to do but I'm just not seeing it the right way.
我确信这很容易做到,但我只是没有以正确的方式看到它。
What I have is a page called "company_list.php" and on this page I am displaying all companies in the company table. 我所拥有的是一个名为“ company_list.php”的页面,在此页面上,我在公司表中显示所有公司。 What I want to do is have a drop down selection form to sort my results by.
我想要做的是有一个下拉选择表单,用于对我的结果进行排序。 At the moment I am just trying to sort by category.
目前,我只是试图按类别进行排序。
table company: companyID categoryID companyName etc... 表公司:companyID categoryID companyName等...
table category: categoryID categoryName 表类别:categoryID categoryName
**FORM**
<form action="company_list.php" method="POST" >
<label>Category: </label>
<select name="categoryID">
<?php foreach ($categorys as $category) : ?>
<option value="<?php echo $category['categoryID']; ?>">
<?php echo $category['categoryName']; ?>
</option>
<?php endforeach; ?>
</select>
<input type="submit" value="Sort" />
PHP This is where I am stuck and have tried many different things PLEASE HELP!! PHP这是我被困住的地方,尝试了许多不同的事情,请帮助!!
//include db here
//global
global $db;
if(isset($_POST['categoryID']) === " '.$categoryID.' ") {
$query='SELECT *
FROM company
WHERE categoryID ="'.$categoryID.'"
$db->query($query);
}
//get data
//get all cats
$query='SELECT * FROM category
ORDER BY categoryID';
$categorys=$db->query($query);
//Get all COMPANYS for cat
$query='SELECT * FROM company WHERE categoryID = "'.$categoryID.'"
ORDER BY companyName ASC';
$category_companys=$db->query($query);
I`ve hit a wall here and would really love some help. 我在这里碰壁,真的很想帮忙。 Thanks in advance.
提前致谢。
I was able to accomplish something similar to this but not in a drop down list. 我能够完成与此类似的操作,但未在下拉列表中完成。 Below is what I have done but idk how to get it into a drop down.
以下是我已经完成的工作,但idk如何将其放入下拉菜单。
PHP 的PHP
global $db;
if(!isset($categoryID)) {
@$categoryID = $_GET['categoryID'];
if(!isset($categoryID)){
$categoryID=2; //I want to start by displaying companys for all categorys...not at categoryID=2
}
}
//get name of all categorys
$query='SELECT * FROM category
WHERE categoryID = "'.$categoryID.'"';
$category_results=$db->query($query);
$category_row = $category_results->fetch();
$categoryName = $category_row['categoryName'];
//get all cats
$query='SELECT * FROM category
ORDER BY categoryID';
$categorys=$db->query($query);
//Get all COMPANYS for cat
$query='SELECT * FROM company WHERE categoryID = "'.$categoryID.'"
ORDER BY companyName ASC';
$category_companys=$db->query($query);
HTML 的HTML
<!--lists all category`s in <li> not a drop down form -->
<?php foreach($categorys as $category) : ?>
<li>
<a href="company_list.php?categoryID=<?php echo $category['categoryID']; ?>" >
<?php echo $category['categoryName']; ?>
</a>
</li>
<?php endforeach; ?>
<!--Result Set-->
<table border="1px solid black" cellspacing="1px" cellpadding="2px;" style="margin-top:5px;">
<!--<h1>Company Name</h1>-->
<th>Company Name</th>
<th>Address</th>
<th>Phone</th>
<th>Email</th>
<?php foreach($category_companys as $company) : ?>
<tr>
<td><a href="company_page.php?company_id=<?php echo $company['companyID']; ?>">
<?php echo $company['companyName']; ?></a>
</td>
<td>
<?php echo $company['companyAddress']; ?>
</td>
<td>
<?php echo $company['companyPhone']; ?>
</td>
<td>
<?php echo $company['companyEmail']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
Your php syntax is pretty bad: 您的php语法非常糟糕:
if(isset($_POST['categoryID']) === " '.$categoryID.' ") {
^^^^^^^^^^^^^^^^^^
is trying to compare the posted value against a string that has a space, a single quote, a period, the value of the $categoryID variable, another period, another quote, another space. 正在尝试将发布的值与具有空格,单引号,句点,$ categoryID变量的值,另一个句点,另一个引号,另一个空格的字符串进行比较。
did you perhaps want: 您是否想要:
if(isset($_POST['categoryID']) === $categoryID) {
instead? 代替? Note that this will most likely STILL fail.
请注意,这很可能仍会失败。 Anything that comes out of the $_GET/$_POST/$_REQUEST superglobals is a STRING .
$ _GET / $ _ POST / $ _ REQUEST超全局变量中出现的任何内容均为STRING 。 If $categoryID is an integer, the comparison will fail, because the types don't match - even though the values are the same.
如果$ categoryID是整数,则比较将失败,因为类型不匹配-即使值相同。
PS properly formatted (eg indented) code is your friend. PS格式正确(例如,缩进)的PS是您的朋友。 Don't be afraid of tabs.
不要害怕标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.