简体   繁体   中英

Errors in Controller - Zend

I am working on a small application and I have a problem with my controllers_viewpvcontroller . I'm trying to display a report. The user can decide the year and the month and after submitting them, the report will be displayed in the same page under the form.

Here is my controller:

require_once ('library/Zend/Controller/Action.php');

class controllers_viewpvController extends Zend_Controller_Action {

public function init()
{

}

public function viewpvAction()
{
    require_once 'models/PastPV.php';

    $data = $this->getRequest()->getParams();
    $year = $data['year'];
    $month = $data['month'];
    $pv = new PastPV();
    $return = $pv->viewPV($year, $month); // this function is working fine.
    $this->view->assign('values',$return);

}

public function getpvAction()
{


}

}

Here is my viewpv.phtml :

<body>

<h1 id="h2"> Review</h1>

<br><div><center><form action="../Viewpv/viewpv" method="post" >

<h3>Payment Vouchers for : <select name="month">
            <option value="null">(Month)</option>
    <option value="01">January</option>
    <option value="02">February</option>
    <option value="03">March</option>
    <option value="04">April</option>
    <option value="05">May</option>
    </select>

<select name="year">
<option value="null">(Year)</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>                                                                              
</select></h3>
<br><br>
<input type="submit" value="View">

</form></center>
</div></body>

<!--the codes to display the form are below-->

<?php 
$return = $this->values;
if(!empty($return))
$tbl = '<table><tr><td>Document No</td></tr>';
while($data = $return->fetchRow()){       //here I think 'fetchRow()' may cause a problem.
                                              //but that is not my question.
$tbl.='<tr><td>'.$data['docNo'].'</td></tr>';
}
$tbl.='</table>';
echo $tbl;
?>

when I try to load the page, it gives the following php errors:

Undefined index: year
Undefined index: month

Also, it gives a Zend_Db_Statement_Exception, No parameters were bound . What I feel is, I have not found the correct way to do this.

Please help me with this. If there are better ways to achieve this, please let me know.

If the form is on the same page as the report you want displayed, you will need to check for POST data. You can do this by using the request object in Zend.

if($this->getRequest()->isPost())
{
  //Process the form and get things you need to display the report
}

Currently, if you go straight to that page like it sounds like you're doing, you're trying to look up indices that have not yet been submitted back to the action.

So this will fail causing your error:

$year = $data['year'];
$month = $data['month'];

Then you're doing a DB look-up on that information, causing the DB error. If you add the "if" statement I listed, you should be fine.

You should read the Zend documentation for Zend Form and The Request Object . Zend Form can make your life so much easier. It is also good practice to only do "display" things in views. Doing DB look-ups in the view should be discouraged.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM