0

我有一个创建图像链接列表的查询,我想通过这些图像链接的 url 传递一个变量。但是,因为有多个结果,所以当我尝试传递它时,我得到的查询乘以有多少结果。

例如,如果 $filter = "sub 1, sub 2" 并且返回三张图片,那么passed_filter = "sub1, sub2, sub1, sub2, sub1, sub2"

所以我要做的是passed_filter =“sub1,sub2”。

$filter 从多选下拉列表中传递。而passed_filter 是$filter 的结果被发送回结果页面。

关于如何更改下面的代码来做我想做的任何想法?

php页面:

<?php
echo "<h2>Currently Displaying: Custom Selection</h2>";

    include ("connect.php");

    $filter = $_GET["filter"];
    $filterIn = $filter;

        $result = mysql_query("SELECT * FROM edt_images
                                WHERE cat1 IN ($filterIn)
                                OR cat2 IN ($filterIn)
                                OR cat3 IN ($filterIn)
                                OR cat4 IN ($filterIn)
                                OR cat5 IN ($filterIn)
                                OR cat6 IN ($filterIn)
                                ORDER BY vote_item_id")                         
            or die(mysql_error());



        echo "<div id='results_container'>";
        echo "<ul id='items'>";

        while ($row = mysql_fetch_array($result)) {

                echo "<li><a href='subpage.php?passed_filter=".$filter."&id=".$row['id']."' border='0'>
                         <img src='files/300x200/thumb2_".$row['item_name'].".".$row['file_extension']."' border='0' class='filtered_images'/>
                         </a>
                            <br />
                                <strong>
                                    ".$row['file_tag_line']." 
                                </strong>
                      </li>"; 
        }

        echo "</ul>";
        echo "</div>";
?>

提前致谢。请让我知道是否还有其他需要发布的内容。

4

1 回答 1

1

1) 停止使用mysql_函数

2) 标准化

CREATE TABLE edit_images_category (
  id tinyint,
  category varchar(10),
  PRIMARY KEY (id, category));

INSERT INTO edit_image_category (id, category)
SELECT id, cat1 AS cat FROM edit_images
UNION ALL
SELECT id, cat2 AS cat FROM edit_images
UNION ALL
SELECT id, cat3 AS cat FROM edit_images
UNION ALL
SELECT id, cat4 AS cat FROM edit_images
UNION ALL
SELECT id, cat5 AS cat FROM edit_images
UNION ALL
SELECT id, cat6 AS cat FROM edit_images

3) 进行搜索

SELECT * FROM edit_images_category WHERE cat IN ($filterIn)

查看演示

于 2013-02-08T20:17:26.640 回答