如何将一个字段包含在while循环查询中?

时间:2014-11-04 08:23:57

标签: php mysql

我在查询中使用了foreach来检索数据但是在页面中间我有一个字段$row['RemoteEmployeeID'],它不在while循环中,我无法将其包括在内循环,如果我包含它,所以我的标题将与每个记录重复。

这是我的代码:

<html>
<head>
</head>
<body>
<div class="wrapper">
<div class="header">
<?php
error_reporting('0');
  ob_start();
session_start();
if(!isset($_SESSION['email']))
{
  echo '<META HTTP-EQUIV="Refresh" Content="0; URL= ../../spd/myaccount.php">';
  exit();
}
else 
{
$email = $_SESSION['email'];
require_once('../../Admin Panel/db.php');

$query = "SELECT 
   properties.PropertyID,
   properties.PropertyType,
   properties.PropertyDealType,
   properties.Status,
   properties.PropostedPrice,
   properties.PropertyStatus,
   properties.RemoteEmployeeEmail,
   properties.PropertyDealerName,
   properties.PropertyOwnerName,
   remoteemployees.RemoteEmployeeID

   FROM  remoteemployees, properties

    WHERE remoteemployees.RemoteEmployeeEmail = properties.RemoteEmployeeEmail   

   AND  properties.RemoteEmployeeEmail = '".$email."'

   AND  properties.Status= 'Active'

   AND  properties.PropertyStatus= 'APPROVED'


   ORDER BY properties.PropertyID
   ";


  $query_run = $connection->query($query);
  if( $connection->error ) exit( $connection->error );
  while($res=$query_run->fetch_assoc()) {
                $count      =   $query_run->num_rows;          // Taking the number of queries returened by the result
                $array[]    =   $res;
  }
         ?>
<div style="float:left;"><font color="#000000">Welcome: <?php echo $_SESSION['email']; ?> 
</font>
</div>
<div style="float:right; color:#999; width:600px; margin-left:155px;">


<a href="#" class="button">My Contract Copy</a>

<a href="#" class="button">My Property List</a>

<a href="#" class="button">My Trans.History</a>

<a href="Remote Employee profile.php?RemoteEmployeeid=<?php echo $row['RemoteEmployeeID'] ?>" class="button" >My Profile</a>

<a href="logout.php" class="button">Logout</a>


</div>


</div>

<div class="Labelcon">
  <div class="Label">Property ID</div>
  <div class="Label">Property Type</div>
  <div class="Label">Property Deal Type</div>
  <div class="Labelowner">Property Owner</div>
  <div class="Labeldealer">Property Dealer</div>
  <div class="Label">Proposted Price</div>
  <div class="Label">Property Status</div>
</div>
<?php
        // Here is the start of the loop
        foreach($array as $row) { ?>

<div class="Valuecon">
  <div class="Value"><?php echo $row['PropertyID'] ?></div>
  <div class="Value"><?php echo $row['PropertyType'] ?></div>
  <div class="Value"><?php echo $row['PropertyDealType']?></div>
  <div class="Valueowner"><?php echo $row['PropertyOwnerName'] ?></div>
  <div class="Valuedealer"><?php echo $row['PropertyDealerName'] ?></div>
  <div class="Value"><?php echo $row['PropostedPrice'];?></div>
  <div class="Value"><?php echo $row['PropertyStatus'];?></div>
</div>

<?php 

  if($query_run->num_rows == 0)
  {
      echo 'No Records Found';
  }

}}
?>
</div>
<p></p>
<center>
<?php $queryemail= "SELECT SUM(transactions.EarnAmount) AS TotalEarn, transactions.TaxDeduction,

SUM(transactions.AmountPaid) AS TotalPaid from transactions WHERE transactions.RemoteEmployeeID = ".$row['RemoteEmployeeID']." limit 1"; ?>
<div class="Amountpayablepaid">

  <div class="amountpayable">

      <div class="aclabel">Earn Amount:</div>

      <div class="acvalue">

      <?php
       $queryemail_run = $connection->query($queryemail);
  if( $connection->error ) exit( $connection->error );
  while($row=$queryemail_run->fetch_assoc()) 
  {   
      ?>
      <div class="USD">
      <?php echo 'USD '.$row['TotalEarn'];  ?>
      </div>


      </div>
      <br>
    <br>  
  </div>
  <div class="amountpaid">

      <div class="acpaidlable">Tax Deduction:</div>

      <div class="acpaidvalue">

        <div class="USDpaid"><?php echo $row['TaxDeduction'].' %'; ?></div>


      </div>

  </div>

  <div class="amountpaid" style="margin-top:22px;">

      <div class="acpaidlable">Amount Paid:</div>

      <div class="acpaidvalue">

        <div class="USDpaid"><?php echo 'USD '.$row['TotalPaid'];}?></div>


      </div>

  </div>

</div>
</center>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

简单:

$RemoteEmp = null;
$count = $query_run->num_rows;

while($res=$query_run->fetch_assoc()) {
    $array[]    =   $res;
    $RemoteEmp = $res["RemoteEmployeeID"];

}

现在:

<a href="Remote Employee profile.php?RemoteEmployeeid=<?php echo $RemoteEmp; ?>" class="button" >My Profile</a>