I am struggling to get my head around this since too many hours and I need some help.:) I have a website build on Kohana and want to dynamically change the content of some text when the user click one button or and another. Not sure if I am doing it the right way but this what I did so far (ho by the way I am new to this framework).
我正在努力解决这个问题,因为太多时间我需要一些帮助。:)我有一个建立在Kohana上的网站,并希望在用户点击一个按钮或另一个时动态更改某些文本的内容。不确定我是否采用正确的方式,但这是我迄今为止所做的事情(顺便说一下,我是这个框架的新手)。
Controller:
控制器:
class Controller_Homepage extends Controller_General {
class Controller_Homepage extends Controller_General {
public $template = "template/widepage";
public $textbuyer = array (
'text1' => "homepage.buyer.bigtext1", //transfering language variable.
'text2' => "homepage.buyer.bigtext2",
//with more ...
);
public $textseller = array (
'text1' => "homepage.seller.bigtext1",
'text2' => "homepage.seller.bigtext2",
'text3' => "homepage.seller.bigtext3",
//with more ...
);
public $thetext = array ("textbuyer"); //the defaul array is textbuyer
public function action_index() {
public function action_index(){
$this->content = View::factory("homepage")
->bind('pagetext', $thetext );
if ($this->request->method() === Request::POST) {
$post= $this->request->post();
if (isset($post['buyer'])){
$thetext = $textbuyer;//gives rrorException [ Notice ]: Undefined variable: textbuyer
// arr::overwrite($thetext, $textbuyer);
}else if(isset($post['seller'])){
$thetext = $textseller;
}
}
}
Section of my view to show how I use the variable in the view:
我的视图部分显示了我如何在视图中使用变量:
<div class="container_content">
<div>
<p id='sline'><?php echo $pagetext['text1']; ?></p>
</div>
<div>
Can't get the content of my array to the view and when I click on one of the two buttons this code gives me the following error: ErrorException [ Notice ]: Undefined variable: textbuyer. What I am doing wrong ? Why I get the error I mentionned ? Thank you!
无法将我的数组的内容添加到视图中,当我单击其中一个按钮时,此代码会给出以下错误:ErrorException [Notice]:未定义的变量:textbuyer。我做错了什么?为什么我收到我提到的错误?谢谢!
1 个解决方案
#1
1
When you define the variables like this
当您定义这样的变量时
public $textbuyer = ...
public $textseller = ...
public $thetext = ...
They are attributes of your class. And since they are, you need to call them via
它们是您班级的属性。既然它们是,你需要通过它们来调用它们
$this->textbuyer
$this->textseller
$this->thetext
Just as you call methods inside the same class with $this->methodName()
instead of methodName()
.
正如您使用$ this-> methodName()而不是methodName()调用同一类中的方法一样。
class Foo {
public $bar = "hello ";
public function foo() {
$bar = "world";
print $this->bar.$bar; // hello world
}
}
This would work just fine and you get the error because you never define $textbuyer
(because you want to call $this->textbuyer
).
这可以正常工作,你得到错误,因为你从来没有定义$ textbuyer(因为你想调用$ this-> textbuyer)。
#1
1
When you define the variables like this
当您定义这样的变量时
public $textbuyer = ...
public $textseller = ...
public $thetext = ...
They are attributes of your class. And since they are, you need to call them via
它们是您班级的属性。既然它们是,你需要通过它们来调用它们
$this->textbuyer
$this->textseller
$this->thetext
Just as you call methods inside the same class with $this->methodName()
instead of methodName()
.
正如您使用$ this-> methodName()而不是methodName()调用同一类中的方法一样。
class Foo {
public $bar = "hello ";
public function foo() {
$bar = "world";
print $this->bar.$bar; // hello world
}
}
This would work just fine and you get the error because you never define $textbuyer
(because you want to call $this->textbuyer
).
这可以正常工作,你得到错误,因为你从来没有定义$ textbuyer(因为你想调用$ this-> textbuyer)。