我是codeigniter的新手,所以想知道如何修改这些代码以适应MVC?现在我在视图中同时拥有html和php代码。任何改进它的建议并将它保持在控制器和我很欣赏。如何在视图中分离php和html代码?
My View is as follows
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/js/select2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".searchabledropdown").select2();
</script>
</head>
<body>
<form action="">
<div>
<select id="main_language" name ="main_language">
<option>English</option>
<option>Japanese</option>
</select>
<input type="button" value="Set Main Language">
</div>
<div>
<h3>Translation Management</h3>
</div>
<div>
<select name="lang_choice1" id="lang_choice1">
<!-- <option value="" selected="selected">-----</option>-->
<?php
$path = './application/language/';
$dir = new DirectoryIterator($path);
foreach ($dir as $fileinfo) {
if ($fileinfo->isDir() && !$fileinfo->isDot()) {
echo "<option value='" . $fileinfo->getFilename() . "'>".$fileinfo->getFilename()."</option>";
}
}
?>
</select>
=> <select name="lang_choice2">
<!--<option value="" selected="selected">-----</option>-->
<?php
$path = './application/language/';
$dir = new DirectoryIterator($path);
foreach ($dir as $fileinfo) {
if ($fileinfo->isDir() && !$fileinfo->isDot()) {
echo "<option value='" . $fileinfo->getFilename() . "'>".$fileinfo->getFilename()."</option>";
}
}
?>
</select>
</div>
我的控制器如下:
class Language extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->accesscontrol->can_or_redirect('view', 'translation');
$this->output->view('translation/language');
}
}
答案 0 :(得分:0)
您应该将与DirectoryIterator相关的操作移动到您的控制器函数,然后让中间数组包含所有通过条件条件的文件,并将此数组传递给view。在您的视图中,只需打印数组而无需额外操作。
CI的视图功能有第二个参数,在您的情况下,它看起来像:
$this->output->view('translation/language', $array_with_files);
请参阅https://www.codeigniter.com/user_guide/general/views.html