Page 1 of 1

[SOLVED] CreateInputRadioGroup and Smarty foreach

Posted: Tue Sep 25, 2012 12:13 pm
by nervino
Hi, I'm trying to display a table with a radio button on each row.
I create the radio buttons in this way:

Code: Select all

$arr_ids = array();
$entryarray = array();
while ($dbresult && $row = $dbresult->FetchRow())
{
    $onerow = new stdClass();
		
	$arr_ids[''] = $row['id'];
        $onerow->name = $row['name'];
	
    $entryarray[] = $onerow;
 }
 $smarty->assign_by_ref('items', $entryarray);
 $smarty->assign('radios', $this->CreateInputRadioGroup($id, 'id', $arr_ids));
and I display the table with Smarty:

Code: Select all

<tbody>
{foreach from=$items item=entry}
	<tr>
		<td>{$radios}</td>
		<td>{$entry->name}</td>
	</tr>
{/foreach}
</tbody>
But I got the same value for all radiobutton:
<tbody>
<tr>
<td><input class="cms_radio" name="cntnt01id" id="cntnt01id1" value="3" type="radio"><label class="cms_label" for="cntnt01id1"></label></td>
<td>Jane</td>
</tr>
<tr>
<td><input class="cms_radio" name="cntnt01id" id="cntnt01id1" value="3" type="radio"><label class="cms_label" for="cntnt01id1"></label></td>
<td>John</td>
</tr>
</tbody>
Of course, I can manually write in the template:

Code: Select all

<td><input type="radio" name="{$form_id}id" value="{$entry->id}" /></td>
but is there a way to achieve the same result using CreateInputRadioGroup?

Thank you

Re: CreateInputRadioGroup and Smarty foreach

Posted: Tue Sep 25, 2012 4:09 pm
by Duketown
nervino,

You have put the {$radios} inside the loop of the smarty foreach. I think that if you push it outside of the loop you will notice that it works, although you will not see anything on the outside, since the labels will all be blank.
So either use the CreateInputRadioGroup as it should be (with $arr_ids[$row['name']] = $row['id'];)
or
use the alternative you described.

Duketown

Re: CreateInputRadioGroup and Smarty foreach

Posted: Tue Sep 25, 2012 4:49 pm
by nervino
I've put the label blank because I only need the radio buttons to be displayed; if I put {$radios} outside the loop it works, showing only the radio buttons.
I was wondering if there was a way to use the cmsms' function to reach my goal: since I can not use CreateInputRadioGroup(), I'll go with the tag.

Thank you Duketown.

Re: [SOLVED] CreateInputRadioGroup and Smarty foreach

Posted: Tue Sep 25, 2012 6:32 pm
by calguy1000
I would go with the <input type="radio" name-"{$form_id}foo" .../> method personally.

This puts more control in the realm of the 'view' (your smarty template). And is in fact what I am doing with most of my newer templates (the ones with forms).

Your 'controller' (the action file) just has to provide data to smarty, and handle the data from the form.