评论列未显示在csv输出文件

时间:2015-09-30 16:27:19

标签: php wordpress csv

我是一名网页设计师,是php的新手,并试图修复由开发人员完成的自定义wordpress插件。有一个客户调查,结果吐出到CSV文件中。除了注释框之外,所有字段都会吐出。

以下是客户调查表中的PHP。

<div class="form-field-container form-field-comment">
<label class="form-field-label" for="customer-survey-member-comment">Comments:</label>
<textarea id="customer-survey-member-comment" name="customer_survey[member_comment]"><?php print esc_textarea($survey['member_comment']); ?></textarea>
</div>

我注意到了

<?php

function customer_survey_variables() {
return array(

页面没有列出member_comment所以我将它添加到'members'数组中。

'members' => array(
        'ackerley_rose' => 'Ackerley, Rose',
        'bailey_teresa' => 'Bailey, Teresa',
        'behshid_mary' => 'Behshid, Mary',
        'chiang_dudee' => 'Chiang, Dudee',
        'cooper_julie' => 'Cooper, Julie',
        'coppin_ann' => 'Coppin, Ann',
        'daruthayan_beth' => 'Daruthayan, Beth',
        'hernandez_marlon' => 'Hernandez, Marlon',          
        'honchell_mickey' => 'Honchell, Mickey',
        'le_minh' => 'Le, Minh',
        'kelly_michele' => 'Kelly, Michele',
        'medina_danielle' => 'Medina, Danielle',
        'phipps_dan' => 'Phipps, Dan',
        'powers_robert' => 'Powers, Robert',
        'reynolds_lee' => 'Reynolds, Lee',
        'schofield_sunny' => 'Schofield, Sunny',
        'tompson_sara' => 'Tompson, Sara',
        'member_comment' => 'Comments - Members',

我能够获得一个member_comment列,但它中有N / A.我很遗憾,但我的下一步是创建一个新的注释数组并将其添加到customer_survey_csv结果部分,但没有N / A,就像这里一样

foreach ($results as $index => $result) {
$ratings = array(
    'product' => array_fill_keys(array_keys($variables['products']), 'N/A'),
    'service' => array_fill_keys(array_keys($variables['services']), 'N/A'),
    'member' => array_fill_keys(array_keys($variables['members']), 'N/A'),
);

如果有人有任何指导或建议,我将不胜感激。感谢。

我正在使用的代码如下。

//设置客户调查的页面

<?php foreach ($services as $name => $label): ?>
<div class="form-field">
<label class="form-field-label"><?php print $label; ?></label>
<ul class="form-field-radio">
<li><input type="radio" name="customer_survey[service][<?php print $name; ? >]" value="1" <?php print $survey['service'][$name] == 1 ? 'checked ' : ''; ?>/></li>
<li><input type="radio" name="customer_survey[service][<?php print $name; ?>]" value="2" <?php print $survey['service'][$name] == 2 ? 'checked ' : ''; ?>/></li>
<li><input type="radio" name="customer_survey[service][<?php print $name; ?>]" value="3" <?php print $survey['service'][$name] == 3 ? 'checked ' : ''; ?>/></li>
<li><input type="radio" name="customer_survey[service][<?php print $name; ?>]" value="4" <?php print $survey['service'][$name] == 4 ? 'checked ' : ''; ?>/></li>
<li><input type="radio" name="customer_survey[service][<?php print $name; ?>]" value="5" <?php print $survey['service'][$name] == 5 ? 'checked ' : ''; ?>/></li>
</ul>
</div>
<?php endforeach; ?>

<?php foreach ($comments as $name => $label): ?>
<div class="form-field-container form-field-comment">
<label class="form-field-label" for="customer-survey-service-comment">Service Comments:</label>
<textarea id="customer-survey-service-comment" name="customer_survey[service_comment]"><?php print esc_textarea($survey['service_comment']); ?></textarea>
</div>
<?php endforeach; ?>
</div>

<div class="form-field-container" id="customer-survey-members">
<div class="form-field-container-label form-field">
<label class="form-field-label">Staff:</label>
<ul class="form-field-radio">
<li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
        </ul>
    </div>

    <?php foreach ($members as $name => $label): ?>
    <div class="form-field">
        <label class="form-field-label"><?php print $label; ?></label>
        <ul class="form-field-radio">
            <li><input type="radio" name="customer_survey[member][<?php print $name; ?>]" value="1" <?php print $survey['member'][$name] == 1 ? 'checked ' : ''; ?>/></li>
            <li><input type="radio" name="customer_survey[member][<?php print $name; ?>]" value="2" <?php print $survey['member'][$name] == 2 ? 'checked ' : ''; ?>/></li>
            <li><input type="radio" name="customer_survey[member][<?php print $name; ?>]" value="3" <?php print $survey['member'][$name] == 3 ? 'checked ' : ''; ?>/></li>
            <li><input type="radio" name="customer_survey[member][<?php print $name; ?>]" value="4" <?php print $survey['member'][$name] == 4 ? 'checked ' : ''; ?>/></li>
            <li><input type="radio" name="customer_survey[member][<?php print $name; ?>]" value="5" <?php print $survey['member'][$name] == 5 ? 'checked ' : ''; ?>/></li>
        </ul>
    </div>
    <?php endforeach; ?>


    <div class="form-field-container form-field-comment">
        <label class="form-field-label" for="customer-survey-member-comment">Comments:</label>
        <textarea id="customer-survey-member-comment" name="customer_survey[member_comment]"><?php print esc_textarea($survey['member_comment']); ?></textarea>
    </div>

//收集结果的页面并将其发送到CSV页面

<?php if (!empty($delete_message)): ?>
<div class="customer-survey-delete-message <?php print !empty($deleted_rows) ? 'success' : 'error'; ?>"><?php print $delete_message; ?></div>
<?php endif; ?>

<table cellspacing="0" id="customer-survey-product-averages" class="wp-list-table widefat fixed customer-survey-average">

<thead>
    <tr>
        <th class="customer-survey-average-label">Product Name</th>
        <th class="customer-survey-average-label">Average Rating</th>
    </tr>
</thead>
<tbody>
<?php foreach ($averages['products'] as $name => $average): ?>
<tr>
<td class="customer-survey-average-label"><?php print $variables['products'][$name]; ?></td>
<td class="customer-survey-average-value"><?php print is_null($average) ? 'No Rating' : $average; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

<table cellspacing="0" id="customer-survey-member-averages" class="wp-list-table widefat fixed customer-survey-average">
<thead>
    <tr>
        <th class="customer-survey-average-label">Staff Member Name</th>
        <th class="customer-survey-average-value">Average Rating</th>
    </tr>
</thead>
<tbody>
    <?php foreach ($averages['members'] as $name => $average): ?>
    <tr>
        <td class="customer-survey-average-label"><?php print $variables['members'][$name]; ?></td>
        <td class="customer-survey-average-value"><?php print is_null($average) ? 'No Rating' : $average; ?></td>
    </tr>
    <?php endforeach; ?>
</tbody>
</table>

<table cellspacing="0" id="customer-survey-service-averages" class="wp-list-table widefat fixed customer-survey-average">
<thead>
    <tr>
        <th class="customer-survey-average-label">Service Name</th>
        <th class="customer-survey-average-value">Average Rating</th>
    </tr>
</thead>
<tbody>
    <?php foreach ($averages['services'] as $name => $average): ?>
    <tr>
        <td class="customer-survey-average-label"><?php print $variables['services'][$name]; ?></td>
        <td class="customer-survey-average-value"><?php print is_null($average) ? 'No Rating' : $average; ?></td>
    </tr>
    <?php endforeach; ?>
</tbody>
</table>

<hr class="customer-survey-break" />

<form id="customer-survey-result" method="post">

<a href="<?php print plugin_dir_url(__FILE__) . 'customer_survey_csv.php'; ?>" id="customer-survey-download-csv">Download CSV</a>

<table cellspacing="0" id="customer-survey-results" class="wp-list-table widefat fixed">
<thead>
<tr>
        <th class="customer-survey-average-label sortable <?php print $dir . ($sort == 'badge' ? ' sorted' : NULL); ?>">
            <a href="<?php print admin_url('admin.php?' . http_build_query(array(
                'page' => 'customer-survey-results',
                'pagination' => $page,
                'sort' => 'badge',
                'dir' => $sort == 'badge' ? ($dir == 'asc' ? 'desc' : 'asc') : 'asc',
            ))); ?>"><span>Badge Number</span><span class="sorting-indicator"></span></a>
        </th>
        <th class="customer-survey-average-label sortable <?php print $dir . ($sort == 'email' ? ' sorted' : NULL); ?>">
            <a href="<?php print admin_url('admin.php?' . http_build_query(array(
                'page' => 'customer-survey-results',
                'pagination' => $page,
                'sort' => 'email',
                'dir' => $sort == 'email' ? ($dir == 'asc' ? 'desc' : 'asc') : 'asc',
            ))); ?>"><span>Email</span><span class="sorting-indicator">   </span></a>
        </th>
        <th class="customer-survey-average-label sortable <?php print $dir . ($sort == 'ipaddress' ? ' sorted' : NULL); ?>">
            <a href="<?php print admin_url('admin.php?' . http_build_query(array(
                'page' => 'customer-survey-results',
                'pagination' => $page,
                'sort' => 'ipaddress',
                'dir' => $sort == 'ipaddress' ? ($dir == 'asc' ? 'desc' : 'asc') : 'asc',
            ))); ?>"><span>IP Address</span><span class="sorting-indicator"></span></a>
        </th>
        <th class="customer-survey-average-label sortable <?php print $dir . ($sort == 'date' ? ' sorted' : NULL); ?>">
            <a href="<?php print admin_url('admin.php?' . http_build_query(array(
                'page' => 'customer-survey-results',
                'pagination' => $page,
                'sort' => 'date',
                'dir' => $sort == 'date' ? ($dir == 'asc' ? 'desc' : 'asc') : 'asc',
            ))); ?>"><span>Date Submitted</span><span class="sorting-indicator"></span></a>
        </th>
        <th class="customer-survey-average-label">&nbsp;</th>
    </tr>
