dqeonr8554 2014-04-04 13:50
浏览 21
已采纳

PHP PFBC选择值与显示不同(PHP表单构建类)

PFBC is a simple yet totally undocumented framework that is really useful to get started, it is less complex than other frameworks and looks really nice Out of the box.

http://code.google.com/p/php-form-builder-class/

I have the following two arrays in PHP:

$area = [0=>"name", 10=>"name2", 11=>"name3"];
$ppl = [0=>"name", 1=>"name2", 2=>"name3"];

I want to use them as select, where the user will be able to choose between the names. This is the code I use for each:

$form->addElement(new Element\Select(htmlentities("Area type:"), 
    "area", $area, array("required" => 1)
));

$form->addElement(new Element\Select(htmlentities("Person:"), 
    "ppl", $ppl, array("required" => 1)
));

I was expecting to have this:

<select id="area" required="" name="area">
    <option value="1">
       name
    </option>
    <option value="10">
        name2
    </option>
    <option value="11">
        name3
    </option>
</select>

Wich i got for the first array ($area) but for the second array ($ppl) i've got:}

<select id="ppl" required="" name="ppl">
    <option value="name">
       name
    </option>
    <option value="name2">
        name2
    </option>
    <option value="name3">
        name3
    </option>
</select>

-- I need the numeric code as value since i will use what the user chooses to query a database by that id

Any ideas of what might happen?

  • 写回答

4条回答 默认 最新

  • dounaoji2054 2014-04-04 13:50
    关注

    So i found the problem:

    Somwhere along the way while PFBC creates the Select element, it uses the third parameter (in my example, $area or $ppl) to generate an internal property called options, probably to cover itself against single arrays of the type ["name", "name2", "name3"], inside OptionElement.php the following code is causing problems.

    public function __construct($label, $name, array $options, array $properties = null) {
        $this->options = $options;
        if(!empty($this->options) && array_values($this->options) === $this->options)
            $this->options = array_combine($this->options, $this->options);
            parent::__construct($label, $name, $properties);
    }
    

    The error is here: array_values($this->options) === $this->options in my second array, $ppl, i have a perfectly indexed table starting from zero

    $ppl = [
        0 => "name",
        1 => "name2",
        2 => "name3"
    ]
    

    this triggers the control to think i have a simple array instead of custom keys, as: $array_values($ppl) === $ppl returns boolean(true)

    The second array, $area is different, since it has missing keys:

    $area = [
        0 => "name",
        10=> "name2",
        11=> "name3"
    ]
    

    So $array_values($area) === $area returns boolean(false)

    My solution, before touching PFBC (might be a bug, there has to be a better way to detect this case):

    Change the database so $ppl starts from 1 instead of 0

    $ppl = [
        1 => "name",
        2 => "name2",
        3 => "name3"
    ]
    

    Incidentally then $array_values($ppl) === $ppl returns boolean(false)

    And now my Select works as i expected:

    <select id="person" required="" name="person">
        <option value="1">
           name
        </option>
        <option value="2">
            name2
        </option>
        <option value="3">
            name3
        </option>
    </select>
    

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)