dongsashe6006 2017-09-11 13:33
浏览 27

如何根据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条回答

  • dongsheng1238 2017-09-11 13:36
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用