作者归档:Rick

ThinkPHP快速搭建MVC

分步骤:
1. 创建Model
2. 创建View
3. 创建Controller
4. 修改配置config.php

1. 创建Model

在目录Home\Model创建文件UserModel.class.php

<?php
namespace Home\Model;
use Think\Model;
class UserModel extends Model {
    protected $trueTableName = 'wp_users'; 
}

2. 创建View

在目录Home\View\Test创建文件test.html

this is template!!
<br>
param:{$name}
<br> 
<?php echo($name);?>
<?php
    //echo phpinfo();
//echo phpversion();
?>

3. 创建Controller

在目录Home\Controller创建文件TestController.class.php

<?php
namespace Home\Controller;
use Think\Controller;

class TestController extends Controller {


    public function test() {
        //$this->show("this is testhaha",'utf-8');
        //$this->success('新增成功22', 'test2');

        $User = new \Home\Model\UserModel('users','wp_','DB_CONFIG');
        $list = $User->select();


        //$name = 'ThinkPHP';
        $this->assign('name',$list[0]['user_login']);
        //$this->display();
        $this->display('test');
    }
}

4. 修改配置

在目录Home\Conf修改文件config.php

<?php
return array(
    //'配置项'=>'配置值'
    'DB_CONFIG' => array(
         'db_type'  => 'mysql',
         'db_user'  => 'root',
         'db_pwd'   => '******',
         'db_host'  => '121.42.151.190',
         'db_port'  => '3306',
         'db_name'  => 'wordpress'
    ),
);

访问地址:http://localhost:8080/cms/index.php/Home/Test/test

浏览器显示如下:

    this is template!! 
    param:Rick
    Rick

PHP面向对象编程

AB是两个平级目录,A目录下有个UserModel.phpModel.php,UserModel 继承自Model.php;B目录下有个调用者index.php

Model.php

<?php
class Model {

   public function getMsg() {
        return "this parent message";
   }
}

UserModel.php

<?php
require 'Model.php';
class UserModel extends Model {

    private $name;

    public function __construct($name) {
        echo parent::getMsg().'<br>'; //调用父类方法
        $this->name = $name;
    }

    public function setName($name) {
        $this->name = $name;
    }

     public function getName() {
        return $this->name;
    }

    public static function getStaticFun() {
        echo "hello static";
    }
}

index.php

<?php

require "../UserModel.php";

$User = new UserModel('Leno.Li');
echo $User->getName().'<br>';

$User->setName('Rick.Xu');

echo $User->getName()."<br>";

UserModel::getStaticFun(); //调用类的静态方法
echo "<br>";
$User->getStaticFun();  //通过实例调用静态方法
//UserModel.getStaticFun(); error

Output

this parent message
Leno.Li
Rick.Xu
hello static
hello static

thinkphp初体验-基于MVC开发

官方手册请参考:ThinkPHP3.2.*版本开发手册

快速学习thinkphp从以下几个方面入手:
* 控制器
* 模型
* 视图
* 模板

从上述几个方面构建一个简单的基于MVC的实例:

1. 控制器

在目录Home\Controller\创建测试文件TestController.class.php

<?php
namespace Home\Controller;
use Think\Controller;

class TestController extends Controller {


    public function test() {
        //$this->show("this is testhaha",'utf-8');
        //$this->success('新增成功22', 'test2');

        $User = new \Home\Model\UserModel('users','wp_','DB_CONFIG');
        $list = $User->select();


        //$name = 'ThinkPHP';
        $this->assign('name',$list[0]['user_login']);
        //$this->display();
        $this->display('test');
    }
}

2. 模型

Home\Model下创建UserModel.class.php

<?php
namespace Home\Model;
use Think\Model;
class UserModel extends Model {
    //protected $tablePrefix = 'wp_';
    protected $trueTableName = 'wp_users'; 
}

3. 视图

Home\View\Test下创建test.html

this is template!!
<br>
param:{$name}
<br> 
<?php echo($name);?>

4. 模板

