Home > php教程 > php手册 > body text

ci框架(一),ci框架(

WBOY
Release: 2016-06-13 09:29:20
Original
4030 people have browsed it

ci框架(一),ci框架(

ci目录结构                                                                                   

|-----<span>system</span><span>                框架程序目录  
    </span>|-----<span>core              框架的核心程序  
        </span>|-----CodeIgniter.<span>php   引导性文件  
        </span>|-----Common.<span>php    加载基类库的公共函数  
        </span>|-----Controller.<span>php    基控制器类文件:CI_Controller  
        </span>|-----Model.<span>php     基模型类文件:CI_Model  
        </span>|-----Config.<span>php    配置类文件:CI_Config  
        </span>|-----Input.<span>php     输入类文件:CI_Input  
        </span>|-----Output.<span>php    输出类文件:CI_Output  
        </span>|-----URL.<span>php       URL类文件:CI_URl  
        </span>|-----Router.<span>php    路由类文件:CI_Router  
        </span>|-----Loader.<span>php    加载类文件:CI_Loader  
    </span>|-----<span>helpers           辅助函数  
        </span>|-----url_helper.<span>php    url相关的辅助函数,如:创建url的辅助函数  
        </span>|-----captcha_helper.<span>php创建图形验证码的辅助函数  
    </span>|-----<span>libraries         通用类库  
        </span>|-----Pagination.<span>php    通用分页类库  
        </span>|-----Upload.<span>php    通用文件上传类库  
        </span>|-----Image_lib.<span>php 通用图像处理类库  
        </span>|-----Session.<span>php   通用session类库  
    </span>|-----<span>language          语言包  
    </span>|-----<span>database          数据库操作相关的程序  
        </span>|-----DB_active_rec.<span>php 快捷操作类文件(ActiveRecord)  
    </span>|-----<span>fonts             字库  
      
</span>|-----<span>application           项目目录  
    </span>|-----<span>core              项目的核心程序  
    </span>|-----<span>helpers           项目的辅助函数  
    </span>|-----<span>libraries         通用类库  
    </span>|-----<span>language          语言包  
    </span>|-----<span>config            项目相关的配置  
        </span>|-----config.<span>php    项目相关的配置文件     
        </span>|-----database.<span>php  数据库相关的配置文件  
        </span>|-----autoload.<span>php  设置自动加载类库的配置文件  
        </span>|-----constants.<span>php 常量配置文件  
        </span>|-----routes.<span>php    路由配置文件  
    </span>|-----<span>controllers       控制器目录  
        </span>|-----welcome.<span>php   控制器文件,继承CI_Controller  
    </span>|-----<span>models            模型目录  
        </span>|-----welcome_model.<span>php 模型文件,继承CI_Model  
    </span>|-----<span>views             视图目录  
        </span>|-----welcome.php   视图模板文件,默认后缀名为.<span>php  
    </span>|-----<span>cache             存放数据或模板的缓存文件  
    </span>|-----<span>errors            错误提示模板  
    </span>|-----<span>hooks             钩子,在不修改系统核心文件的基础上扩展系统功能  
    </span>|-----<span>third_party       第三方库  
    </span>|-----<span>logs              日志  
  
</span>|-----index.php             入口文件
Copy after login

访问形式

在\application\controllers\welcome.php中添加函数:

<span>public</span> <span>function</span><span> hello()    
{    
    </span><span>echo</span> "test"<span>;   
}</span>
Copy after login

入口文件.php/控制器/动作

自己新建控制器,hello.php:

<?php <span>if</span> ( ! <span>defined</span>('BASEPATH')) <span>exit</span>('No direct script access allowed'<span>);

    </span><span>class</span> Hello <span>extends</span><span> CI_Controller 
    {
        </span><span>public</span> <span>function</span> sayHello(<span>$name</span><span>)
        {
            </span><span>echo</span> <span>$name</span>,",Hello World"<span>;
        }
    }
</span>?>
Copy after login

注意:

  • 方法名以下划线开头的是不能访问成功的,只能间接访问。
  • 只能访问public修饰的方法。
  • 尽量不要与类名相同的方法,会当作构造函数。

加载视图

application\views\view_test.php或者application\views\view\test.php(这中做法主要是方便同一类型的视图可以放在同一个文件夹下方便管理)

<span><</span><span>html</span><span>></span>
<span><</span><span>head</span><span>></span>
<span></</span><span>head</span><span>></span>
<span><</span><span>body</span><span>></span><span>
test_ci_hello_world
</span><span></</span><span>body</span><span>></span>
<span></</span><span>html</span><span>></span>
Copy after login

控制器:

<span>public</span> <span>function</span><span> addView()
{
    </span><span>$this</span>->load->view("view_test"<span>);
}</span>
Copy after login

或者:

<span>public</span> <span>function</span><span> addView2()
{
    </span><span>$this</span>->load->view("view/test"<span>);
}</span>
Copy after login

效果:

分配变量

把数据从控制器中传到视图中,控制器:

<span>public</span> <span>function</span><span> addView()
        {
            </span><span>$this</span>->load->vars("title","value"<span>);
            </span><span>$list</span> = <span>array</span><span>(
            </span><span>array</span>('id'=>1,'name'=>'jack','email'=>'123@123.com'),
            <span>array</span>('id'=>2,'name'=>'jack2','email'=>'1233@123.com'),
            <span>array</span>('id'=>3,'name'=>'jack3','email'=>'12333@123.com'<span>)
            );
            </span><span>$data</span>['new_title']="标题"<span>;
            </span><span>$data</span>['list']=<span>$list</span><span>;
            </span><span>$this</span>->load->vars(<span>$data</span><span>);
            
            </span><span>$this</span>->load->view("view_test"<span>);
        }</span>
Copy after login

视图:

<html>
    <head>
    </head>
    <body>
        <h1><?php <span>echo</span> <span>$title</span>;?></h1>
        <h1><?php <span>echo</span> <span>$new_title</span>;?></h1><span>
        test_ci_hello_world
        </span><table>
            <?php <span>foreach</span>(<span>$list</span> <span>as</span> <span>$item</span>):?>
            <tr>
                <td><?=<span>$item</span>['id']?></td>    
                <td><?=<span>$item</span>['name']?></td>
                <td><?=<span>$item</span>['email']?></td>                
            </tr>
            <?php <span>endforeach</span>;?>
        </table>
    </body>
</html>
Copy after login

效果:

public function addView() { $this->load->vars("title","value"); $list = array( array('id'=>1,'name'=>'jack','email'=>'123@123.com'), array('id'=>2,'name'=>'jack2','email'=>'1233@123.com'), array('id'=>3,'name'=>'jack3','email'=>'12333@123.com') ); $data['new_title']="标题"; $data['list']=$list; $this->load->vars($data); $this->load->view("view_test"); $this->load->view("footer"); }

视图view_test:

<html>
    <head>
    </head>
    <body>
        <h1><?php <span>echo</span> <span>$title</span>;?></h1>
        <h1><?php <span>echo</span> <span>$new_title</span>;?></h1><span>
        test_ci_hello_world
        </span><table>
            <?php <span>foreach</span>(<span>$list</span> <span>as</span> <span>$item</span>):?>
            <tr>
                <td><?=<span>$item</span>['id']?></td>    
                <td><?=<span>$item</span>['name']?></td>
                <td><?=<span>$item</span>['email']?></td>                
            </tr>
            <?php <span>endforeach</span>;?>
        </table>
Copy after login

视图footer:

<span>CI_hello_world!!!
</span><span></</span><span>body</span><span>></span>
<span></</span><span>html</span><span>></span>
Copy after login

显示效果:

uri参数获取

控制器:

<span>public</span> <span>function</span> getUri(<span>$id</span>,<span>$name</span>,<span>$year</span><span>)
        {
            </span><span>echo</span> "id--->".<span>$id</span>."---name--->".<span>$name</span>."---year--->".<span>$year</span>."<br />"<span>;
            </span><span>echo</span> "segment(1)--->".<span>$this</span>->uri->segment(1)."<br />"<span>;
            </span><span>echo</span> "segment(2)--->".<span>$this</span>->uri->segment(2)."<br />"<span>;
            </span><span>echo</span> "segment(3)--->".<span>$this</span>->uri->segment(3)."<br />"<span>;
            </span><span>echo</span> "segment(4)--->".<span>$this</span>->uri->segment(4)."<br />"<span>;
            </span><span>echo</span> "segment(5)--->".<span>$this</span>->uri->segment(5)."<br />"<span>;
        }</span>
Copy after login

效果:

加载数据库

这个操作在MVC中是放到model中做的。

在\application\config\database.php中配置数据库参数,注意dbprefixswap_pre这两个参数。在php中写的是前缀,会默认当作swap_pre,然后放到数据库中的时候会转成dbprefix,但是最好两个都弄成一样的。

还有$active_group,默认是default,如果要连接两个数据库,把default另外取名,然后在函数中写明参数就OK。

必须继承数据核心类CI_Model,同时重载父类中的构造方法。

<span>class</span> Model_name <span>extends</span><span> CI_Model
{
    </span><span>function</span><span> __construct()
    {
        parent</span>::<span>__construct();
    }
}</span>
Copy after login

在每次使用数据库的时候,都需要加载一次数据库:

<span>$this</span>->load->database();
Copy after login

为了方便,可以将数据库的加载设置成自动加载,在\application\config\autoload.php中。

<span>$autoload</span>['libraries'] = <span>array</span>('database');
Copy after login

对于数据库访问对象,装载到超级对象的属性中 $this->db

<span>$res</span> = <span>$this</span>->db->query(<span>$sql</span>);<span>//</span><span>返回对象</span>
<span>$res</span>->result();<span>//</span><span>返回数组,数组中是一个一个的对象</span>
<span>$res</span>->result_array();<span>//</span><span>返回二维数组,里面是关联数组</span>
<span>$res</span>->row();<span>//</span><span>返回第一条数据,直接是一个对象</span>
Copy after login

AR操作数据库

在database.php文件中,将$active_recoed的值改为TRUE,这样就可以使用AR了。

<span>//</span><span>查询</span>
<span>public</span> <span>function</span><span> index()
{
    </span><span>$res</span> = <span>$this</span>->db->get('表名');<span>//</span><span>这里自动调用前缀</span>
    <span>foreach</span>(<span>$res</span>->result() <span>as</span> <span>$item</span><span>)
    {
        </span><span>echo</span> <span>$item</span>->name."<br />"<span>;
    }
}</span>
Copy after login
<span>//</span><span>插入</span>
<span>public</span> <span>function</span><span> index()
{
    </span><span>$data</span>=<span>array</span><span>(
        </span>'name'=>'lisi',
        'password'=><span>md5</span>('lisi'<span>)
    );
    </span><span>$bool</span> = <span>$this</span>->db->insert("表名",<span>$data</span><span>);
    </span><span>var_dump</span>(<span>$bool</span><span>);
}</span>
Copy after login
<span>//</span><span>更新</span>
<span>public</span> <span>function</span><span> index()
{
    </span><span>$data</span>=<span>array</span><span>(
        </span>'name'=>'wangwu',
        'password'=><span>md5</span>('wangwu'<span>)
    );

    </span><span>$bool</span> = <span>$this</span>->db->update('表名',<span>$data</span>,<span>array</span>('id'=>3<span>));
    </span><span>var_dump</span>(<span>$bool</span><span>);
}</span>
Copy after login
<span>//</span><span>删除</span>
<span>$bool</span> = <span>$this</span>->db->delete('表名',<span>array</span>('id'=>2<span>));
</span><span>var_dump</span>(<span>$bool</span>);
Copy after login

新手助PHP 程序 CI框架开发 进来看一眼吧

常规的方法是ul嵌套,即主菜单ul-li里嵌套子菜单ul,要用到两级循环
首先循环主菜单,要有固定的条件来判断出主菜单,比如主菜单的uid==0或者其它。。。


  • 栏目名称
    if($news_item['uid'] == 0){ //判断并得到主菜单

    echo "
  • ".$news_item['title'] . '
      ';
      foreach ($news as $child_item): //循环二次
      if($news_item['id'] == $child_item['uid']){ //判断并得到对应子菜单
      echo "
    • "."ss".$child_item['title']."";
      }
      endforeach;
      echo "";
      }

      endforeach; ?>


      当然这仅限于两级菜单,多级或无限极,可以使用函数递归
      function menu($uid=0){ //设置缺省从主菜单开始
      global $news;
      foreach ($news as $news_item):

      if($news_item['uid'] == $uid){

      echo "
    • ".$news_item['title'] . '
        ';
        menu($news_item['id']); //递归调用

        echo "";
        }

        endforeach;

        }
        ------ 调用方法 ------------------------------
          >
           

          CI框架想创建一个model类继承CI_Modle,我创建在app/core/MY_Model,直接报错了

          难道MY_Controller不要类名一致?
           

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template