如何根据codeigniter mvc修改下面的代码?

I am new to codeigniter and so would like to know how can this code be modified to suit the MVC?Right now I have both html and php codes in the view.Any suggestions to improve it and keep it separated in controller and view is appreciated.How can I separate the php and html codes in the view?

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>

My controller is as follows:

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

}

1个回答

You should move the DirectoryIterator related operations to your controller function, then have an intermediary array with all files that pass your conditional criteria and pass this array to view. In your view just print the array with no extra operations.

There is a second argument available for CI's view function, in your case it would look something like:

$this->output->view('translation/language', $array_with_files);

Please refer https://www.codeigniter.com/user_guide/general/views.html

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