【问题标题】:Two WHILE inside a function - Not getting one executed函数内的两个 WHILE - 没有执行一个
【发布时间】:2013-01-16 06:58:07
【问题描述】:

我试图在 WHILE 的帮助下将两个 DB_QUERY 的结果一个接一个地添加到一个函数中,但是当我下载 CSV 时,我只在一个 WHILE 中得到结果,我正在打印最终变量。这是代码-

    <?php

function getPaidScoutUsers($nid) {
  $csv_output = '';
  $all_paid_scouts_entry = db_query("SELECT * FROM {signup_event} WHERE event_nid = %d",$nid);
  while($paid_user = db_fetch_object($all_paid_scouts_entry)){
    $role_name     = 'Scout';
    $profile       = content_profile_load(scout_profile, $paid_user->attendee_uid);
    $firstname     = $profile->field_sfname[0]['value'];
    $lname         = $profile->field_last_name[0]['value'];
    $patrol        = get_term_name($profile->field_scout_patrol[0]['value']);
    $position      = get_term_name($profile->field_scout_rank[0]['value']);    
    $homephone     = "";
    $cellphone     = "";
    $email         = "";
    $paid_status   = 'Paid';
    $payment_date  = date_format_date($paid_user->created_date[0]['value'],$type = 'custom', $format = 'm/d/Y');
    $csv_output .= "\"".$role_name."\"".","."\"".$lname ."\"".","."\"".$firstname."\"".","."\"".$patrol."\"".","."\"".$position."\"".","."\"".$homephone."\"".",".$cellphone.",".$email.","."\"".$paid_status."\"".","."\"".$payment_date."\""."\r";    
  }
  return $csv_output;
}

