数据不会显示在我的视图中(the data do not display in my view)

编程入门 行业动态 更新时间:2024-10-28 00:23:32
数据不会显示在我的视图中(the data do not display in my view)

未定义的变量:我视图中的数据

这是输入中的简单显示数据。

那么,为什么这个输入不显示我的查询结果呢?

我的看法

<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';

更多推荐

本文发布于:2023-08-06 06:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1445013.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   数据   data   display   view

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!