PHP Foreach 里面创建一个新对象

发布于 2024-11-15 12:53:20 字数 1849 浏览 3 评论 0原文

我对如何修改以下代码有点困惑,该代码生成一个表单,以允许 $Show->DateTimes() 自动生成一组新字段并根据情况填充价格有多少个日期可用。

我不确定这是否可能。虽然我在注释中添加了 foreach 来解释我认为它应该如何工作,但我认为我只是把我的 PHP 搞混了。

我现在有这个代码:

编辑:最终固定代码:

public function RegistrationForm() {
    $date_id = (int) $this->getRequest()->requestVar('DateID');

    if(!$date = DataObject::get_by_id("ShowDateTime", $date_id)) {
        return $this->httpError(404);
    }

    $date_map = array();
    if($Show = $date->Show()) {
        if($all_dates = $Show->DateTimes()) {
            $date_map = $all_dates->toDropdownMap('Price','DateLabel');
        }   
    }

    $fields = new Fieldset (
        new TextField('Event', _t('Show.Event','Name of Event'),$Show->Title),
        new EmailField('Name', _t('Show.Name','Name')),
        new EmailField('Email', _t('Show.Email','Email')),
        new TextField('Address', _t('Show.Address','Address')),
        new TextField('Telephone', _t('Show.Telephone','Telephone')),
        new DropdownField('DateID', _t('Show.CHOOSEDATE','Choose a show'), $date_map, $date_id)         
    );

    // Loop through the time/events creating a fieldset for: class,horse,number,price
    $i =0;
    foreach($all_dates as $show_price){
        $fields->push(new TextField('Class_'.$i, 'Class'));
        $fields->push(new TextField('Horse_'.$i, 'Horse'));
        $fields->push(new TextField('Number_'.$i, 'Number'));
        $fields->push(new CurrencyField('Price_'.$i, 'Price',$show_price->Price));
        $i++;
    }


    $form = new Form (
        $this,
        "RegistrationForm",
        $fields,        
        new FieldSet (
            new FormAction('doRegister', _t('Show.REGISTER','Register'))
        ),
        new RequiredFields('Event','Name','DateID')
    );

    return $form;
}

I'm a little confused about how I can amend the following code that generates a form to allow for the $Show->DateTimes() to automatically generate a set of new fields and populate the price depending on how many dates are available.

I'm not sure if this is even possible. Although I have added the foreach inside the comments explaining how I think it should work, I think I just have my PHP mixed up.

I have this code at the moment:

EDIT: Final fixed code:

public function RegistrationForm() {
    $date_id = (int) $this->getRequest()->requestVar('DateID');

    if(!$date = DataObject::get_by_id("ShowDateTime", $date_id)) {
        return $this->httpError(404);
    }

    $date_map = array();
    if($Show = $date->Show()) {
        if($all_dates = $Show->DateTimes()) {
            $date_map = $all_dates->toDropdownMap('Price','DateLabel');
        }   
    }

    $fields = new Fieldset (
        new TextField('Event', _t('Show.Event','Name of Event'),$Show->Title),
        new EmailField('Name', _t('Show.Name','Name')),
        new EmailField('Email', _t('Show.Email','Email')),
        new TextField('Address', _t('Show.Address','Address')),
        new TextField('Telephone', _t('Show.Telephone','Telephone')),
        new DropdownField('DateID', _t('Show.CHOOSEDATE','Choose a show'), $date_map, $date_id)         
    );

    // Loop through the time/events creating a fieldset for: class,horse,number,price
    $i =0;
    foreach($all_dates as $show_price){
        $fields->push(new TextField('Class_'.$i, 'Class'));
        $fields->push(new TextField('Horse_'.$i, 'Horse'));
        $fields->push(new TextField('Number_'.$i, 'Number'));
        $fields->push(new CurrencyField('Price_'.$i, 'Price',$show_price->Price));
        $i++;
    }


    $form = new Form (
        $this,
        "RegistrationForm",
        $fields,        
        new FieldSet (
            new FormAction('doRegister', _t('Show.REGISTER','Register'))
        ),
        new RequiredFields('Event','Name','DateID')
    );

    return $form;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沩ん囻菔务 2024-11-22 12:53:20

$form = new Form ( 中,您不能放置循环表达式。您只能在其中放置简单的表达式,例如 $variablenew

因此首先将对象列表生成到它自己的变量(或一个数组)中,然后将其添加到

Form 构造函数中 。要获取包含字段的 FieldSet 对象,您可以创建一个新对象并进行操作。向其中添加字段,然后将此对象传递到构造函数中,请考虑以下替代方法:

   $controller = $this;
   $name = 'RegistrationForm';
   $fields = new FieldSet();
   // add fields to $fields, setup $actions ...
   $form = new Form($controller, $name, $fields, $actions);

Inside the $form = new Form ( you can not place a loop expression. You can only put simple expressions in there, like a $variable or new. But nothing that needs a higher level of execution.

So first generate a list of the objects into variables of it's own (or one array) and then add it to $form.

The Form constructor expects to get a FieldSet object containing the fields. You can create a new one and add the fields to it and then pass this object into the constructor. Consider the following, alternative approach:

   $controller = $this;
   $name = 'RegistrationForm';
   $fields = new FieldSet();
   // add fields to $fields, setup $actions ...
   $form = new Form($controller, $name, $fields, $actions);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文