help with template loop

A general technical discussion area for patTemplate.
Locked
MrMoney
Joomla! Apprentice
Joomla! Apprentice
Posts: 39
Joined: Mon Nov 21, 2005 9:33 pm

help with template loop

Post by MrMoney » Sat Nov 26, 2005 7:36 pm

Hi,
I would like to loop through a database result set inserting the data as select options.
But it is not recognizing the template, as it is an html file.

For example, here is the html template file...

Add New Group





{GROUP_NAME}






and here is the php file that loads it...

function addGroup( $parentRows ) {
        // import the body of the page
        $tmpl =& groupScreens::createTemplate();
        $tmpl->setAttribute( 'body', 'src', 'add_group.html' );
       
// Replace placeholder var       
        foreach( $parentRows as $row ) {
$tmpl->addVar( "parents", "GROUP_ID", $row['group_id'] );
$tmpl->addVar( "parents", "GROUP_NAME", $row['name'] );
$tmpl->parseTemplate( "parents", "a" );
        }
       
        $tmpl->displayParsedTemplate( 'form' );       
    }

How can I loop through adding the options?

Thanks

User avatar
ianmac
Joomla! Virtuoso
Joomla! Virtuoso
Posts: 4784
Joined: Sat Sep 24, 2005 11:01 pm
Location: Toronto, Canada

Re: help with template loop

Post by ianmac » Sun Nov 27, 2005 6:04 am

Try...
        $options = array();

        foreach($parentRows as $row) {
            $options[] = array (
                    'group_id' => $row['group_id'],
                    'group_name' => $row['name']
            );
        }
        $tmpl->addRows( 'parents', $options);   


Ian

wene
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 203
Joined: Sat Aug 20, 2005 1:40 am
Location: south of France

Re: help with template loop

Post by wene » Sun Nov 27, 2005 3:41 pm

hello
i do like this :

html :

Code: Select all

<select name="myselectlist">
    <mos:tmpl name="selectlist">
        <option value="{SEL_ID}" {SEL_SELECTED}>{SEL_NAME}</option>
    </mos:tmpl>
</select>
php :

Code: Select all

//------------- finish to get the values -------------------
$rows = $database->loadObjectList();
if ($rows){
    //----------- assign the "selected" --------------------
    for ($i=0;$i<count($rows);$i++){
        if ($rows[$i]->id == $another_id){
        $rows[$i]->selected = "selected";
        }
    }
}
//------------- assign values to the template ---------------
$tmpl-> addObject( 'selectlist', $rows, 'sel_' );

User avatar
masterchief
Joomla! Hero
Joomla! Hero
Posts: 2316
Joined: Fri Aug 12, 2005 2:45 am
Location: Brisbane, Australia
Contact:

SOLUTION: help with template loop

Post by masterchief » Fri Dec 09, 2005 3:15 am

There are some built-in functions to help you out here:

Code: Select all

$selectedOption = 'some value';
patHTML::selectArray( $parentRows, $selectedOption, 'group_id', 'selected' );
$tmpl->addRows( 'parent', parentRows );
then your template becomes

Code: Select all

<select name='PARENT' size='1'>
<mos:tmpl name="parents">
   <option value="{GROUP_ID}" {SELECTED}>{GROUP_NAME}</option>
</mos:tmpl>
</select>
Yes-no radio sets are also dead easy, for example:

Code: Select all

patHTML::yesNoRadio( $tmpl, 'body', 'published_radio', $row->published );
Then in your template:

Code: Select all

Published: {PUBLISHED_RADIO}
Hope this helps.
Andrew Eddie - Tweet @AndrewEddie
<><
http://eddify.me
http://www.kiva.org/team/joomla - Got Joomla for free? Pay it forward and help fight poverty.

User avatar
Websmurf
Joomla! Hero
Joomla! Hero
Posts: 2230
Joined: Fri Aug 19, 2005 2:23 pm
Location: The Netherlands
Contact:

Re: help with template loop

Post by Websmurf » Sat Dec 10, 2005 1:48 pm

masterchief wrote: There are some built-in functions to help you out here:
Andrew is there documentation available about these functions or will I just have to look in the code? I've looked at the help site, but didn't really find anything.
Adam van Dongen - Developer

- Blocklist, ODT Indexer, EasyFAQ, Easy Guestbook, Easy Gallery, YaNC & Redirect -
http://www.joomla-addons.org - http://www.bandhosting.nl


Locked

Return to “patTemplate”