而循环不按预期循环

时间:2014-01-23 14:57:05

标签: php mysql sql

我有一个循环,它运行MySQL查询返回的行并执行命令。

当数据显示在网页上时(在粘贴代码的末尾看到),requested_date(来自$ result_dec查询)正在循环,但memPayout(来自$ emailcompleted查询)不是。在显示memPayout的地方,它只列出了每个日期的相同数字。

当我在phpmyadmin中运行时,我看到不同的日期有不同的memPayouts ..所以我知道这里有一个错误。

我很感激帮助。

更新:我已经使用我调整过的新代码更新了旧代码。我试图用最初的foreach包装电子邮件收入查询。我还删除了不必要的额外bgcolor开关。我网页上的结果仍然与以前相同。真的很感激任何更多的建议。

<?php                           
//--------------------- Code for Decline TRANSACTION --------------------------------------
$u_id = $_SESSION['user_ses']['id'];

$result_dec = @mysql_query("(select trv.tr_user_id , usr.full_name ,usr.email , usr.paypal ,  trv.requested_date, trv.requested_status, trv.click_payment_status, trv.tracking_id
from  tbl_trackvalue as trv ,tbl_tracking as t , tbl_offers as off , tblusers as usr
where t.id=trv.tracking_id  and off.id=t.offer_id and  usr.id=trv.tr_user_id and usr.id='1454' 
and trv.payment_status='pending' and trv.requested_status='declined'  group by trv.tr_user_id, trv.requested_date order by trv.requested_date asc )
union all
(select trv.tr_user_id , usr.full_name ,usr.email , usr.paypal , trv.click_request_date, trv.requested_status, trv.click_payment_status, trv.tracking_id
from  tbl_trackvalue as trv ,tbl_tracking as t , tbl_offers as off , tblusers as usr
where t.id=trv.tracking_id  and off.id=t.offer_id and  usr.id=trv.tr_user_id and usr.id='1454' 
and trv.payment_status='pending' and trv.click_payment_status='declined'  group by trv.tr_user_id, trv.click_request_date order by trv.requested_date asc ) ");
    $nr = mysql_num_rows($result_dec);
    $categories_d = array();
    while($row = mysql_fetch_array($result_dec))
    { 
            $categories_d[] = $row;
    }

    if(count($categories_d)>0)
    {
        $counter=0;
        foreach($categories_d as $index=>$rec)
        {
            $counter++;
            if ($counter % 2 == 0)
            {
                    $bgcolor = "#FFFFFF";
            }
            else
            {
                    $bgcolor = "#F5F7D9";
            }



        $userId=$rec['tr_user_id'];

  $EmailCompletedOffer=@mysql_query("select off.member_amount as memPayout
                        from  tbl_trackvalue as trv ,tbl_tracking as t , tbl_offers as off , tblusers as usr 
                        where t.id=trv.tracking_id  and off.id=t.offer_id and  off.offer_type='mailchimp' and  usr.id=trv.tr_user_id and 
                        trv.tr_user_id=$userId and trv.requested_date='".$requested_date."' and trv.payment_status='pending' and trv.total_conversion !=0 and trv.requested_status='declined' ");
                        $rowemailEarn=@mysql_fetch_array($EmailCompletedOffer);
                        $totalEmailEarnAmount1=$rowemailEarn['memPayout'];

?>
                        <div class="datarow_his">
                            <div class="his_onecol1"><?php echo '$'. $totalEmailEarnAmount1;?> </div>
                            <div class="his_onecol1"> <?php echo "Declined"; ?> </div>
                            <div class="his_onecol2"><?php echo date("M j,Y " ,strtotime($rec['requested_date']));?></div>
                            <!--<div class="his_onecol1"><?php //echo date("M j,Y " ,strtotime($rec['paid_date']));?></div>-->
                        </div> 
                        <p><?php echo $totalEmailEarnAmount1?></p>
<?php                    /*}
                    } */




        } 
    } 

?>

1 个答案:

答案 0 :(得分:0)

您的代码大致如下所示:

fetch categories
if(categories not empty) {
  foreach(category) {
    set $bgcolor and $userId (has no effect)
  }
  get query results for the last $userID and set $totalEmailEarnAmount1
  if(categories not empty) {
    foreach(category) {
      set $bgcolor again (has no effect)
      generate a div using $totalEmailEarnAmount1
    }
  }
}

由于您将$ totalEmailEarnAmount1设置在最终foreach之外,因此它在循环的每次迭代中都具有相同的值。

(如果你以与其结构一致的方式缩进你的代码,这样的错误就会变得很明显。凌乱的缩进使你很难看到发生了什么。)