Home > Backend Development > PHP Tutorial > ci framework (1), ci framework (_PHP tutorial

ci framework (1), ci framework (_PHP tutorial

WBOY
Release: 2016-07-13 10:23:26
Original
1011 people have browsed it

ci framework (1), ci framework (

ci directory structure                                                                                                                      

Visit form
|-----<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

Add function in applicationcontrollerswelcome.php:

Entry file.php/controller/action
<span>public</span> <span>function</span><span> hello()    
{    
    </span><span>echo</span> "test"<span>;   
}</span>
Copy after login

ci framework (1), ci framework (_PHP tutorial Create a new controller yourself, hello.php:

Note:

<?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

ci framework (1), ci framework (_PHP tutorial Method names starting with an underscore cannot be accessed successfully and can only be accessed indirectly.

Only public modified methods can be accessed.
  • Try not to use methods with the same name as the class, as they will be treated as constructors.
  • Loading view                                                                                         
applicationviewsview_test.php or applicationviewsviewtest.php (this method is mainly to facilitate the same type of views to be placed in the same folder for easy management)

Controller:

or:

<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
Effect:

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

Transmit data from the controller to the view, controller:

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

View:

ci framework (1), ci framework (_PHP tutorialEffect:

public

<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
function

addView() {
<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

$this

->load->vars("title","value"

); ci framework (1), ci framework (_PHP tutorial$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']="title"; $data['list']=$list; $this->load->vars($data); $this->load->view("view_test"); $this->load->view("footer"); } View view_test: View footer: Display effect: uri parameter acquisition                                                               

Controller:

<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

Effect:

ci framework (1), ci framework (_PHP tutorial加载数据库

这个操作在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或者其它。。。


  • 栏目名称</li>
    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']."</li>";
      }
      endforeach;
      echo "</ul></li>";
      }

      endforeach; ?>
      </ul>

      当然这仅限于两级菜单,多级或无限极,可以使用函数递归
      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 "</ul></li>";
        }

        endforeach;

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

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

          Doesn’t the class name of MY_Controller need to be the same?

          www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/836503.htmlTechArticleci framework (1), ci framework (ci directory structure |----- system framework program directory |- ---- core The core program of the framework |-----CodeIgniter.php boot file|-----Common.php loads the base class library...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template