0

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

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

我在下面包含了人们将数据输入到数据库时使用的代码——它们是复选框——我尝试用它替换“下拉选择代码”,但它不起作用。

有谁知道我必须使用什么代码来替换“下拉选择代码”,以便可以查看复选框并且您可以过滤多个项目 - 我还包括了“结果页面代码”,它显示了结果和我我认为“ClientStage”需要在某处添加“复选框代码”。

很抱歉我缺乏这方面的知识,并会感谢一些帮助?


下拉选择代码


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

复选框代码


<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>';

    }

    ?>
4

1 回答 1

1

这是您的新结果代码。我希望这是你要找的。注意您的代码:尝试将 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>';

}

?>
于 2013-01-15T16:20:32.397 回答