[CodeIgniter]get_table_names_by_dbname

$this->db->list_tables();

上記だとキャッシュから取得する。キャッシュを回避する場合下記を使用。

public function get_table_names_by_dbname($dbname)
{
    $names = array();
    $sql = "SHOW TABLES FROM {$dbname}";
    $query = $this->db->query($sql);
    foreach ($query->result_array() as $row)
    {
        if ( ! isset($key))
        {
            if (isset($row['table_name']))
            {
                $key = 'table_name';
            }
            elseif (isset($row['TABLE_NAME']))
            {
                $key = 'TABLE_NAME';
            }
            else
            {
                $key = array_keys($row);
                $key = array_shift($key);
            }
        }

        $names[] = $row[$key];
    }
    return $names;
}

[CodeIgniter]set_header

$this->output->set_header('X-Frame-Options: SAMEORIGIN');
$this->output->set_header('Cache-control: no-cache, no-store, post-check=0, pre-check=0');
$this->output->set_header('Pragma: no-cache');
$this->output->set_header('Expires: Thu, 01 Dec 1994 16:00:00 GMT');

[PHP]zip

public function zip($zipfile,$add_file)
{
    $zip = new ZipArchive();
    $result = $zip->open($zipfile, ZipArchive::CREATE);
    if($result === true)
    {
        $zip->addFile($add_file);
        $result = $zip->close();
    }
    return $result === true;
}

[AWS]FILE権限

AmazonRDSは仕様上FILE権限は付与出来ない。
故に

LOAD DATA INFILE xxxx.csv INTO TABLE wp_names FIELDS TERMINATED BY ',' ENCLOSED BY '\"' IGNORE 1 LINES;

LOAD DATA LOCAL INFILE xxxx.csv INTO TABLE wp_names FIELDS TERMINATED BY ',' ENCLOSED BY '\"' IGNORE 1 LINES;

へ変更するとよい

※余談
windows上ではパスのセパレータを/に変換しないと動かないことも

$filepath = preg_replace('/\\\\/','/',$filepath);

[PHP]echo_json

function echo_json($data)
{
    header("Content-Type: application/json; charset=utf-8");
    echo json_encode($data);
}

[PHP]is_https

public function is_https()
{
    if(!empty($_SERVER["HTTPS"]))
        return true;
    //AWS
    if(!empty($_SERVER["HTTP_X_FORWARDED_PROTO"]) && strtolower($_SERVER["HTTP_X_FORWARDED_PROTO"])==="https")
        return true;
    return false;
}

[PHP]array_group_by

SQLのGROUP BYの様な処理を配列に行う

public function array_group_by($arr,$keys = array(array('id','address'),array('name') ),$index = 0)
{
    if(empty($arr))
        return null;
    $results = array();
    $children = array();
    $_vkey = $keys[$index][0];
    $_keys = array_slice($keys,0,$index+1);
    $is_final = count($keys) <= $index + 1;
    $id = null;
    array_push($arr,null);
    foreach ($arr as $i => $data){
        $current_id = $this->get_values_by_keys($data,$_keys,'');
        if($id != $current_id){
            if(!is_null($id)) {
                if(!empty($children)) {
                    $tmp = $this->get_std_by_array($children[0], $keys[$index]);
                    $tmp->value = $children[0]->$_vkey;
                }else{
                    $tmp = new stdClass();
                }
                $tmp->children = $is_final ? $children : $this->array_group_by($children,$keys,$index+1);
                $results[] = $tmp;
                $children = array();
            }
            $id = $current_id;
        }
        $children[] = $data;
    }
    return $results;
}
public function get_std_by_array($arr,$include_keys = null)
{
    $tmp = new stdClass();
    foreach ($arr as $key => $v){
        if(empty($include_keys) || (!empty($include_keys) && in_array($key,$include_keys)))
            $tmp->$key = $v;
    }
    return $tmp;
}
public function get_values_by_keys($data,$keys,$glue = null)
{
    if(empty($data))
        return null;
    $tmp = array();
    foreach ($keys as $key){
        if(is_array($key))
            $key = array_shift(array_values($key));
        $tmp[] = $data->$key;
    }
    return is_null($glue) ? $tmp : implode($glue,$tmp);
}

[MovableType]アーカイブマッピング:yyyymmdd_x.html

x
同じ日付の記事1つ目:1
同じ日付の記事2つ目:2

<mt:For regex_replace="/[\ \t\r\n]/mg","">
<mt:EntryDate format="%Y%m%d" setvar="current_entry_date" />
<mt:EntryID setvar="current_entry_id" />
<mt:EntryBlogID setvar="blog_id"/>
<mt:Blogs blog_ids="$blog_id">
<mt:Var name="count" value="1"/>
<mt:Var name="no" value="1"/>
<mt:Entries lastn="0" sort_by="authored_on" sort_order="ascend">
<mt:EntryDate format="%Y%m%d" setvar="entry_date" />
<mt:EntryID setvar="entry_id" />
<mt:If name="entry_date" eq="$current_entry_date">
<mt:If name="entry_id" eq="$current_entry_id"><mt:Var name="no" value="$count"/></mt:If>
<mt:SetVar name="count" op="++"/>
</mt:If>
</mt:Entries>
</mt:Blogs>
<mt:Var name="current_entry_date"/>_<mt:Var name="no"/>.html</mt:For>