</thead>
<tbody>
    <?php foreach ($results as $index => $result): ?>
    <tr data-survey-trigger="<?php print $index; ?>">
        <td class="customer-survey-average-value"><?php print !empty($result->customer_badge) ? $result->customer_badge : 'Not given'; ?></td>
        <td class="customer-survey-average-value"><?php print !empty($result->customer_email) ? $result->customer_email : 'Not given'; ?></td>
        <td class="customer-survey-average-value"><?php print $result->ipaddress ?></td>
        <td class="customer-survey-average-value"><?php print date("m/d/y g:i A", strtotime($result->created_at)) ?></td>
        <th class="customer-survey-average-label">
            <a href="#">view</a>
            &nbsp;&nbsp;
            <button class="customer-survey-delete" type="submit" name="delete" value="<?php print $result->id; ?>">delete</button>
        </th>
    </tr>
    <tr data-survey-target="<?php print $index; ?>">
        <td colspan="5">
            <div class="customer-survey-product-container">
            <?php if (!empty($result->products)): ?>
                <h4>Product Ratings</h4>
                <ul class="customer-survey-product-ratings">
                <?php foreach ($result->products as $product): ?>
                    <?php if (!empty($variables['products'][$product->product])): ?>
                        <li>
                            <span class="customer-survey-rating-label"><?php print $variables['products'][$product->product]; ?></span>
                            <span class="customer-survey-rating-value"><?php print intval($product->rating); ?></span>
                        </li>
                    <?php endif; ?>
                <?php endforeach; ?>
                </ul>
            <?php endif; ?>

            <?php if (!empty($result->product_comment)): ?>
                <h4>Product Comment</h4>
                <div class="customer-survey-comment"><?php print esc_textarea($result->product_comment); ?></div>
            <?php endif; ?>
            </div>

            <div class="customer-survey-service-container">
            <?php if (!empty($result->services)): ?>
                <h4>Service Ratings</h4>
                <ul class="customer-survey-service-ratings">
                <?php foreach ($result->services as $service): ?>
                    <?php if (!empty($variables['services'][$service->service])): ?>
                        <li>
                            <span class="customer-survey-rating-label"><?php print $variables['services'][$service->service]; ?></span>
                            <span class="customer-survey-rating-value"><?php print intval($service->rating); ?></span>
                        </li>
                    <?php endif; ?>
                <?php endforeach; ?>
                </ul>
            <?php endif; ?>

            <?php if (!empty($result->service_comment)): ?>
                <h4>Service Comment</h4>
                <div class="customer-survey-comment"><?php print esc_textarea($result->service_comment); ?></div>
            <?php endif; ?>
            </div>

            <div class="customer-survey-member-container">
            <?php if (!empty($result->members)): ?>
                <h4>Member Ratings</h4>
                <ul class="customer-survey-member-ratings">
                <?php foreach ($result->members as $member): ?>
                    <?php if (!empty($variables['members'][$member->member])): ?>
                        <li>
                            <span class="customer-survey-rating-label"><?php print $variables['members'][$member->member]; ?></span>
                            <span class="customer-survey-rating-value"><?php print intval($member->rating); ?></span>
                        </li>
                    <?php endif; ?>
                <?php endforeach; ?>
                </ul>
            <?php endif; ?>

            <?php if (!empty($result->member_comment)): ?>
                <h4>Member Comment</h4>
                <div class="customer-survey-comment"><?php print esc_textarea($result->member_comment); ?></div>
            <?php endif; ?>
            </div>
        </td>
    </tr>
    <?php endforeach; ?>
