That's how I got it to work, but without the Tales-Keyword. I just added a IF statement to my SQL statement that retrieved the categories. Kind of hackish, if you ask me, but it works! Thanks for all your replies!

----- Original Message -----
Date: Tue, 28 Apr 2009 12:45:43 +0200
From: Julian Haupt <julian.ha...@hauptmedia.de>
Subject: Re: [PHPTAL] Selected attribute in select list
To: phptal@lists.motion-twin.com
Message-ID: <1240915543.9147.27.ca...@superwash-2000>
Content-Type: text/plain

Hi,

The way you USA should work, but try this:

<tal:block tal:repeat="c categories">
     <option tal:attributes="value c/id;selected php: f.categoryid ==
c.id" tal:content="c/category"></option>
</tal:block>

In general it might be a bad idea to use php code and explicit
comparisons in the templates. This is a bad practice which entangles the
view with the underlying business logic.

I've added a new TALES-Keyword for this purpose: condition

It works like mysql's IF statement. If the expression in the first block
is true, execute the next block, otherwise the last block.

function phptal_tales_condition($expression, $nothrow) {
       // split OR expressions
       $exps = preg_split('/\s*\|\s*/sm', $expression);

       if (!isset($exps[1])) {
               $exps[1] = 'nothing';
       }

       if (!isset($exps[2])) {
               $exps[2] = 'nothing';
       }

       return '(' . phptal_tales($exps[0], $nothrow) . ') ? ' .
phptal_tales($exps[1], $nothrow) . ' : '. phptal_tales($exps[2],
$nothrow);
}


It might not be perfect, because it uses TALES' "|" operator for a
completely different operation.

Please note: as you cannot do explicit comparisons with this keyword,
you must define the result of the comparison in your model.

With the help of the condition keyword you can write the above template
like this:

<tal:block tal:repeat="c categories">
   <option tal:attributes="value c/id; selected condition: c.active|
string:selected|not:true" tal:content="c/category"></option>
</tal:block>

The "active" property might be determined like this:

$id = (int)$_REQUEST["id"]; // get the id in the url query

$formSQL = "SELECT i.id, categoryid, category, title, publication,
DATE_FORMAT(date, '%Y/%m/%d') AS fDate, author, summary, filename,
IF(i.id=$id,1,NULL) AS active FROM `resources-items` AS i LEFT JOIN
`resources-categories` AS c ON i.categoryid=c.id WHERE i.id=$id"; // get
the row to be edited

$t = new PHPTAL("test.html");
$t->categories = getRows("SELECT id, category FROM
`resources-categories` ORDER BY category"); // get all categories
$t->form = getRows($formSQL);


Greetings,

Julian Haupt



_______________________________________________
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal

Reply via email to