2013-01-16 31 views
1

我试图通过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; 

} 

我在哪里错了?任何人都可以帮忙吗?

+0

echo $ csv_output是否显示正确的结果? – cartina

+0

你可以添加var_dump(db_fetch_object($ all_paid_scouts_entry));在进入while循环之前?它是否返回一个对象数组? – thecodeassassin

+0

我不能告诉你在这里使用了哪个数据库接口,但许多接口一次只能保持一个资源句柄处于活动状态。所以可能会发生这样的情况:一个接一个地执行两个'db_query'调用基本上抛出一个。哪些查询正在添加,哪些不显示?尝试在第一个while循环之后移动第二个'db_query'调用。 –

回答

0

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

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; 

} 

注:我没有测试此代码

+0

我得到了这个问题,但最好是通过分解它们来解决这个问题。 – RajeevK

+0

@RajeevK好吧,如果它的作品让我知道:) – thecodeassassin

+1

我已经使它的工作和你的分裂功能的想法做得很好...我接受这个作为答案.. 谢谢.. – RajeevK

0
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循环与这一个。

+0

它不起作用 – RajeevK

+0

echo $ csv_output是什么;打印? – cartina

+0

和以前一样.. – RajeevK