</tbody>

<div class="tablenav bottom">
<div class="tablenav-pages">
    <span class="displaying-num"><?php print !empty($survey_count) ? $survey_count : 0; ?> surveys</span>
    <span class="pagination-links">
        <a class="first-page" title="Go to the first page" href="<?php print admin_url('admin.php?' . http_build_query(array(
                'page' => 'customer-survey-results',
                'pagination' => 1,
                'sort' => $sort,
                'dir' => $dir,
            ))); ?>">«</a>
        <a class="prev-page" title="Go to the previous page" href="<?php print admin_url('admin.php?' . http_build_query(array(
                'page' => 'customer-survey-results',
                'pagination' => $page > 1 ? $page - 1 : 1,
                'sort' => $sort,
                'dir' => $dir,
            ))); ?>">‹</a>
        <span class="paging-input"><?php print $page; ?> of <span class="total-pages"><?php print $max_page ?></span></span>
        <a class="next-page" title="Go to the next page" href="<?php print admin_url('admin.php?' . http_build_query(array(
                'page' => 'customer-survey-results',
                'pagination' => ($page + 1) <= $max_page ? $page + 1 : $max_page,
                'sort' => $sort,
                'dir' => $dir,
            ))); ?>">›</a>
        <a class="last-page" title="Go to the last page" href="<?php print admin_url('admin.php?' . http_build_query(array(
                'page' => 'customer-survey-results',
                'pagination' => $max_page,
                'sort' => $sort,
                'dir' => $dir,
            ))); ?>">»</a>
    </span>
