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条)

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口