CodeIgniter Forums

Full Version: validation, radio button, and loops?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]alanphil[/eluser]
In my view I'm creating a list of concerts, like this:

Code:
<?php foreach ($concerts as $row): ?>

&lt;input type="radio" name="concert_selection" value="&lt;?=$row-&gt;id?&gt;">&nbsp;&lt;?=$row->concert_name?&gt;, &lt;?=$row->concert_date?&gt;, &lt;?=$row->count?&gt; pair(s) of tickets available<br />

&lt;?php endforeach; ?&gt;


I'm trying to add validation to keep the selected radio button selected, if some other field fails validation. I've tried many variations of the following from the user guide:

Code:
&lt;input type="radio" name="myradio" value="1" &lt;?php echo $this-&gt;validation->set_radio('myradio', '1'); ?&gt; />

When I try the following, the radio button isn't set after validation:

Code:
&lt;?php echo $this->validation->set_radio('concert_selection', "&lt;?=$row->id?&gt;"); ?&gt;

I've tried many variations of the above. What am I missing?

Alan

El Forum

[eluser]Thorpe Obazee[/eluser]
maybe you can use

Code:
if ($_POST) {
// get post variables
echo $_POST['name'];
}else {
// get database results
echo $db_result->name;
}

something like that...

El Forum

[eluser]alanphil[/eluser]
Thanks Chamyto, but if possible, I'd like to use the set_radio method as defined in the user guide. Is there an issue with using dynamic data in the set_radio method?

Thanks,
Alan

El Forum

[eluser]Thorpe Obazee[/eluser]
Code:
if ($_POST) {
// get post variables
echo $_POST['name'];
}else {
// get database results
echo $db_result->name;
}

the above won't work on radio buttons. you will need to echo a 'selected' so.... set_radio()

Code:
&lt;?php foreach ($clients->result() as $client):?&gt;
<option value="&lt;?php echo $client->id;?&gt;" &lt;?php if ($_POST) { echo $this->validation->set_radio('client', $invoice_detail->client_id);} else { echo( $invoice_detail->client_id == $client->id) ? 'selected' : '';}?&gt;>&lt;?php echo $client->name; ?&gt;</option>
&lt;?php endforeach;?&gt;

the above is if there is something selected from the database already.

Code:
&lt;?php foreach ($clients->result() as $client):?&gt;
<option value="&lt;?php echo $client->id;?&gt;" &lt;?php  echo $this->validation->set_radio('client', $invoice_detail->client_id);?&gt;>&lt;?php echo $client->name; ?&gt;</option>
&lt;?php endforeach;?&gt;

this one is if there is none.

El Forum

[eluser]Thorpe Obazee[/eluser]
I reviewed your code a bit and saw something.

[quote author="alanphil" date="1220043107"]In my view I'm creating a list of concerts, like this:

Code:
[code]&lt;?php echo $this->validation->set_radio('concert_selection', "&lt;?=$row->id?&gt;"); ?&gt;

I've tried many variations of the above. What am I missing?

Alan[/quote]


Code:
&lt;?php echo $this->validation->set_radio('concert_selection', $row->id); ?&gt;

I believe that's what you were missing

El Forum

[eluser]alanphil[/eluser]
Thanks Chamyto! I thought it might be something I overlooked, and having you review this was much appreciated. Works great now!

Alan