在yii中计算成功和失败上传的总数

时间:2016-01-07 18:23:36

标签: php html yii

我的代码现在看起来像这样。我想在页面顶部添加一个总计,用于计算成功和失败数据的总数。

<?php echo $form->labelEx($model,'Upload File'); ?>

<?php echo $form->fileField($model,'csv_file'); ?>
<?php echo $form->error($model, 'csv_file'); ?><?php echo CHtml::submitButton('Submit'); ?><?php echo $form->errorSummary($model); ?><?php $this->endWidget();if (!empty($Results)) {
echo "<h3><font face='arial'><b> Upload Results </b></h3>";
echo implode(", ", $firstLine) . "<br/>";

foreach ($Results as $result) {
    echo implode(", ", $result["data"]) . "<br/>";
    echo "<div style='padding-left: 20px'>";
    echo "<span style='color: red;'>" . $result["message"] . "</span> <br/>";

    foreach ($result["model"]->getErrors() as $attribute => $errors) {
        echo "Attribute $attribute has the following errors:" . "<br/>";
        echo "<ul>";
        foreach ($errors as $error) {
            echo "<li style='color: red;'>" . $error . "</li>";

        }
        echo "</ul>";
    }
    echo "</div>";

}
}
?>
  • 总记录
  • 总成功
  • 总失败

我该怎么做?无论如何在视图本身中都这样做了吗?

1 个答案:

答案 0 :(得分:0)

如果这可能是一个解决方案,那么

$numRecords = 0;
$numErrors = 0;
$numRecordsWithError = 0;
foreach ($Results as $result) {
    $numRecords++;

    echo implode(", ", $result["data"]) . "<br/>";
    echo "<div style='padding-left: 20px'>";
    echo "<span style='color: red;'>" . $result["message"] . "</span> <br/>";

    $flagRecErr = false;
    foreach ($result["model"]->getErrors() as $attribute => $errors) {
        if ($flagRecErr == false){
          $flagRecErr = true;
          $numRecordsWithError++;
        }
        $numErrors++;
        echo "Attribute $attribute has the following errors:" . "<br/>";
        echo "<ul>";
        foreach ($errors as $error) {
            echo "<li style='color: red;'>" . $error . "</li>";

        }
        echo "</ul>";
    }
    echo "</div>";

}
}
echo "<div>Num Records " .  $numRecords . "</div><br />";
echo "<div>Num Errors " .  $numErrors . "</div><br />";
echo "<div>Num Record with Errors " .  $numRecordsWithError . "</div><br />";