如何使用php / mysql显示/过滤搜索结果

时间:2013-01-15 16:04:54

标签: php mysql database search

我对使用php / mysql编码非常陌生,我在显示链接到sql数据库的结果方面遇到了一些问题。

目前人们使用以下代码进行搜索 - 工作正常 - 这是一个下拉选择框,但您只能选择一个选项。我想要它所以项目是复选框,你可以选择多个项目。

我在下面列出了当人们将数据输入数据库时​​使用的代码 - 这是复选框 - 我尝试用这个代替'下拉选择代码',但它不起作用。

是否有人知道我必须使用哪些代码来替换“下拉选择代码”以便复选框可见并且您可以过滤多个项目 - 我还包含了“结果页面代码”,其中显示了结果我认为'ClientStage'需要在某处添加'复选框代码'。

很抱歉我对此缺乏了解并会感激一些帮助吗?


DROP DOWN SELECT CODE


<select name="ClientStage" id="ClientStage">
            <option value=""></option>
            <?php
        include 'Easyspace.php';
        $sql = 'SELECT * FROM `clienttype`;';
        $rs = mysql_query($sql, $conn) or die ("error with sql query ".$sql);

        while ($row = mysql_fetch_array ($rs)){
            $ClienttypeID = $row["ClienttypeID"];
            $Clienttype = $row["Clienttype"];

            echo '<option value="' .$ClienttypeID. '">' .$Clienttype. '</option>'; 
        }
        ?>
          </select></span>

检查BOX代码


<table width="100%" border="0" cellspacing="1">
    <?php
    $side=1;  
    $sql = 'SELECT * FROM `clienttype`;';
    $rs = mysql_query($sql, $conn) or die ("error with sql query ".$sql);

    while ($row = mysql_fetch_array ($rs)){
        $ClienttypeID = $row["ClienttypeID"];
        $Clienttype = $row["Clienttype"];

        if ($side == 1){
            $side = 2;
            echo '<tr>';
            echo '<td><span class="RPATtext"><input type="checkbox" name="Clients[]" value="' .$ClienttypeID. '"> ' .$Clienttype. '</div></td>';

        } else {
            $side = 1;
            echo '<td><span class="RPATtext"><input type="checkbox" name="Clients[]" value="' .$ClienttypeID. '"> ' .$Clienttype. '</div></td>';
            echo '<option value="' .$ClienttypeID. '">' .$Clienttype. '</option>'; 
        }

    }
    ?>
    </table>

结果页面代码


 <?php

    $Country = $_POST['Country'];
    $County = $_POST['County'];
    $ClientStage = $_POST['ClientStage'];
    $HealthIssues = $_POST['HealthIssues'];

    include 'Easyspace.php';
    $sql = "SELECT * FROM `therapists` WHERE ";

    if ($Country){
        $sql .= "`Country` = '$Country'";
    }

    if ($County){
        if ($Country){
            $sql .= ' AND ';    
        }
        $sql .= "`County` = '$County'";
    }

    if ($ClientStage){
        if ($Country or $County){
            $sql .= ' AND ';    
        }
        $sql .= "FIND_IN_SET('$ClientStage', Client_typeID)";
    }

    if ($HealthIssues){
        if ($Country or $County or $ClientStage){
            $sql .= ' AND ';    
        }
        $sql .= "FIND_IN_SET('$HealthIssues', IssuesID)";
    }
    // echo $sql;

    $rs = mysql_query($sql, $conn) or die ("error with sql query ".$sql);

    while ($row = mysql_fetch_array ($rs)){
        $TherapistID = $row['TherapistID'];
        $Title = $row['Title'];
        $FirstName = $row['First Name'];
        $LastName = $row['Last Name'];
        $PostCode = $row['PostCode'];

        echo '<tr>';
        echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$Title. '</td>';
        echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$FirstName. '</td>';
        echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$LastName. '</td>';
        echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$PostCode. '</td>';
        echo '<td height="34" valign="middle" bgcolor="#D9E5C3"><p class="bodycopy"><a href="Therapist.php?ID=' .$TherapistID. '">View more details</a></p></td>';
        echo '</tr>';

    }

    ?>

1 个答案:

答案 0 :(得分:0)

这是您的新结果代码。我希望这就是你要找的东西。注意你的代码:尝试将vars换成小写($ Country必须是$ country,尝试使用PDO for MySQL等)。

<?php

$Country = $_POST['Country'];
$County = $_POST['County'];
/* change here */
$ClientStage = $_POST['Clients'];
$HealthIssues = $_POST['HealthIssues'];

include 'Easyspace.php';
$sql = "SELECT * FROM `therapists` WHERE ";

if ($Country){
    $sql .= "`Country` = '$Country'";
}

if ($County){
    if ($Country){
        $sql .= ' AND ';    
    }
    $sql .= "`County` = '$County'";
}

if ($ClientStage){
    if ($Country or $County){
        $sql .= ' AND ';    
    }
    /* change here */
    $sql .= 'Client_typeID IN (';
    foreach($ClientStage as $i => $id) {
       if ($i !== 0)
           $sql .=  ',';
       $sql .=  "'" . $id . '"';
    }
    $sql .= ')';
}

if ($HealthIssues){
    if ($Country or $County or $ClientStage){
        $sql .= ' AND ';    
    }
    $sql .= "FIND_IN_SET('$HealthIssues', IssuesID)";
}
// echo $sql;

$rs = mysql_query($sql, $conn) or die ("error with sql query ".$sql);

while ($row = mysql_fetch_array ($rs)){
    $TherapistID = $row['TherapistID'];
    $Title = $row['Title'];
    $FirstName = $row['First Name'];
    $LastName = $row['Last Name'];
    $PostCode = $row['PostCode'];

    echo '<tr>';
    echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$Title. '</td>';
    echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$FirstName. '</td>';
    echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$LastName. '</td>';
    echo '<td valign="middle" bgcolor="#D9E5C3" class="bodycopy">' .$PostCode. '</td>';
    echo '<td height="34" valign="middle" bgcolor="#D9E5C3"><p class="bodycopy"><a href="Therapist.php?ID=' .$TherapistID. '">View more details</a></p></td>';
    echo '</tr>';

}

?>