php 遍历数组后如何处理结果

新手小白,在网上找到下面这段代码,想用于搜索,但不知道该如何添加mysql查询语句,使之能循环查询遍历后的值。急~~在线等!
function strsToArray($strs) {
$result = array();
$array = array();
$strs = str_replace(',', ',', $strs);
$strs = str_replace("n", ',', $strs);
$strs = str_replace("rn", ',', $strs);
$strs = str_replace(' ', ',', $strs);
$array = explode(',', $strs);
foreach ($array as $key => $value) {
if ('' != ($value = trim($value))) {
$result[] = $value;
echo $value;
}
}
return $result;
}
//test
$strs = $_GET["zi"];
var_dump(strsToArray($strs));

第1个回答  2013-09-28
<?php

function strsToArray($strs) {
$result = array();
$array = array();
$strs = str_replace(',', ',', $strs);
$strs = str_replace("n", ',', $strs);
$strs = str_replace("rn", ',', $strs);
$strs = str_replace(' ', ',', $strs);
$array = explode(',', $strs);
foreach ($array as $key => $value) {
if ('' != ($value = trim($value))) {
$result[] = $value;
}
}
foreach($result as $k=>$v){
$sql="";
$sql="select * from table where 查询字段 = '".$v."' ";
$row = mysql_query($sql);
if($ret = mysql_fetch_assoc($row)){
print_r($ret);
}else{
echo "没有找到值为".$v."的数据";
}
echo "<br>";
}
}

$strs = $_GET["zi"];
strsToArray($strs);

?>本回答被提问者采纳
第2个回答  推荐于2017-10-15

//示例代码:index.php

<?php
//请求URL示例:,zz,ddd
//获取参数
$strs = $_GET["zi"];
//调用函数(strsToArray) 构造查询sql条件
$where = strsToArray($strs);
//连接数据库
$con = mysql_connect("localhost","root","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);
//拼装sql、结果如:SELECT * FROM test where 1=1 and title like '%111%' and title like '%zz%' and title like '%ddd%' 
$sql="SELECT * FROM test where 1=1 ".$where;
echo $sql;exit;
$result = mysql_query($sql);

echo "查询信息如下:<br/>";
while($row = mysql_fetch_array($result))
  {
  echo $row['字段2'] . "=====" . $row['字段三'];
  echo "<br />";
  }

mysql_close($con);
function strsToArray($strs) {
$where = "";
$array = array();
$strs = str_replace(',', ',', $strs);
$strs = str_replace("n", ',', $strs);
$strs = str_replace("rn", ',', $strs);
$strs = str_replace(' ', ',', $strs);
$array = explode(',', $strs);
foreach ($array as $key => $value) {
    if ('' != ($value = trim($value))) {
        $where.=" and title like '%{$value}%' ";
    }
}
return $where;
}
?>

本回答被网友采纳
第3个回答  2013-09-28

你贴的这段代码有问题:

function strsToArray($strs) { 
    $strs = str_replace(',', ',', $strs); //处理中文逗号
    $strs = str_replace("\n", ',', $strs); //处理换行符
    $strs = str_replace("\r\n", ',', $strs); //处理换行
    $strs = str_replace(' ', ',', $strs); //替换空格为逗号
    $array = explode(',', $strs); //将处理后的字符串转为数组
    return $array; 


$strs = trim($_GET["zi"]);
var_dump(strsToArray($strs));

精简掉不必要的,当然,还可以再精简,至于你说的用于搜索,就不明白。

这算是个字符串处理函数,和搜索能沾边吗?

第4个回答  2013-09-28

首先连接数据库

$conn = mysql_connect('localhost', 'root', 'mypassword'); //连接数据库
mysql_select_db('mydatabase'); //选择库
mysql_query('set names mycharset'); //设置编码

调用函数得到关键字数组

$arr = strsToArray($strs); //得到要查询的关键字数组

遍历查询

$result = array(); //初始化结果数组
foreach($arr as $keyword) //遍历数组
{
  $sql = "select * from mytable where myfield like '%$keyword%'"; //构造SQL语句
  $obj = mysql_query($sql); //查询数据库
  $result = array_merge($result, mysql_fetch_array($obj)); //取得结果数组
}
$result = array_unique($result); //去重

第5个回答  2013-09-28
亲,我想知道你这if ('' != ($value = trim($value)
是怎么回事?你在if里打印一下$value或着$result就知道自己错哪了

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网