Laravel 的多数据库连接

在 app/config/database.php  中可以设置

<?php
return array(
    'default' => 'mysql',
    'connections' => array(
        # Our primary database connection
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'host1',
            'database'  => 'database1',
            'username'  => 'user1',
            'password'  => 'pass1'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

        # Our secondary database connection
        'mysql2' => array(
            'driver'    => 'mysql',
            'host'      => 'host2',
            'database'  => 'database2',
            'username'  => 'user2',
            'password'  => 'pass2'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    ),
);

在 controller 中指定数据库

Schema::connection('mysql2')->create('some_table', function($table)
{
    $table->increments('id'):
});
$users = DB::connection('mysql2')->select(...);
<?php
class SomeModel extends Eloquent {
    protected $connection = 'mysql2';
}
<?php
class SomeController extends BaseController {
    public function someMethod()
    {
        $someModel = new SomeModel;
        $someModel->setConnection('mysql2');
        $something = $someModel->find(1);
        return $something;
    }
}

具体有时间再翻译

via: http://fideloper.com/laravel-multiple-database-connections

搬瓦工搬家记录

搬家的一点记录

yum -y upgrade
yum -y install wget
yum -y install vim
wget -c http://soft.vpser.net/lnmp/lnmp1.4.tar.gz && tar zxf lnmp1.4.tar.gz && cd lnmp1.4 && ./install.sh lnmp
lnmp vhost add
vim /usr/local/php/etc/php.ini
:set nu
:314
:wq
cd /root/lnmp1.4
./pureftpd.sh
lnmp ftp add
lnmp restart

服务器整站远程迁移备份

cd /home
tar czf - wwwroot | ssh root@67.216.213.31 -p 28376 tar xzf - -C /home

解决 “噢,没有这个文件,请重新检查文件名,然后再试。谢谢”

编辑主题时提示:噢,没有这个文件,请重新检查文件名,然后再试。谢谢

因使用 lnmp 一键安装包所致,进行如下操作:

找到服务器上的 php.ini 的位置,可以用 phpinfo() 函数查看该文件的位置,找到后编辑该文件,

vi /usr/local/php/etc/php.ini

找到第314行或附近的disable_functions按 i 键进入编辑模式,将其值里的 scandir 这个函数去掉,记得逗号也要去掉,再esc退出编辑模式,输入:wq 保存退出,之后重启你的nginx服务和php服务,或直接重启的lnmp

lnmp restart

解决安装WordPress主题及插件需要输入FTP问题,要执行请求的操作,WordPress需要访问您网页服务器的权限。请输入您的FTP登陆凭据以继续

更改项目目录权限及用户组

chown -R www /home/wwwroot/guofeng.io/
chmod -R 775 /home/wwwroot/guofeng.io/

.user.ini文件无法直接修改,如要修或删除需要先执行:chattr -i /网站目录/.user.ini

chattr -i /home/wwwroot/guofeng.io/.user.ini
chattr +i /home/wwwroot/guofeng.io/.user.ini

一球从100米高度自由落下,每次落地后反跳回原高度的一半,再落下。编写程序,输出第n次落地时,小球落下弹起共经过多少米。

一球从100米高度自由落下,每次落地后反跳回原高度的一半,再落下。编写 PHP 程序,输出第 n 次落地时,小球落下弹起共经过多少米。

<?php
function rebouns($n) {
    $links = array();
    $begin = 100;
    for($i=1; $i<=$n; $i++){
        if($i==1 || $i==2){
            $links[$i] = 100;
        }else{
            $links[$i] = $links[$i-1]/2;
        }
        $sum = array_sum($links);
        echo "第".$i."次落地,经过距离为".$sum."<br />";
    }
}
rebouns(5);

输出结果为:

第1次落地,经过距离为100
第2次落地,经过距离为200
第3次落地,经过距离为250
第4次落地,经过距离为275
第5次落地,经过距离为287.5

Please compose a functio thar meets the following requirement

We are a php shop and prefer you answering in php,

But you might answer in any other languages in case you are not familiarwith it, we can read C#, Java, C++, Perl, Ruby, etc.

Requirement:

The input would be an English sentence as a string, please transforms it as described below and return a new...

The sentence would contains only alphabet(a-z and A-Z) and space, each word would be separated by exactly...

space. There would be no spaces before and after the sentence.

Please return the string with each word spelled in reverse, however, the position of the capitalization of each ...

should stay the same for each word.

For example:

Input: This is an Apple on eBay

Output: sihT si na elppA no yaBe

<?php
function rev($arr) {
    $arr = explode(" ",$arr);
    foreach($arr as &$v){
        $v = strrev($v);
    }
    return implode(" ",$arr);
}
$arr = "This is an Apple on eBay";
$res = rev($arr);
print_r($res);

输出为:sihT si na elppA no yaBe

 

 

用 PHP 语言解决鸡兔同笼问题

用 PHP 语言解决鸡兔同笼问题,设已知总头数为H,总脚数为Y,求鸡兔各有多少只?

<?php
function fn($h,$y) {
    $maxC = $h;
    $minC = ceil($y/4);
    $c = $minC;
    $r = false;
    for(; $c<$maxC; $c++)
    {
        $r = $h - $c;
        if(($c*2 + $r*4) == $y) return $c;
    }
    return false;
}
$chick = fn(10,26);
if(false == $chick){
    echo "输入有误";
}else{
    $rabbit = 10 - $chick;
    echo "鸡:{$chick},兔:{$rabbit}";
}

输出为:鸡:7,兔:3

使用 PHP 描述选择排序算法

基本思想:在要排序的一组数中,选出最小的一个数(要遍历一下)与第一个位置的数交换;
然后在剩下的数当中再找最小的与第二个位置的数交换,如此循环到倒数第二个数和最后一个数比较为止。

提示:在查找最小元素时,一定要记录下标的位置

<?php
function select(&$arr)
{
    for($i=0; $i<count($arr)-1; ++$i)
    {
        //假设下标为$i的元素就是最小的数
        $min = $arr[$i];
        $minIndex = $i;
        //取得最小的元素及其下标
        for($j=$i+1; $j<count($arr); ++$j)
        {
            if($min>$arr[$j])
            {
                $min = $arr[$j];
                $minIndex = $j;
            }
        }
        // 最后交换
        $temp = $arr[$i];
        $arr[$i] = $arr[$minIndex];
        $arr[$minIndex] = $temp;
    }
    return $arr;
};
$arr = array(12,5,33,78,96,16,8,57,62);
$res = select($arr);
print_r($res);

结果:Array ( [0] => 5 [1] => 8 [2] => 12 [3] => 16 [4] => 33 [5] => 57 [6] => 62 [7] => 78 [8] => 96 )