cakephp logic - 获得相同结果的其他方法

时间:2013-03-22 00:52:01

标签: php cakephp cakephp-1.3 cakephp-2.0 cakephp-1.2

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

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

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

QNS 1.还有另一种方法可以实现这一结果吗?

QNS 2.我可以在控制器中执行所有操作吗?如果是这样,怎么样?

CONTROLLER

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 OUTPUT

  <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>

1 个答案:

答案 0 :(得分:1)

首先,你为什么“拥有”才能使用requestAction?您根本不遵循蛋糕约定。您需要花一点时间阅读文档... Imo,您希望将“Score”模型与“Customer”模型相关联,并将两者合二为一。

<?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个Scores。

<?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] => ...
        //)
    }
}

?>

我写得非常快,请原谅任何错误或错误的假设。希望这有助于解决您的问题。