我有两个下拉表单,从数据库中提取数据,一个用于服务,另一个用于城镇。该代码使用一个关键字$ k,但我不确定如何搜索城镇($ t)和服务($ k)。
<form action="search.php" method="get" style="margin:0 auto; text-align:center;" id="the-basics" onsubmit=submitForm(this)>
<select id="cd" name="k" class="search-left rounded-left">
<?php
$mysqlserver="localhost";
$mysqlusername="";
$mysqlpassword="";
$link=mysql_connect(localhost, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error());
$dbname = '';
mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error());
$cdquery="SELECT * FROM clients";
$cdresult=mysql_query($cdquery) or die ("Query to get data from clients failed: ".mysql_error());
while ($cdrow=mysql_fetch_array($cdresult)) {
$company_services=$cdrow["company_services"];
echo "<option>$company_services</option>";
}?>
</select>
<select id="cd" name="t" class="search-left rounded-left">
<?php
$mysqlserver="localhost";
$mysqlusername="";
$mysqlpassword="";
$link=mysql_connect(localhost, $mysqlusername, $mysqlpassword) or die ("Error connecting to mysql server: ".mysql_error());
$dbname = '';
mysql_select_db($dbname, $link) or die ("Error selecting specified database on mysql server: ".mysql_error());
$cdquery="SELECT * FROM clients";
$cdresult=mysql_query($cdquery) or die ("Query to get data from clients failed: ".mysql_error());
while ($cdrow=mysql_fetch_array($cdresult)) {
$town=$cdrow["town"];
echo "<option>$town</option>";
} ?>
</select>
<input type="submit" value="Search">
</form>
$k = $_GET['k'];
$t = $_GET['t'];
$i = 0;
$terms = explode(" ", $k);
$terms = explode(" ", $t);
$query ="SELECT * FROM clients WHERE ";
foreach ($terms as $each) {
$i++;
if ($i == 1)
$query .= "company_services LIKE '%$each%'" . "OR town LIKE '%$each%'";
}
$dbconnect=@mysql_connect($mysql_host, $mysql_user, $mysql_password);
$db = mysql_select_db('');
$query = mysql_query($query) or die(mysql_error());;
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)) {
/* show result here */
$k = $_GET['k'];
$t = $_GET['t'];
$i = 0;
$term1 = explode(" ", $k);
$term2 = explode(" ", $t);
$result = array_merge($term1, $term2);
$query ="SELECT * FROM clients WHERE ";
foreach ($result as $each) {
$i++;
if ($i == 1)
$query .= "company_services LIKE '%$each%'" . "OR town LIKE '%$each%'";
}
$dbconnect=@mysql_connect($mysql_host, $mysql_user, $mysql_password);
$db = mysql_select_db('db_name');
$query = mysql_query($query) or die(mysql_error());;
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)) {
答案 0 :(得分:1)
你正在写$terms
这样做,并给它一个去(顺便说一下,对于循环是可疑的大时间,考虑你想要实现的目标)
改变:
$terms = explode(" ", $k);
$terms = explode(" ", $t);
到:
$terms: [];
array_push($terms, explode(' ', $k));
array_push($terms, explode(' ', $t));
告诫这将为您提供嵌套数组,$terms[0]
将是$k
值的数组,而terms[1]
将是$t
值的数组。如果您只想要一个大平面阵列,请查看array_merge