我試圖通過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是否顯示正確的結果? – cartina
你可以添加var_dump(db_fetch_object($ all_paid_scouts_entry));在進入while循環之前?它是否返回一個對象數組? – thecodeassassin
我不能告訴你在這裏使用了哪個數據庫接口,但許多接口一次只能保持一個資源句柄處於活動狀態。所以可能會發生這樣的情況:一個接一個地執行兩個'db_query'調用基本上拋出一個。哪些查詢正在添加,哪些不顯示?嘗試在第一個while循環之後移動第二個'db_query'調用。 –