在编辑页面显示选定的单选按钮 [英] In edit page show selected radio button

查看:44
本文介绍了在编辑页面显示选定的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的编辑页面上,如果 $subscribedrips 等于 Yes 或 No,我将如何显示正确的单选按钮?这是我所拥有的,但它不起作用:

On my edit pages, how would I show the correct radio button if for $subscribedrips is equal to Yes or No? Here is what I have and its not working:

 if ($row['subscribedrips'] == Yes) { 
  echo 
  '<input type="radio" name="subscribedrips" value="Yes" CHECKED /> Yes 
  <input type="radio" name="subscribedrips" value="No" /> No';
  }
  elseif ($row['subscribedrips'] == No) {
  echo 
  '<input type="radio" name="subscribedrips" value="Yes" /> Yes 
  <input type="radio" name="subscribedrips" value="No" CHECKED/> No';
  }
  elseif (empty($row['subscribedrips'])) {
  echo 
  '<input type="radio" name="subscribedrips" value="Yes" CHECKED/> Yes 
  <input type="radio" name="subscribedrips" value="No" /> No';
  }

推荐答案

类似这样的事情

printf('<input type="radio" name="subscribedrips" value="Yes" %s /> Yes'."\n", 
    ($subscribedrips == 'Yes' ? 'CHECKED' : ''));
printf('<input type="radio" name="subscribedrips" value="No" %s /> No'."\n",   
    ($subscribedrips == 'No'  ? 'CHECKED' : ''));

有效.它使用三元运算符根据 $subscribedrips 的值将 'CHECKED' 或空字符串插入到输入标签中.

works. It uses the ternary operator to either insert 'CHECKED' or an empty string into the input tag, based on the value of $subscribedrips.

您也可以使用更详细的方式,例如使用 switch:

You could also do in a more verbose manner, for example with switch:

$sel_y = '';
$sel_n = '';
switch($subscribedrips)
{
    case 'Yes':
        $sel_y = 'CHECKED';
        break;
    case 'No':
        $sel_n = 'CHECKED';
        break;
    default:
        // Neither need to be changed, so we dont even need this branch
        break;
}

printf('<input type="radio" name="subscribedrips" value="Yes" %s /> Yes'."\n", $sel_y);
printf('<input type="radio" name="subscribedrips" value="No" %s /> No'."\n",   $sel_n); 

个人喜好.

更新片段 1

printf('<input type="radio" name="subscribedrips" value="Yes" %s /> Yes'."\n", 
    ((array_key_exists('subscribedrips', $row) && $row['subscribedrips'] == 'Yes') ? 'CHECKED' : ''));
printf('<input type="radio" name="subscribedrips" value="No" %s /> No'."\n",   
    ((array_key_exists('subscribedrips', $row) && $row['subscribedrips'] == 'No')  ? 'CHECKED' : ''));

更新的代码段 2

$sel_y = '';
$sel_n = '';
if(array_key_exists('subscribedrips', $row))
{
    switch($row['subscribedrips'])
    {
        case 'Yes':
            $sel_y = 'CHECKED';
            break;
        case 'No':
            $sel_n = 'CHECKED';
            break;
        default:
            // Neither need to be changed, so we dont even need this branch
            break;
    }
}

printf('<input type="radio" name="subscribedrips" value="Yes" %s /> Yes'."\n", $sel_y);
printf('<input type="radio" name="subscribedrips" value="No" %s /> No'."\n",   $sel_n); 

<小时>

关于您的最后一个问题,我们的方法之间的区别非常简单,但再次(啊!)它是一种风格选择,两者都实现了相同的目标,两种方法都用于生产"PHP 代码.


Regarding your last question, the difference between our approaches is pretty simple, but once again (ahh!) its a style choice, both accomplish the same goal, both methods are used in "production" PHP code.

我的示例在 PHP 中构建整个输入标签并打印它.Valentinas 的方法是从 PHP 字符串中提取静态文本并将其直接放入 HTML 中.

My example builds the entire input tag in PHP and prints it. Valentinas' approach pulls the static text out of the PHP strings and puts it directly into HTML.

例如,以下几行都将产生相同的输出:

For example, the following lines will all result in the same output:

<?php printf("<strong>%s</strong>", $some_string); ?>

<?php echo "<strong>$some_string</strong>"; ?>

<?php echo "<strong>".$some_string."</strong>"; ?>

<strong><?php echo $some_string; ?></strong>

我怀疑这两种方法之间是否存在显着的性能差异,但我将强调一个外观差异.

I'm doubtful there is any significant performance difference between the two methods, but there is one cosmetic differences that I'll highlight.

  • 语法高亮 - 如果您使用带有语法高亮的编辑器,valentinas 的方法将允许语法高亮器适当地高亮 input 标签及其属性.使用我的方法,整个字符串将被突出显示相同.这是一张截图,显示了 notepad++ 如何突出显示这两种方法.

  • Syntax highlighting - If you use an editor with syntax highlighting, valentinas' approach will allow the syntax highlighter to appropriately highlight the input tag and its attributes. Using my approach, the entire string would be highlighted the same. Here is a screenshot showing how notepad++ highlights the two methods.

如您所见,valentinas 的方法产生了更丰富多彩的显示,这有助于识别和追踪错误.

As you can see valentinas' approach results in a more colorful display, which could help identify and track down errors.

如果您想有条件地打印整个标签,在如何格式化代码方面存在一些细微的差异,但它们并不真正值得讨论——在我看来,最大的是语法突出显示.

There are some subtle differences when it comes to how your code has to be formatted if you want to conditionally print the entire tag, but they're not really worth talking about -- the biggest, in my opinion, is the syntax highlighting.

这篇关于在编辑页面显示选定的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