2016-03-15 54 views
0

我有許多記錄具有相同的專業知識,只要我搜索,它只給出一個結果而$exp的值是數組。 suppose $ exp = array(); 我已經試過我的代碼下面給出我用foreach()我在搜索期間只得到一個結果

提交
<?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(i.e.<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 } } ?> 
+2

你能嘗試生產最小((不)工作),例如你可以用下面? – BrainStone

+0

你可以給我們存儲在表中的數據嗎? –

+0

**使用不推薦使用的'mysql_ *'API停止**。使用'mysqli_ *'或'PDO' – Jens

回答

0

首先,不使用mysql_ *,見this後獲取更多信息。

除此之外,您不能將用戶數據直接粘貼到查詢中,這是不安全的,想象有人輸入類似「1; DROP TABLE要求」的內容。

要回答你的問題,你不能直接粘貼一個數組到一個查詢中,所以$ exp不是一個數組(除非你現在有錯誤)。

如果你確實有一個數組作爲$ EXP,使用的foreach循環將數組改寫爲字符串,所以你喜歡的東西

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

的問題mysql_fetch_assoc你已經使用。 mysql_fetch_assoc將返回一個與獲取行相對應的關聯數組,並將內部數據指針向前移動。

因爲您沒有迭代,將返回當前指針的關聯數組。

如果您還需要爲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']; 
} 
相關問題