Вопрос

I want to validate or show the radio button checked,if some value is present in database. Please refer my views code,

<input type="radio" name="bothadvandlor"  value="1" <?php if($advs->is_checked == "1");?>checked="checked"<?php endif;?> class="advs"/> Show Google Ads<br/>
<input type="radio" name="bothadvandlor"  value="2" <?php if($advs->is_checked == "2");?>checked="checked"<?php endif;?> class="advs"/> Others

The above is the form i tried,but i am getting the syntax error message like "ErrorException [ Parse Error ]: syntax error, unexpected T_ENDIF" .

Need help to solve this.

Это было полезно?

Решение 2

You need to use colon : instead of semi-colon ; here after your if condition:

if($advs->is_checked == "1"):
// -----------------------  ^ here

if($advs->is_checked == "2"):
// -----------------------  ^ and here   

From the docs:

PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively.

Другие советы

Should be colon :,

if($advs->is_checked == "1"):
                            ^

Reference.

<input type="radio" name="bothadvandlor"  value="1" <?php if($advs->is_checked == "1"):?>checked="checked"<?php endif;?> class="advs"/> Show Google Ads<br/>
<input type="radio" name="bothadvandlor"  value="2" <?php if($advs->is_checked == "2"):?>checked="checked"<?php endif;?> class="advs"/> Others

I just wanted to add that Kohana has got some neat stuff for this:

public static radio( string $name [, string $value = NULL , boolean $checked = bool FALSE , array $attributes = NULL ] )

e.g.

<?php echo Form::radio('bothadvandlor', 1, ($advs->is_checked == '1'), array('class' => 'advs')); ?> Show Google Ads <br />

I think it makes these kinds of operations a whole lot easier. More separation of HTML and PHP thus better readability.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top