我要从两个共同user_id
的传出表中获取记录。但是我可以去拿。以下是情况和预期结果。
table->users
id name email phone city users_id
1 abc [email protected] 9899989989 pqr BMP-1
2 ijk [email protected] 9899989988 jkl BMP-2
table->requirement
id label_name label_value users_id requirement_id date
103 budget 5000 BMP-1 4 11.03.16
104 specialist dentist BMP-1 4 11.03.16
105 treatment clinic BMP-1 4 11.03.16
106 expertise criminal BMP-2 5 10.03.16
107 charges 5100 BMP-2 5 10.03.16
预期结果:-应该在
while { }
---------------------------------------------------------
Name:-abc(BMP-1) dated:11.03.16
Looking For:- budges - 5000
specialist - dentist
treatment - clinick
--------------------------------------------------------------
---------------------------------------------------------
Name:-ijk(BMP-2) dated:10.03.16
Looking For:- expertise - criminal
charges - 5100
--------------------------------------------------------------
我已经试过了:-
<div class="table-responsive">
<table class="table table-hover">
<tbody>
<?php
$sql=mysql_query(
"SELECT * FROM users AS u RIGHT JOIN requirement AS r ON
u.users_id=r.users_id");
while($data=mysql_fetch_assoc($sql)){
?>
<!--loop start-->
<tr>
<td>
<div class="row" style="background: #00ACFF; ">
<p style="padding: 10px;margin: 0;color: white;">
<i class="fa fa-user"></i>
<strong> <?=$data['name']?> </strong>
<span style="font-size:85%;"><?=$data['users_id']?></span>
<span class="pull-right">
<i class="fa fa-clock-o"></i> <?=$data['date']?>
</span>
</p>
</div>
<div class="row" style="background: #EDFEAF; min-height:80px;">
*Looking For<br>
<br>
-> <?=$data['label_name']?>: <?=$data['label_value']?>
<br>
</div>
<div class="row" style="background: #00ACFF; ">
<p style="padding: 10px;margin: 0;color: white;">
<i class="fa fa-envelope"></i> <?=$data['email']?>
<i class="fa fa-mobile" style="margin-left:20px"></i>
<strong> <?=$data['phone']?> </strong>
<span class="pull-right">
<a class="btn btn-default btn-xs" href="#">
View @
<i class="fa fa-circle-thin"></i> 12pt
</a>
</span>
</p>
</div>
</td>
</tr>
<?php } ?>
<!--loop close-->
</tbody>
</table>
</div>
请告诉我我该怎么做。
最佳答案
试试这个办法。它有点长,但能解决你的问题。
$query = mysql_query("SELECT u.`users_id`, u.`name`, u.`email`, u.`phone`, re.`label_name`, re.`label_value`, re.`requirement_id`, re.`date` FROM users2 u INNER JOIN requirement re ON u.`users_id`=re.`users_id` WHERE 1");
$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']?></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> <?=$tempEmail?>
<i class="fa fa-mobile" style="margin-left:20px"></i>
<strong> <?=$tempPhone?> </strong>
<span class="pull-right">
<a class="btn btn-default btn-xs" href="#">
View @
<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']?></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> <?=$tempEmail?>
<i class="fa fa-mobile" style="margin-left:20px"></i>
<strong> <?=$tempPhone?> </strong>
<span class="pull-right">
<a class="btn btn-default btn-xs" href="#">
View @
<i class="fa fa-circle-thin"></i> 12pt
</a>
</span>
</p>
</div>
</td>
</tr>
</tbody>
</table>