未定义的变量:我视图中的数据
这是输入中的简单显示数据。
那么,为什么这个输入不显示我的查询结果呢?
我的看法
<input type="text" name="sitename" value="<?php echo $data['sitename']; ?>"><br>模型
public function getData() { $query = "SELECT * FROM $this->tablename ORDER BY 'id' DESC LIMIT 1"; if (!$sqli = mysqli_query($this->cxn->connect(),$query)) { throw new Exception("Error Processing Request"); } else { $num = mysqli_num_rows($sqli); while ($num > 0) { $data = mysqli_fetch_array($sqli); $num--; } return $data; } }Undefined variable: data in my view
This is a simple display data in the input.
So, why this input isn't display my query result at it?
my view
<input type="text" name="sitename" value="<?php echo $data['sitename']; ?>"><br>model
public function getData() { $query = "SELECT * FROM $this->tablename ORDER BY 'id' DESC LIMIT 1"; if (!$sqli = mysqli_query($this->cxn->connect(),$query)) { throw new Exception("Error Processing Request"); } else { $num = mysqli_num_rows($sqli); while ($num > 0) { $data = mysqli_fetch_array($sqli); $num--; } return $data; } }最满意答案
仅仅因为变量在某处被声明,并不意味着它随处可用。 所有变量都具有可访问的scope 。 请参阅: http : //php.net/manual/en/language.variables.scope.php以获取有关范围的更多信息。
您需要将$data变量传递到视图中。 我想你正在使用某种MVC框架,因为你有一个模型和一个视图。 如果是这种情况,您可以查找如何将变量传递到该特定框架中的视图。 控制器方法的基本结构可能如下所示:
//sudo code - not specific to an actual framework public function controller_method() { $data = $model->getData(); $this->template->set('data',$data); $this->template->load('view'); }只需在您的特定框架中搜索如何执行此操作。 希望有所帮助!
编辑根据您的评论看起来您在加载视图后设置数据。 您需要交换订单并调用$display = new Display("main"); $data = $display->getData(); $display = new Display("main"); $data = $display->getData(); 在 include'../model/display.php';
Simply because a variable is declared somewhere, doesn't mean it is available everywhere. All variables have scope in which they are accessible. See this: http://php.net/manual/en/language.variables.scope.php for more information on scope.
You need to pass the $data variable into your view. I image you're using some sort of MVC framework since you have a model and a view. If this is the case you can lookup how to pass variables into views in that specific framework. The basic structure of your controller method might look something like this:
//sudo code - not specific to an actual framework public function controller_method() { $data = $model->getData(); $this->template->set('data',$data); $this->template->load('view'); }Just search how to do that in your specific framework. Hope that helps!
EDIT Base on your comment it looks like you're setting data after you load the view. You need to swap the order and call $display = new Display("main"); $data = $display->getData(); before you include'../model/display.php';
更多推荐
发布评论