我在MVC中与forloop做斗争。我想从输入文本中传递数据,其中的数据相当于1,2,3,4,5,6,7.-that来自数据库。
我将其传递给控制器,并使用名称为$_POST的text1方法获取数据。这是我的控制器代码
{
$template = $this->loadView('view_display');
if(isset($_POST['check'])):
$text1= $_POST['text1'];
$text1= explode(',', $text1);
foreach($text1 as $text1):
$rem = $this->Faculty_model->testFunction($this->security_plugin->cleanInput($text1));
$template->set(['stud' => $rem, 'username' => $this->session_helper->get('username'), 'security'=> $this->security_plugin, 'url' => $this->url_helper]);
endforeach;
endif;
$this->security_plugin->CSRFToken();
$template->render();这是我的模型
{
$sql="select * from table where id=:text1";
$bind = array(
':text1' => $text1
);
$data = $this->db->fetchAll($sql, $bind);
return $data;
}这就是我的观点
<?php if(!empty($stud)): ?>
<?php foreach($stud as $stud): ?>
<?php echo $security->SanitizeString($stud->ID); ?>
<?php echo $security->SanitizeString($stud->NAME); ?>
<?php echo $security->SanitizeString($stud->AGE); ?>问题是它将只显示text1文本框中的最后一个数字。我搞不懂。
任何帮助都很感谢:)
发布于 2020-04-11 00:09:53
1.您需要在$rem之前将foreach()定义为数组,然后进行值的赋值。
2.将$template->set()代码放在foreach()之外
$template = $this->loadView('view_display');
if(isset($_POST['check'])):
$text1= $_POST['text1'];
$text1= explode(',', $text1);
$rem = [];
foreach($text1 as $text1):
$rem[] = $this->Faculty_model->testFunction($this->security_plugin->cleanInput($text1));
endforeach;
$template->set(['stud' => $rem, 'username' => $this->session_helper->get('username'), 'security'=> $this->security_plugin, 'url' => $this->url_helper]);
endif;
$this->security_plugin->CSRFToken();
$template->render();https://stackoverflow.com/questions/61150391
复制相似问题