[SOLVED] CreateInputRadioGroup and Smarty foreach

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
nervino
Power Poster
Power Poster
Posts: 448
Joined: Sun Dec 28, 2008 12:15 pm
Location: Roma, Italy

[SOLVED] CreateInputRadioGroup and Smarty foreach

Post 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
Last edited by nervino on Tue Sep 25, 2012 4:49 pm, edited 1 time in total.
Duketown

Re: CreateInputRadioGroup and Smarty foreach

Post 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
nervino
Power Poster
Power Poster
Posts: 448
Joined: Sun Dec 28, 2008 12:15 pm
Location: Roma, Italy

Re: CreateInputRadioGroup and Smarty foreach

Post 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.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: [SOLVED] CreateInputRadioGroup and Smarty foreach

Post 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.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
Post Reply

Return to “Developers Discussion”