1

我已经尝试解决这个问题好几天了。如果可能,请协助或建议其他方法。我可以在 chrome、safari 和 firefox 中显示每个学生 10 节课的总分。但是,我在 IE9/10 中收到此错误“无法显示此页面”。

我厌倦了调试,发现当我通过 requestAction 使用 for 循环时,IE9/10 显示上述错误。话虽如此,我会要求 requestAction 获得总分。

为了获得总分,我依赖于 for each 循环( foreach ($customers as $customer) { ... } )来获取每个 $customer['Customer']['id'] 并将其传递给 requestAction 并返回回分数结果。

QNS 1. 有没有其他方法可以达到这个结果?

QNS 2. 我可以在控制器中做所有事情吗?如果是这样,怎么做?

控制器

function eachlesson($lessonid, $sessionkey, $customer_id) {

return $this->Score->find('first', array('conditions' => array('Score.test_bk_session_id' => $sessionkey, 'Customer.customers_types' => 'student', 'Score.lesson' => $lessonid, 'Score.customer_id' => $customer_id)));

}

看法

<table>
<?php foreach ($customers as $customer) { ?>
<tr>
   <td>
      <?php echo $customer['Customer']['customers_name']; ?>
   </td>
   <td>
   <?php 
   $customer_id = $customer['Customer']['id'];
   $sessionkey = $this->params['pass'][1];

   //LOOP THROUGH 10 TIMES TO GET LESSON 1 - 10 SCORES
   for ($i=1; $i<=10; $i++) { 
       $lessonid = $i;
       $score = $this->requestAction('/scores/eachlesson/'.$lessonid."/".$sessionkey."/".$customer_id);


   //GETTING THE TOTAL SCORE FOR LESSON 1 TO 10
   (int)${'totaleachlesson'.$i} = $score['Score']['BI_pts'] + $score['Score']['FD_pts'] + $score['Score']['PO_pts']  + $score['Score']['WW_pts'] + $score['Score']['MG_pts'] + $score['Score']['FO_pts'];

   }

    //ADDING THE TOTAL SCORE OF THE 10 LESSONS
   $figureofcorrecttotal = $totaleachlesson1 + $totaleachlesson2 + $totaleachlesson3 + $totaleachlesson4 + $totaleachlesson5 + $totaleachlesson6 + $totaleachlesson7 + $totaleachlesson8 + $totaleachlesson9 + $totaleachlesson10;

   //DISPLAY THE TOTAL SCORE
   echo $figureofcorrecttotal;

   ?>
   </td>
</tr>
<?php } ?>
</table>

HTML 输出

  <table class="tablesorter summary3pt2">
        <thead>
        <tr> 
                <th width="170" style="padding-right:5px;" class="empty">Name</th> 
                <th width="120" class="header">No of Correct</th> 

            </tr>
          </thead>
          <tbody>
                        <tr>
            <td class="bold" align="right">
            Drew Parsons                </td>
            <td>
         2                </td>
            </tr>
                            <tr>
            <td class="bold" align="right">
            Natasha Francis                </td>
            <td>
         0                </td>
            </tr>
                            <tr>
            <td class="bold" align="right">
            Johanna Harmon                </td>
            <td>
         0                </td>
            </tr>
                            <tr>
            <td class="bold" align="right">
            Aubrey Mckenzie                </td>
            <td>
         0                </td>
            </tr>
                            <tr>
            <td class="bold" align="right">
            Edith Sims                </td>
            <td>
         0                </td>
            </tr>
                            <tr>
            <td class="bold" align="right">
            Brandy Ruiz                </td>
            <td>
         0                </td>
            </tr>
                            <tr>
            <td class="bold" align="right">
            Toni Marshall                </td>
            <td>
         0                </td>
            </tr>
                            <tr>
            <td class="bold" align="right">
            Cedric Nash                </td>
            <td>
         0                </td>
            </tr>
                            <tr>
            <td class="bold" align="right">
            Penny Maldonado                </td>
            <td>
         0                </td>
            </tr>
                            <tr>
            <td class="bold" align="right">
            Brandi Perry                </td>
            <td>
         0                </td>
            </tr>
                            <tr>
            <td class="bold" align="right">
            Conrad Hogan                </td>
            <td>
         0                </td>
            </tr>
                            <tr>
            <td class="bold" align="right">
            Travis Sparks                </td>
            <td>
         0                </td>
            </tr>
                            <tr>
            <td class="bold" align="right">
            Winifred Watson                </td>
            <td>
         0                </td>
            </tr>
                            <tr>
            <td class="bold" align="right">
            Shannon Strickland                </td>
            <td>
         0                </td>
            </tr>
                        </tbody> 

        </table>
4

1 回答 1

1

首先,为什么你“必须”使用 requestAction 您根本没有遵循蛋糕约定。您需要花一点时间阅读文档... Imo,您希望将“评分”模型与“客户”模型相关联,并在一次调用中包含两者。

<?php
// Customer model.

class Customer extends AppModel {
    var $name = 'Customer';

    var $hasMany = array(
        'Score' => array(
            'className' => 'Score',
            'foreignKey' => 'customer_id',
            'limit' => 10,
            'conditions' => array(
                '<conditions go here>'
            )
        )
    );
}

?>

<?php
// Score model.

class Score extends AppModel {
    var $name = 'Score';

    var $belongsTo = array(
        'Customer' => array(
            'className' => 'Customer',
            'foreignKey' => 'customer_id' // This assumes you have a customer_id field in the 'scores' table.
        )
    );
}

?>

现在模型已关联,如果您没有将可包含的行为附加到它们,您可以在每个模型或主 app_controller.php 中添加:

var $actsAs = array('Containable');

现在您所要做的就是在对 Customer 模型执行查找时包含 Score 模型,系统会自动为您检索 10 个分数。

<?php
// customers_controller.php

class CustomersController extends AppController {
    var $name = 'Customers';

    function index() {
        // Contain the child method.
        $this->Customer->contain(array(
            'Score' => array(
                'conditions' => array(
                    'Score.test_bk_session_id' => $sessionkey,
                    'Score.lesson' => $lessonid
                )
            )
        ));
        $customers = $this->Customer->find('all', array(
            'conditions' => array(
                'Customer.customers_types' => 'student'
            )
        ));
        // Now you have all your customers, AND their associated
        // scores. No more request action needed (never use that imo)
        $this->set('customers', $customers);

        // Your array should look like this:
        // $customers =
        //array(
        //    [0] => array(
        //        'Customer' => array(<CustomerArray>),
        //        'Score' => array(<ScoreArray>)
        //    ),
        //    [1] => ...
        //)
    }
}

?>

我写得很快,所以请原谅任何错误或错误假设。希望这对您的问题有所帮助。

于 2013-03-25T21:43:36.127 回答