dongxian6285 2012-09-12 06:30
浏览 32
已采纳

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());
}

The generateMapNumber() function looks like this:

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

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

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

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

--- EDIT ---

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
  • 写回答

3条回答 默认 最新

  • douzai1074 2012-09-12 20:48
    关注

    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()));
        }
    }
    

    And then in the add.php file...

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

    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!

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

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?