</div>
<br class="clear">
</div>

</div>

// CSV页面

define('CUSTOMER_SURVEY_LIMIT', 10);
define('CUSTOMER_SURVEY_DB', 'customer_survey');
define('CUSTOMER_SURVEY_PRODUCT_DB', 'customer_survey_product_rating');
define('CUSTOMER_SURVEY_SERVICE_DB', 'customer_survey_service_rating');
define('CUSTOMER_SURVEY_MEMBER_DB', 'customer_survey_member_rating');

$variables = customer_survey_variables();

$sql = "SELECT * FROM `" . CUSTOMER_SURVEY_DB . "` cs WHERE 1=1;";

$results = $wpdb->get_results($sql);
foreach ($results as &$result) {
$result->products = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" .     CUSTOMER_SURVEY_PRODUCT_DB . "` WHERE customer_survey_id = %d", $result->id));
$result->services = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . CUSTOMER_SURVEY_SERVICE_DB . "` WHERE customer_survey_id = %d", $result->id));
$result->members = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . CUSTOMER_SURVEY_MEMBER_DB . "` WHERE customer_survey_id = %d", $result->id));
}

header('Content-Type: text/csv' );
header('Content-Disposition: attachment;filename=survey_results.csv');

$headers = array(
'Badge Number',
'Email',
'IP Address',
'Date Submitted',
);

foreach ($variables as $category => $ratings) {
foreach ($ratings as $key => $label) {
    $headers[] = $label;
}
}
$fp = fopen('php://output', 'w');
fputcsv($fp, $headers);

foreach ($results as $index => $result) {
$ratings = array(
    'product' => array_fill_keys(array_keys($variables['products']), 'N/A'),
    'service' => array_fill_keys(array_keys($variables['services']), 'N/A'),
    'member' => array_fill_keys(array_keys($variables['members']), 'N/A'),
);

$row = array(
    !empty($result->customer_badge) ? $result->customer_badge : 'Not given',
    !empty($result->customer_email) ? $result->customer_email : 'Not given',
    $result->ipaddress,
    date("m/d/y g:i A", strtotime($result->created_at)),
);

  foreach ($ratings as $type => $rating) {
    if (!empty($result->{$type . 's'})) {
        foreach ($result->{$type . 's'} as $value) {
            $rating[$value->{$type}] = $value->rating;
        }
    }
    $row += $rating;
    }
  fputcsv($fp, $row);
}

0 个答案:

没有答案