嘿伙计们,我已经坚持了一段时间了,真的需要帮助。到了我确信这很容易做到的地步,但我只是没有以正确的方式看待它。
我拥有的是一个名为“company_list.php”的页面,在此页面上,我显示了公司表中的所有公司。我想要做的是有一个下拉选择表单来对我的结果进行排序。目前我只是想按类别排序。
表公司:公司ID类别ID公司名称等...
表类别: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这是我卡住的地方,尝试了很多不同的东西,请帮助!
//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);
我在这里碰壁了,真的很想得到一些帮助。提前致谢。
我能够完成与此类似的事情,但不在下拉列表中。以下是我所做的,但不知道如何将其放入下拉列表中。
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
<!--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>