drcb19700 2012-11-01 15:34
浏览 43

如何使用MYSQL数据创建“排序依据”下拉列表

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. 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...

table category: 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!!

    //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.

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>
  • 写回答

1条回答 默认 最新

  • dongmei8071 2012-11-01 15:39
    关注

    Your php syntax is pretty bad:

    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.

    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. If $categoryID is an integer, the comparison will fail, because the types don't match - even though the values are the same.

    P.S. properly formatted (e.g. indented) code is your friend. Don't be afraid of tabs.

    评论

报告相同问题?

悬赏问题

  • ¥15 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)