简体   繁体   English

我在搜索过程中只得到一个结果,

[英]i am getting only one result during search,

I have many records with same expertise and whenever i search then it gives only one result While the values of $exp is array . 我有许多具有相同专业知识的记录,每当我搜索时,它只会给出一个结果,而$exp的值是array。 suppose $exp=array(); 假设$ exp = array(); I have tried my code is given below i used foreach() 我已经尝试过我的代码如下我使用foreach()

Submit 提交
 <?php if(isset($_POST['submit'])){ echo $exp=$_POST['expertise']; $sq=mysql_fetch_assoc(mysql_query("select distinct(requirement_id) from requirement where label_value='$exp'")); foreach($sq as $ii){ echo $query1 = "SELECT u.`users_id`, u.`name`, u.`email`, u.`phone`, re.`label_name`, re.`label_value`, re.`requirement_id`, re.`date` FROM users u INNER JOIN requirement re ON u.`users_id`=re.`users_id` WHERE re.`requirement_id`='$ii'"; $query=mysql_query($query1); $resultArr = array(); //to store query result while($row=mysql_fetch_assoc($query)) { $resultArr[] = $row; } //print_r($resultArr); ?> <table class="table table-hover"> <tbody> <?php $tempUserID = ""; $tempEmail = ""; $tempPhone = ""; $tempReqID = 0; for($i=0;$i<count($resultArr);$i++) { //if user id is blank, assign temporary values we need only for one time. if($tempUserID=="") { $tempUserID = $resultArr[$i]['users_id']; $tempEmail = $resultArr[$i]['email']; $tempPhone = $resultArr[$i]['phone']; $tempReqID = $resultArr[$i]['requirement_id']; //printing first row ?> <tr> <td> <div class="row" style="background: #00ACFF; "> <p style="padding: 10px;margin: 0;color: white;"> <i class="fa fa-user"></i> <strong> <?=$resultArr[$i]['name']?> </strong> <span style="font-size:85%;"><?=$resultArr[$i]['users_id']?>000<?=$tempReqID?></span> <span class="pull-right"> <i class="fa fa-clock-o"></i> <?=$resultArr[$i]['date']?> </span> </p> </div> <div class="row" style="background: #EDFEAF; min-height:80px;"> *Looking For<br>-> <?php } //ok if($tempUserID == $resultArr[$i]['users_id'] && $tempReqID==$resultArr[$i]['requirement_id']) { //printing label_name and label_value if users_id is equal to the tempuserid ?> <br> <?=$resultArr[$i]['label_name']?>: <?=$resultArr[$i]['label_value']?> <br> <?php } else { //if users_id is not equal to the previous one, we will close the first row(ie<tr>) and start a new one ?> </div> <div class="row" style="background: #00ACFF; "> <p style="padding: 10px;margin: 0;color: white;"> <i class="fa fa-envelope"></i>&nbsp;<?php echo $email = substr_replace($tempEmail,"xxxxxxxxxx",3,10);?> <!--<i class="fa fa-envelope"></i> <?php //$tempEmail?>--> <i class="fa fa-mobile" style="margin-left:20px"></i> <strong><?php echo $var1 =substr_replace($tempPhone, str_repeat("X", 6), 2, 6);?> </strong> <span class="pull-right"> <a class="btn btn-default btn-xs" href="#"> View @&nbsp;&nbsp;&nbsp; <i class="fa fa-circle-thin"></i> 12pt </a> </span> </p> </div> </td> </tr> <?php //since the users_id is not equal to the previous row, which means that data about new user is available, we will assign new values to temporary variables and start a new table row. $tempUserID = $resultArr[$i]['users_id']; $tempEmail = $resultArr[$i]['email']; $tempPhone = $resultArr[$i]['phone']; $tempReqID = $resultArr[$i]['requirement_id']; ?> <tr> <td> <div class="row" style="background: #00ACFF; "> <p style="padding: 10px;margin: 0;color: white;"> <i class="fa fa-user"></i> <strong> <?=$resultArr[$i]['name']?> </strong> <span style="font-size:85%;"><?=$resultArr[$i]['users_id']?>000<?=$tempReqID?></span> <span class="pull-right"> <i class="fa fa-clock-o"></i> <?=$resultArr[$i]['date']?> </span> </p> </div> <!--the edited part --> <div class="row" style="background: #EDFEAF; min-height:80px;"> *Looking For<br>-> <br> <?=$resultArr[$i]['label_name']?>: <?=$resultArr[$i]['label_value']?> <br> <?php } } ?> <!--we will close the table row if current row is the last one in the result--> <div class="row" style="background: #00ACFF; "> <p style="padding: 10px;margin: 0;color: white;"> <i class="fa fa-envelope"></i> <?php echo $email = substr_replace($tempEmail,"xxxxxxxxxx",3,10);?> <i class="fa fa-mobile" style="margin-left:20px"></i> <strong> <?php echo $var1 =substr_replace($tempPhone, str_repeat("X", 6), 2, 6); ?></strong> <span class="pull-right"> <a class="btn btn-default btn-xs" href="#"> View @&nbsp;&nbsp;&nbsp; <i class="fa fa-circle-thin"></i> 12pt </a> </span> </p> </div> </td> </tr> </div><!--/col-9--> </tbody> </table> <?php } } ?> 

First off, don't use mysql_*, see this post for more info. 首先,不要使用mysql_ *,有关更多信息,请参见这篇文章。

Apart from that, you can't paste user data directly into a query, that's unsafe, imagine someone entering something like "1; DROP TABLE requirements". 除此之外,您不能将用户数据直接粘贴到查询中,这是不安全的,假设有人输入了类似“ 1; DROP TABLE requirements”的内容。

To answer your question, you can't paste an array directly into a query so $exp is not an array (unless you get errors right now). 要回答您的问题,您不能将数组直接粘贴到查询中,因此$ exp不是数组(除非您现在遇到错误)。

If you actually have an array as $exp, use an foreach-loop to rewrite your array to a string so you get something like 如果您实际上有一个数组作为$ exp,请使用foreach循环将您的数组重写为字符串,以便获得类似

select distinct(requirement_id) from requirement where label_value IN(1,2,3)

The problem with mysql_fetch_assoc which you have used . 您使用的mysql_fetch_assoc问题。 The mysql_fetch_assoc will return an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc将返回一个与所获取的行相对应的关联数组,并将内部数据指针向前移动。

since you are not iterating that will return associate array of the current pointer . 由于您未进行迭代,因此将返回当前指针的关联数组。

You can use as below if you still need to use as foreach or directly use while 如果您仍然需要使用foreach或直接while

$result = mysql_query("select distinct(requirement_id) from requirement     where label_value='$exp'");
while($row = mysql_fetch_assoc($result))// this will iterate the pointer 
{
    $sq[] = $row['requirement_id'];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM