I just want to toss in my 2c here.
If you end up using PDO, you might consider just returning the statement
handle from your model function, rather than an array produced by
PDOStatement::fetchAll(). PDO supports iterators, so you can do
something like this in your model:
function getFoo($barVal)
{
$stmt = 'SELECT foo FROM baz WHERE bar = :bar';
$stmt->bindParam(':bar', $barVal);
$stmt->execute();
return $stmt;
}
Set the value in your view:
$this->setAttribute('fooHnd', getFoo('wally'));
Then from your template, you can just iterate through the values like so:
foreach ($template['fooHnd'] as $fooRecord) {
// do something with $fooRecord
}
By returning the PDOStatement handle, you're basically returning a
database cursor. If your dataset is large, this can result in much lower
PHP memory usage than doing a fetchAll() into an array. You could also
just do something like:
while ($fooRecord = $template['fooHnd']->fetch()) {
// do someting with $fooRecord
}
but iterators are cooler ;-)
PS: If you must pass an array back, consider using setFetchMode() with
FETCH_ASSOC or FETCH_NAMED - it can halve the size of the array returned
by fetchAll().
David Zülke wrote:
> Ah okay.
>
> First: NEVER NEVER NEVER NEVER use sql in templates! You're using the
> model correctly already to retrieve the list, but put the
> mysql_fetch_array call into the model, too, and make it return an
> array of ids and values:
>
> <select name="qstID">
> <option>Select</option>
> <?php foreach($template['qstlist'] as $id => $value): ?>
> <option value="<?=$id?>"><?=$value?></option>
> <?php endforeach; ?>
> </select>
>
>
> The reason why your select list was empty on form submit is because
> the action's executeRead() method runs only on GET, not on POST, so
> add the getModel()->blah call to executeWrite(), too, or put it into
> a common method that you call from both executeRead() and executeWrite
> ().
>
>
> But back to the form. You don't have to select elements by hand or
> fill in text field values. You can use the form population filter. It
> automatically runs on POST (have you enabled it?), but you can make
> it run on any other request method, too. In your example, you can do
> this in the view:
>
> $this->context->getRequest()->setAttribute('populate', new
> AgaviParameterHolder(array(
> 'qstID' => $userDt['qstID'],
> 'textfieldname' => 'value',
> 'foo' => 'bar',
> )), 'org.agavi.filter.FormPopulationFilter');
>
> that will always run, however, also on POST, which you likely don't
> want (there, you want to show the data submitted again, because an
> error occured), so you should wrap that line into an if block:
>
> if($this->context->getRequest()->getMethod() == 'read') {
> }
>
>
> Hope that helps,
>
> David
>
>
> P.S: you really should consider using PDO instead of mysql_*, Agavi
> has a driver for PDO, too.
>
>
>
>
> Am 14.03.2007 um 09:19 schrieb surej ns:
>
>> HI David Zülke ,
>>
>> Just see my code. I have written this code to populate list in a
>> select box from DB.
>> Here is the code
>>
>> Template file
>> <select name= "qstID" >
>> <option value ="0" >Select</option>
>> <?
>> while( $row =mysql_fetch_array( $template['qstlist' ]) )
>> {
>> if($userDt ['qstID']== $row['qstID' ]){
>> echo"<option value=". $row[ 'qstID']." selected >" .$row
>> ['qst']. "</option>";
>> }
>> else{
>> echo"<option value=". $row[ 'qstID'].">" .$row[ 'qst']."</
>> option>" ;
>> }
>> }
>> ?>
>> </select>
>>
>> Action file
>> public function executeRead(AgaviRequestDataHolder $rd)
>> {
>> $list = $this->getContext()->getModel( 'GetQstLst', 'Default')-
>>> GetQstLst();
>> $this->setAttribute('list' ,$list );
>> }
>> View File
>> public function executeHtml(AgaviRequestDataHolder $rd)
>> {
>> parent::setupHtml( $rd);
>> $this->setAttribute('qstlist' ,$this ->getAttribute('list'));
>>
>> }
>> Now my issue is..
>> I have a select box in which value is generated through DB. When
>> the page loads for the first time the select box is filled. When I
>> press submit button. The validation occurs and the error message
>> comes at top. At this time the select box gets Blank or the list
>> gets empty. I have provided validation only to a text box and not
>> to this select box.
>> How can I retain the value after validation in select box?
>>
>> Hope u all understand my issue..
>>
>> Thax
>>
>> surej
>> _______________________________________________
>> users mailing list
>> [email protected]
>> http://lists.agavi.org/mailman/listinfo/users
>
>
> _______________________________________________
> users mailing list
> [email protected]
> http://lists.agavi.org/mailman/listinfo/users
_______________________________________________
users mailing list
[email protected]
http://lists.agavi.org/mailman/listinfo/users