5.配置文件

Home\Conf\config.php

<?php
return array(
    //'配置项'=>'配置值'
    'DB_CONFIG' => array(
         'db_type'  => 'mysql',
         'db_user'  => 'root',
         'db_pwd'   => '123456',
         'db_host'  => '127.0.0.1',
         'db_port'  => '3306',
         'db_name'  => 'db'
    ),
);

访问地址: http://localhost:8080/cms/index.php/Home/Test/test

这里必须强调一下命名规范,不然会有很多的坑。
* 类文件都是以.class.php为后缀(这里是指的ThinkPHP内部使用的类库文件,不代表外部加载的类库文件),使用驼峰法命名,并且首字母大写,例如 DbMysql.class.php;
* 类的命名空间地址和所在的路径地址一致,例如Home\Controller\UserController类所在的路径应该是 Application/Home/Controller/UserController.class.php
* 确保文件的命名和调用大小写一致,是由于在类Unix系统上面,对大小写是敏感的(而ThinkPHP在调试模式下面,即使在Windows平台也会严格检查大小写);
* 类名和文件名一致(包括上面说的大小写一致),例如 UserController类的文件命名是UserController.class.phpInfoModel类的文件名是InfoModel.class.php, 并且不同的类库的类命名有一定的规范;
* 函数、配置文件等其他类库文件之外的一般是以.php为后缀(第三方引入的不做要求);
* 函数的命名使用小写字母和下划线的方式,例如 get_client_ip;
方法的命名使用驼峰法,并且首字母小写或者使用下划线“”,例如 getUserName,_parseType,通常下划线开头的方法属于私有方法;
* 属性的命名使用驼峰法,并且首字母小写或者使用下划线“
”,例如 tableName、_instance,通常下划线开头的属性属于私有属性;
* 以双下划线打头的函数或方法作为魔法方法,例如 __call 和 __autoload;
* 常量以大写字母和下划线命名,例如 HAS_ONE和 MANY_TO_MANY;
* 配置参数以大写字母和下划线命名,例如HTML_CACHE_ON;
* 语言变量以大写字母和下划线命名,例如MY_LANG,以下划线打头的语言变量通常用于系统语言变量,例如 _CLASS_NOT_EXIST_;
* 对变量的命名没有强制的规范,可以根据团队规范来进行;
* ThinkPHP的模板文件默认是以.html 为后缀(可以通过配置修改);
* 数据表和字段采用小写加下划线方式命名,并注意字段名不要以下划线开头,例如 think_user 表和 user_name字段是正确写法,类似 _username 这样的数据表字段可能会被过滤。

PHP操作Mysql

省略php的安装配置,以及如何配置对mysql的支持。

操作代码如下:

<?php
$servername = "127.0.0.1";
$username = "root";
$password = "123456";
$mysql_database = "db";


$sql = "SELECT id,user_login,user_email FROM wp_users";


// 创建连接
$conn = new mysqli($servername, $username, $password, $mysql_database);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // 输出每行数据
    while($row = $result->fetch_assoc()) {
        echo "<br> id: ". $row["id"]. " - Name: ". $row["user_login"]. " " . $row["user_email"];
    }
} else {
    echo "0 个结果";
}
$conn->close();
?>

各种设备的CSS3MediaQuery整理及爽歪歪写法

