php - while 循环没有按预期循环

标签 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                    /*}
                    } */




        } 
    } 

?>

最佳答案

您的代码大致如下所示:

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
    }
  }
}

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

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

关于php - while 循环没有按预期循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21311663/

相关文章:

javascript - 如何将当前表行和 JavaScript 提示值传递给 PHP/MySQL?

php - 如何从多个嵌套if条件调用json

php - 为什么在 PHP MySQL 中用 %40 替换 @

java - 使用存储过程优化查询

mysql - 合并 id 重叠的 2 个表

php - 在 "background"运行函数

php - 如何将我的网站、mySQL、php 页面、文件、设置等迁移到 Amazon EC2?

php - 如何处理数据库中项目列表的 "order"(php)

php - 重试失败的 MySQL 查询?

sql - OData 相关字段选择语法