function getUnpaidUsers($nid) {
    $csv_output = '';
    $all_unpaid_scout_list = db_query("SELECT * FROM users
                                     INNER JOIN users_roles ON users.uid = users_roles.uid
                                     WHERE users_roles.rid =7
                                     AND users.uid NOT IN (
                                     SELECT attendee_uid FROM signup_event WHERE event_nid = %d)
                                     ORDER BY name ASC",$nid);
    while($unpaid_user = db_fetch_object($all_unpaid_scout_list)){
      $role_name     = 'Scout';
      $profile       = content_profile_load(scout_profile, $unpaid_user->uid);
      $firstname     = $profile->field_sfname[0]['value'];
      $lname         = $profile->field_last_name[0]['value'];
      $patrol        = get_term_name($profile->field_scout_patrol[0]['value']);
      $position      = get_term_name($profile->field_scout_rank[0]['value']);   
      $homephone     = trim($profile->home_phone[0]['value']);
      $cellphone     = trim($profile->cell_phone[0]['value']);
      $email         = $profile->email_1;
      $paid_status   = 'Unpaid';
      $payment_date  = '';
      $csv_output .= "\"".$role_name."\"".","."\"".$lname ."\"".","."\"".$firstname."\"".","."\"".$patrol."\"".","."\"".$position."\"".","."\"".$homephone."\"".",".$cellphone.",".$email.","."\"".$paid_status."\"".","."\"".$payment_date."\""."\r";
    }
    return $csv_output;
}

function event_signup_download_detail($nid)
{ 
    //global $user;   
    module_load_include('inc', 'signup_event', 'event_signup_view');
    $csv_output = '';
    $csv_output .= "Role,Last name,First Name,Patrol,Position,Home phone,Cell Phone,Email,Payment Status,Payment Date\n";           
    $nid = 2001;

    // add the paid users to the csv 
    $csv_output .= getPaidScoutUsers($nid);

    // get unpaid users, add them to the csv
    //$csv_output .= getUnpaidUsers($nid);

    echo $csv_output;

    header("Content-type: application/vnd.ms-excel");
    header("Content-Disposition: attachment;   filename=\"Registration_Dues_Information.csv\"");
    print $csv_output;
    exit; 

}

我在哪里错了?有人可以帮忙吗?

【问题讨论】:

  • echo $csv_output 是否显示正确的结果?
  • 你能添加 var_dump(db_fetch_object($all_paid_scouts_entry));在进入while循环之前?它是否返回一个对象数组?
  • 我不知道您在这里使用的是哪个数据库接口,但许多接口一次只保持一个资源句柄处于活动状态。因此,一个接一个地进行两个db_query 调用可能实际上是丢弃一个。哪个查询被添加,哪个没有显示?尝试在第一个 while 循环之后移动第二个 db_query 调用。
  • @PatrickM - 看到你的评论后我确实尝试过......但结果相同
  • @Stephen - 它给出:bool(false)

标签: php mysql drupal drupal-6


【解决方案1】:

尝试将功能拆分为单独的方法。

function getPaidScoutUsers($nid) {

  $csv_output = '';
  $all_paid_scouts_entry = db_query("SELECT * FROM {signup_event} WHERE event_nid = %d",$nid);
  while($paid_user = db_fetch_object($all_paid_scouts_entry)){
    $role_name     = 'Scout';
    $profile       = content_profile_load(scout_profile, $paid_user->attendee_uid);
    $firstname     = $profile->field_sfname[0]['value'];
    $lname         = $profile->field_last_name[0]['value'];
    $patrol        = get_term_name($profile->field_scout_patrol[0]['value']);
    $position      = get_term_name($profile->field_scout_rank[0]['value']);
    $homephone     = trim($paid_user->home_phone);
    $cellphone     = trim($paid_user->cell_phone);
    $email         = $profile->email_1;
    $paid_status   = 'Paid';
    $payment_date  = date_format_date($paid_user->created_date[0]['value'],$type = 'custom', $format = 'm/d/Y');
    $csv_output .= "\"".$role_name."\"".","."\"".$lname ."\"".","."\"".$firstname."\"".","."\"".$patrol."\"".","."\"".$position."\"".","."\"".$homephone."\"".",".$cellphone.","."\"".$paid_status."\"".","."\"".$payment_date."\""."\r";    
  }

  return $csv_output;
}

function getUnpaidUsers($nid) {
    $csv_output = '';
    $all_unpaid_scout_list = db_query("SELECT * FROM users
                                     INNER JOIN users_roles ON users.uid = users_roles.uid
                                     WHERE users_roles.rid =7
                                     AND users.uid NOT IN (
                                     SELECT attendee_uid FROM signup_event WHERE event_nid = %d)
                                     ORDER BY name ASC",$nid);
    while($unpaid_user = db_fetch_object($all_unpaid_scout_list)){
      $role_name     = 'Scout';
      $profile       = content_profile_load(scout_profile, $unpaid_user->uid);
      $firstname     = $profile->field_sfname[0]['value'];
      $lname         = $profile->field_last_name[0]['value'];
      $patrol        = get_term_name($profile->field_scout_patrol[0]['value']);
      $position      = get_term_name($profile->field_scout_rank[0]['value']);   
      $homephone     = trim($profile->home_phone[0]['value']);
      $cellphone     = trim($profile->cell_phone[0]['value']);
      $email         = $profile->email_1;
      $paid_status   = 'Unpaid';
      $payment_date  = '';
      $csv_output .= "\"".$role_name."\"".","."\"".$lname ."\"".","."\"".$firstname."\"".","."\"".$patrol."\"".","."\"".$position."\"".","."\"".$homephone."\"".",".$cellphone.",".$email.","."\"".$paid_status."\"".","."\"".$payment_date."\""."\r";
    }


    return $csv_output;
}

function event_signup_download_detail($nid)
{ 
    global $user;   
    module_load_include('inc', 'signup_event', 'event_signup_view');
    $csv_output = '';
    $csv_output .= "Role,Last name,First Name,Patrol,Position,Home phone,Cell Phone,Email,Payment Status,Payment Date\n";        

    // add the paid users to the csv 
    $csv_output .= getPaidScoutUsers($nid);

    // get unpaid users, add them to the csv
    $csv_output .= getUnpaidUsers($nid);

    header("Content-type: application/vnd.ms-excel");
    header("Content-Disposition: attachment;   filename=\"Registration_Dues_Information.csv\"");
    print $csv_output;
    exit; 

}

注意:我没有测试这段代码

【讨论】:

  • 我遇到了问题,但最好通过按您的方式拆分它们来解决。
  • 我已经成功了,你的拆分功能想法在这方面做得很好......我接受这个作为答案......谢谢......
【解决方案2】:
while($unpaid_user = db_fetch_object($all_unpaid_scout_list)){
      $role_name 1    = 'Scout';
      $profile1       = content_profile_load(scout_profile, $unpaid_user->uid);
      $firstname1     = $profile->field_sfname[0]['value'];
      $lname1         = $profile->field_last_name[0]['value'];
      $patrol1        = get_term_name($profile->field_scout_patrol[0]['value']);
      $position1      = get_term_name($profile->field_scout_rank[0]['value']);   
      $homephone1     = trim($profile->home_phone[0]['value']);
      $cellphone1     = trim($profile->cell_phone[0]['value']);
      $email1         = $profile->email_1;
      $paid_status1   = 'Unpaid';
      $payment_date1  = '';
      $csv_output .= "\"".$role_name1."\"".","."\"".$lname1 ."\"".","."\"".$firstname1."\"".","."\"".$patrol1."\"".","."\"".$position1."\"".","."\"".$homephone1."\"".",".$cellphone1.",".$email1.","."\"".$paid_status1."\"".","."\"".$payment_date1."\""."\r";
    }

echo $csv_output;

只需用这个替换第二个 while 循环。

【讨论】:

  • 什么 echo $csv_output;打印?
  • @RajeevK 你能把它贴在这里吗?
  • 我认为问题出在其他问题上,比如我的 $nid 没有给出查询结果,所以我改变了它,现在我有了结果,但仍然在我试图获取结果数组的 while 循环中,它没有返回的东西......
  • 在 while 循环中打印 $paid_user 并尝试在 phpmyadmin 中回显和运行查询?
  • 你得到 $profile->field_scout_patrol[0]['value'] 的值了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-04
  • 1970-01-01
  • 1970-01-01
  • 2020-02-07
  • 1970-01-01
相关资源
最近更新 更多