原文地址:http://sentsin.com/web/1134.html

  • 响应式布局

    响应式布局麻烦之处就是每个尺寸的都要进行css定义,这个真的不是一般的蛋疼,下面有搜集到的各种尺寸css Media Query内容,搜集来源:media-queries-for-standard-devices。

    看了之后是不是非常之蛋疼呢,那么只有使用工具来写这些玩意儿了,俺用得最爽的就是 stylus ,真的爽yy了,如果 stylus 不会玩耍请看这里 stylus入门使用方法

    stylus

                                // Media queries
    mq-mobile = "screen and (max-width: 479px)"
    mq-tablet = "screen and (min-width: 480px) and (max-width: 767px)"
    mq-iPhones4 = "only screen and (min-device-width: 320px) and (max-device-width: 480px) and (-webkit-min-device-pixel-ratio: 2)"
    mq-normal = "screen and (min-width: 768px)"
    .page-number
        display: inline-block
        @media mq-mobile
            display: none
        @media mq-tablet
            color:red
        @media mq-iPhones4
            font-size:12px
        @media mq-normal
            background:yellow
                                
                               
                            

    代码laycode – v1.1

    编译成

                                .page-number {
      display: inline-block;
    }
    @media screen and (max-width: 479px) {
      .page-number {
        display: none;
      }
    }
    @media screen and (min-width: 480px) and (max-width: 767px) {
      .page-number {
        color: #f00;
      }
    }
    @media only screen and (min-device-width: 320px) and (max-device-width: 480px) and (-webkit-min-device-pixel-ratio: 2) {
      .page-number {
        font-size: 12px;
      }
    }
    @media screen and (min-width: 768px) {
      .page-number {
        background: #ff0;
      }
    }
                                
     
                            

    代码laycode – v1.1

    Phones and Handhelds

    iPhones

                                /* ----------- iPhone 4 and 4S ----------- */
    /* Portrait and Landscape */
    @media only screen 
      and (min-device-width: 320px) 
      and (max-device-width: 480px)
      and (-webkit-min-device-pixel-ratio: 2) {
    }
    /* Portrait */
    @media only screen 
      and (min-device-width: 320px) 
      and (max-device-width: 480px)
      and (-webkit-min-device-pixel-ratio: 2)
      and (orientation: portrait) {
    }
    /* Landscape */
    @media only screen 
      and (min-device-width: 320px) 
      and (max-device-width: 480px)
      and (-webkit-min-device-pixel-ratio: 2)
      and (orientation: landscape) {
    }
    /* ----------- iPhone 5 and 5S ----------- */
    /* Portrait and Landscape */
    @media only screen 
      and (min-device-width: 320px) 
      and (max-device-width: 568px)
      and (-webkit-min-device-pixel-ratio: 2) {
    }
    /* Portrait */
    @media only screen 
      and (min-device-width: 320px) 
      and (max-device-width: 568px)
      and (-webkit-min-device-pixel-ratio: 2)
      and (orientation: portrait) {
    }
    /* Landscape */
    @media only screen 
      and (min-device-width: 320px) 
      and (max-device-width: 568px)
      and (-webkit-min-device-pixel-ratio: 2)
      and (orientation: landscape) {
    }
    /* ----------- iPhone 6 ----------- */
    /* Portrait and Landscape */
    @media only screen 
      and (min-device-width: 375px) 
      and (max-device-width: 667px) 
      and (-webkit-min-device-pixel-ratio: 2) { 
    }
    /* Portrait */
    @media only screen 
      and (min-device-width: 375px) 
      and (max-device-width: 667px) 
      and (-webkit-min-device-pixel-ratio: 2)
      and (orientation: portrait) { 
    }
    /* Landscape */
    @media only screen 
      and (min-device-width: 375px) 
      and (max-device-width: 667px) 
      and (-webkit-min-device-pixel-ratio: 2)
      and (orientation: landscape) { 
    }
    /* ----------- iPhone 6+ ----------- */
    /* Portrait and Landscape */
    @media only screen 
      and (min-device-width: 414px) 
      and (max-device-width: 736px) 
      and (-webkit-min-device-pixel-ratio: 3) { 
    }
    /* Portrait */
    @media only screen 
      and (min-device-width: 414px) 
      and (max-device-width: 736px) 
      and (-webkit-min-device-pixel-ratio: 3)
      and (orientation: portrait) { 
    }
    /* Landscape */
    @media only screen 
      and (min-device-width: 414px) 
      and (max-device-width: 736px) 
      and (-webkit-min-device-pixel-ratio: 3)
      and (orientation: landscape) { 
    }
                                
     
                            

    代码laycode – v1.1

    Galaxy Phones

                                /* ----------- Galaxy S3 ----------- */
    /* Portrait and Landscape */
    @media screen 
      and (device-width: 320px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 2) {
    }
    /* Portrait */
    @media screen 
      and (device-width: 320px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 2) 
      and (orientation: portrait) {
    }
    /* Landscape */
    @media screen 
      and (device-width: 320px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 2) 
      and (orientation: landscape) {
    }
    /* ----------- Galaxy S4 ----------- */
    /* Portrait and Landscape */
    @media screen 
      and (device-width: 320px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 3) {
    }
    /* Portrait */
    @media screen 
      and (device-width: 320px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 3) 
      and (orientation: portrait) {
    }
    /* Landscape */
    @media screen 
      and (device-width: 320px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 3) 
      and (orientation: landscape) {
    }
    /* ----------- Galaxy S5 ----------- */
    /* Portrait and Landscape */
    @media screen 
      and (device-width: 360px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 3) {
    }
    /* Portrait */
    @media screen 
      and (device-width: 360px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 3) 
      and (orientation: portrait) {
    }
    /* Landscape */
    @media screen 
      and (device-width: 360px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 3) 
      and (orientation: landscape) {
    }
                                
     
                            

    代码laycode – v1.1

    HTC Phones

                                /* ----------- HTC One ----------- */
    /* Portrait and Landscape */
    @media screen 
      and (device-width: 360px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 3) {
    }
    /* Portrait */
    @media screen 
      and (device-width: 360px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 3) 
      and (orientation: portrait) {
    }
    /* Landscape */
    @media screen 
      and (device-width: 360px) 
      and (device-height: 640px) 
      and (-webkit-device-pixel-ratio: 3) 
      and (orientation: landscape) {
    }
                                
         
                            

    代码laycode – v1.1

    Tablets

    iPads

                                /* ----------- iPad mini ----------- */
    /* Portrait and Landscape */
    @media only screen 
      and (min-device-width: 768px) 
      and (max-device-width: 1024px) 
      and (-webkit-min-device-pixel-ratio: 1) {
    }
    /* Portrait */
    @media only screen 
      and (min-device-width: 768px) 
      and (max-device-width: 1024px) 
      and (orientation: portrait) 
      and (-webkit-min-device-pixel-ratio: 1) {
    }
    /* Landscape */
    @media only screen 
      and (min-device-width: 768px) 
      and (max-device-width: 1024px) 
      and (orientation: landscape) 
      and (-webkit-min-device-pixel-ratio: 1) {
    }
    /* ----------- iPad 1 and 2 ----------- */
    /* Portrait and Landscape */
    @media only screen 
      and (min-device-width: 768px) 
      and (max-device-width: 1024px) 
      and (-webkit-min-device-pixel-ratio: 1) {
    }
    /* Portrait */
    @media only screen 
      and (min-device-width: 768px) 
      and (max-device-width: 1024px) 
      and (orientation: portrait) 
      and (-webkit-min-device-pixel-ratio: 1) {
    }
    /* Landscape */
    @media only screen 
      and (min-device-width: 768px) 
      and (max-device-width: 1024px) 
      and (orientation: landscape) 
      and (-webkit-min-device-pixel-ratio: 1) {
    }
    /* ----------- iPad 3 and 4 ----------- */
    /* Portrait and Landscape */
    @media only screen 
      and (min-device-width: 768px) 
      and (max-device-width: 1024px) 
      and (-webkit-min-device-pixel-ratio: 2) {
    }
    /* Portrait */
    @media only screen 
      and (min-device-width: 768px) 
      and (max-device-width: 1024px) 
      and (orientation: portrait) 
      and (-webkit-min-device-pixel-ratio: 2) {
    }
    /* Landscape */
    @media only screen 
      and (min-device-width: 768px) 
      and (max-device-width: 1024px) 
      and (orientation: landscape) 
      and (-webkit-min-device-pixel-ratio: 2) {
    }
                                
                                
                            

    代码laycode – v1.1

    Galaxy Tablets

                                /* ----------- Galaxy Tab 10.1 ----------- */
    /* Portrait and Landscape */
    @media 
      (min-device-width: 800px) 
      and (max-device-width: 1280px) {
    }
    /* Portrait */
    @media 
      (max-device-width: 800px) 
      and (orientation: portrait) { 
    }
    /* Landscape */
    @media 
      (max-device-width: 1280px) 
      and (orientation: landscape) { 
    }
                                
                                
                            

    代码laycode – v1.1

    Nexus Tablets

                                /* ----------- Asus Nexus 7 ----------- */
    /* Portrait and Landscape */
    @media screen 
      and (device-width: 601px) 
      and (device-height: 906px) 
      and (-webkit-min-device-pixel-ratio: 1.331) 
      and (-webkit-max-device-pixel-ratio: 1.332) {
    }
    /* Portrait */
    @media screen 
      and (device-width: 601px) 
      and (device-height: 906px) 
      and (-webkit-min-device-pixel-ratio: 1.331) 
      and (-webkit-max-device-pixel-ratio: 1.332) 
      and (orientation: portrait) {
    }
    /* Landscape */
    @media screen 
      and (device-width: 601px) 
      and (device-height: 906px) 
      and (-webkit-min-device-pixel-ratio: 1.331) 
      and (-webkit-max-device-pixel-ratio: 1.332) 
      and (orientation: landscape) {
    }
                                
                                 
                            

    代码laycode – v1.1

    Kindle Fire

                                /* ----------- Kindle Fire HD 7" ----------- */
    /* Portrait and Landscape */
    @media only screen 
      and (min-device-width: 800px) 
      and (max-device-width: 1280px) 
      and (-webkit-min-device-pixel-ratio: 1.5) {
    }
    /* Portrait */
    @media only screen 
      and (min-device-width: 800px) 
      and (max-device-width: 1280px) 
      and (-webkit-min-device-pixel-ratio: 1.5) 
      and (orientation: portrait) {
    }
    /* Landscape */
    @media only screen 
      and (min-device-width: 800px) 
      and (max-device-width: 1280px) 
      and (-webkit-min-device-pixel-ratio: 1.5) 
      and (orientation: landscape) {
    }
    /* ----------- Kindle Fire HD 8.9" ----------- */
    /* Portrait and Landscape */
    @media only screen 
      and (min-device-width: 1200px) 
      and (max-device-width: 1600px) 
      and (-webkit-min-device-pixel-ratio: 1.5) {
    }
    /* Portrait */
    @media only screen 
      and (min-device-width: 1200px) 
      and (max-device-width: 1600px) 
      and (-webkit-min-device-pixel-ratio: 1.5) 
      and (orientation: portrait) {
    }
    /* Landscape */
    @media only screen 
      and (min-device-width: 1200px) 
      and (max-device-width: 1600px) 
      and (-webkit-min-device-pixel-ratio: 1.5) 
      and (orientation: landscape) {
    }
                                
                                 
                            

    代码laycode – v1.1

    Laptops

                                /* ----------- Non-Retina Screens ----------- */
    @media screen 
      and (min-device-width: 1200px) 
      and (max-device-width: 1600px) 
      and (-webkit-min-device-pixel-ratio: 1) { 
    }
    /* ----------- Retina Screens ----------- */
    @media screen 
      and (min-device-width: 1200px) 
      and (max-device-width: 1600px) 
      and (-webkit-min-device-pixel-ratio: 2)
      and (min-resolution: 192dpi) { 
    }
                                
                                
                            

    代码laycode – v1.1

    Wearables

    Apple Watch

                                /* ----------- Apple Watch ----------- */
    @media
      (max-device-width: 42mm)
      and (min-device-width: 38mm) { 
    }
                                
                                
                            

    代码laycode – v1.1

    Moto 360 Watch

                                /* ----------- Moto 360 Watch ----------- */
    @media 
      (max-device-width: 218px)
      and (max-device-height: 281px) { 
    }