PHP Concrete 5将变量传递给Add.php [英] PHP Concrete 5 Pass Variables to Add.php

查看:90
本文介绍了PHP Concrete 5将变量传递给Add.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个新块,我想在添加时将已定义的变量传递给该块实例。

I'm creating a new block and I want to pass a defined variable to the block instance on add.

在我的控制器中,我有以下内容:

In my controller, I have the following:

// declare the var
public $hasMap = 0;

public function add() {
    $this->set('hasMap', $this->generateMapNumber());
}

generateMapNumber()函数如下:

The generateMapNumber() function looks like this:

public function generateMapNumber() {
    return intval(mt_rand(1,time()));
}

在我的add.php表单中,我有一个隐藏字段:

In my add.php form I have a hidden field:

<?php $myObj = $controller; ?>
<input type="hidden" name="hasMap" value="<?php echo $myObj->hasMap?>" />

当我创建一个新块时, hasMap 始终为 0 ,隐藏的输入值也始终为 0 。有什么建议么?谢谢!

When I create a new block, hasMap is always 0 and the hidden input value is always 0 too. Any suggestions? Thank you!

---编辑---

从Concrete5文档中:

From the concrete5 documentation:

// This...
$controller->set($key, $value)
// ... takes a string $key and a mixed $value, and makes a variable of that name 
// available from within a block's view, add or edit template. This is 
// typically used within the add(), edit() or view() function


推荐答案

这就是解决方案。在控制器中...

So here's the solution. In the controller...

public $hasMap = 0;

// no need for this:
// public function add() {  }

public function generateMapNumber() {
    if (intval($this->hasMap)>0) {
        return $this->hasMap;
    } else {
        return intval(mt_rand(1,time()));
    }
}

然后在add.php文件中...

And then in the add.php file...

<?php $myObj = $controller; ?>

<input type="hidden" name="hasMap" value="<?php echo $myObj->generateMapNumber()?>" />

它完美地工作。添加后,将生成一个新数字,并进行编辑后,将从db中的 hasMap 字段中提取现有数字。

It works perfectly. On add, a new number is generated and on edit, the existing number is drawn from the hasMap field in the db.

感谢所有输入。希望对别人有帮助!

Thanks for all the input. Hope that helps someone else!

这篇关于PHP Concrete 5将变量传递给Add.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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