[CodeIgniter]LoginControllerサンプル

LoginControllerサンプル

<?php
require_once 'BaseController.php';
class Login extends BaseController {
    protected $data = array();
    public function __construct()
    {
        parent::__construct();
        $this->load->model('admins');
    }

    public function index()
    {
        //postデータあり -> バリデーション
        if ($this->has_post_data()) {
            //バリデーション
            $result = $this->admins->validate('login');
            //バリデーションOK
            if($result){
                //メアド、パスワードDB照合
                $result = $this->admins->login();
                //メアド、パスワードDB照合OK
                if($result){
                    redirect('/');//ログイン済みホーム
                    return;
                }
            }
        }

        $this->view($this->get_template_name(),$this->data);
    }
}

AdminsModel

<?php
require_once 'BaseModel.php';
class Admins extends BaseModel {
	public $name = 'admins';
	public $validation = array(
		'login' => array(
			array(
				'field' => 'email',
				'label' => 'メールアドレス',
				'rules' => 'trim|xss_clean|required',
				'errors' => array(
					'required' => '%sは必須です。',
					'custom_validation' => '%sかパスワードが異なります。',
				),
			),
			array(
				'field' => 'password',
				'label' => 'パスワード',
				'rules' => 'trim|xss_clean|required',
				'errors' => array(
					'required' => '%sは必須です。',
				),
			),
		),
	);
	public function __construct()
	{
		parent::__construct();
	}
	public function login($email='',$password='')
	{
		if(empty($email)){
			$email = $this->post('email');
		}
		if(empty($password)){
			$password = $this->post('password');
		}
		$this->db->where('email',$email);
		$result = $this->db->get($this->name);
		if($result->num_rows() < 1){
			return false;
		}
		$result_data = $result->result('array');
		$data = array_shift($result_data);
		$login = password_verify($password,$data['password']);
		if($login){
			//ログイン成功
			$data = array(
				"email" => $email,
				"is_logged_in" => 1,
				"user_id" => $data['id'],
			);
			$this->session->set_userdata($data);
		}
		return $login;
	}
}

[MovableType]文字列分割プラグイン「Split」

文字列分割プラグイン「Split」

<MTSetVarBlock name="blog_path"><MTCanonicalURL with_index="1"></MTSetVarBlock>

<MTIgnore>プラグイン「Split」の機能</MTIgnore>
<MTVar name="blog_path" split="/" setvar="bp_parts">
<MTSetVarBlock name="filename"><MTVar name="pop(bp_parts)"></MTSetVarBlock>

[WordPress]アップロードフォルダを変える

アップロードフォルダを変える

/wp-admin/options.php へアクセス

/wp/にWordPressをインストールしており
/contents/uploadsにファイルをアップロードしたい場合
下記のように設定する。

upload_path
../contents/uploads

upload_url_path
http://yourdomain/contents/uploads

http://yourdomain/contents/でWordPressの内容を表示させている場合
http://yourdomain/.htaccess又はhttp://yourdomain/contents/.htaccessが存在するはず。
アップロードしたファイルが表示されない時は、rewrite ruleを修正して対応する。

[CodeIgniter]BaseController

共通関数を設定する時は必要。
各ControllerはこのBaseControllerをextendsする。しなくてもよい。

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class BaseController extends CI_Controller {

	public function __construct(){
		parent::__construct();

		if(!isset($_SERVER['PATH_INFO'])){
			$_SERVER['PATH_INFO'] = strtok($_SERVER['REQUEST_URI'],'?');
		}
		$this->data['class'] = $this->router->class;
		$this->data['method'] = $this->router->method;
		$this->data['uri'] = uri_string();

		//ログインしている
		if($this->session->userdata("is_logged_in")){
			//ログインページの時 -> ホームへリダイレクト
			if(in_array($this->data['class'],array('login'))){
				redirect('/');
			}

		//ログインしてない
		}else{
			//ログインページではない && 登録ページではない -> ログインページへリダイレクト
			if (!in_array($this->data['class'], array('login', 'regist'))) {
				redirect('/login/');
			}
		}
	}
	protected function get_template_name($name='')
	{
		return $this->get_class_method_string($name, '/', 2);
	}
	protected function get_class_method_string($name = '',$delimiter = '/',$backtrace_count = 1)
	{
		if(!empty($name)){
			return strtolower(get_class($this)) . $delimiter . $name;
		}
		$backtraces = debug_backtrace($limit=2);
		return strtolower(get_class($this)) . $delimiter . $backtraces[$backtrace_count]['function'];
	}
	protected function has_post_data()
	{
		return !empty($this->input->post());
	}
	protected function post($index = NULL, $xss_clean = NULL, $default = '')
	{
		$data = $this->input->post($index, $xss_clean);
		return empty($data) ? $default : $data;
	}
	public function view($template='',$data=[],$header='common/header',$footer='common/footer'){
		$this->load->view($header, $data);
		if(is_array($template)){
			foreach($template as $tmpl){
				$this->load->view($tmpl, $data);
			}
		}else {
			$this->load->view($template, $data);
		}
		$this->load->view($footer,$data);
	}
	protected function var_dump($data = null)
	{
		echo '<pre>';
		if(empty($data)){
			var_dump($this->data);
		}else{
			var_dump($data);
		}
		echo '</pre>';
	}
}

[MovableType]ページ分割プラグイン「PageBute」

ページ分割プラグイン「PageBute」
http://www.mtcms.jp/movabletype-blog/plugins/pagebute/

<MTIgnore>1ページに表示する記事数</MTIgnore>
<MTSetVar name="per_page" value="6">

<MTIgnore>プラグイン「PageBute」の機能</MTIgnore>
<MTPageContents count="$per_page">
<MTEntries sort_by="authored_on" sort_order="descend">
	<p><MTEntryTitle trim_to="30+..." encode_html="1"></p>
	<MTPageSeparator>
</MTEntries>
</MTPageContents>

[Swift]UITableView deleteRows

deleteRows

self.mainTableView.beginUpdates()

self.data.remove(at: 0)
self.mainTableView.deleteRows(at: [IndexPath(row:0, section: 0)], with: UITableViewRowAnimation.top)

self.mainTableView.endUpdates()

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return self.data.count
}

[PHP,MySQL]生年月日から年齢を算出する

生年月日から年齢を算出する

■PHP

$birth = '1952-01-26 00:00:00';
$age = (int) ((date('Ymd') - preg_replace('/([\- ]|[0-9]{2}:[0-9]{2}:[0-9]{2})/','',$birth)) / 10000);
echo $age;

結果

65

■MySQL

new_users

id name birth
1 1-名前 1941-01-26 17:30:56
2 2-名前 1952-07-07 00:30:56
3 3-名前 1976-09-02 17:30:56
4 4-名前 1990-05-13 05:00:00
5 5-名前 2016-11-26 17:30:56
6 6-名前 1999-04-01 11:11:11

SQL

SELECT
id,
name,
birth,
DATE_FORMAT(birth, '%Y%m%d') AS tmp_birth,
DATE_FORMAT(NOW(), '%Y%m%d') AS now_date,
FLOOR((DATE_FORMAT(NOW(), '%Y%m%d') - DATE_FORMAT(birth, '%Y%m%d')) / 10000) AS age
FROM new_users
ORDER BY id ASC

結果

id name birth tmp_birth now_date age
1 1-名前 1941-01-26 17:30:56 19410126 20170126 76
2 2-名前 1952-07-07 00:30:56 19520707 20170126 64
3 3-名前 1976-09-02 17:30:56 19760902 20170126 40
4 4-名前 1990-05-13 05:00:00 19900513 20170126 26
5 5-名前 2016-11-26 17:30:56 20161126 20170126 0
6 6-名前 1999-04-01 11:11:11 19990401 20170126 17

[WordPress]ざっくりテンプレ

ざっくりテンプレ

index.php

<!DOCTYPE html>
<html lang="ja">
<?php get_header('Meta'); ?>
<body>
<?php get_header('Top'); ?>

<div id="main" class="clearfix">
<?php get_sidebar('L'); ?>

<div id="content">
	<h2>
		<?php
			if( is_single() ){/* 個別記事 */
				the_title();
			}else if( is_page() ){/* 個別ページ */
				the_title();
			}else if( is_category() ){/* カテゴリーアーカイブ */
				echo 'category : ';
				echo get_cat_name( wp_specialchars($cat,1) );
			}else if( is_search() ){/* 検索結果 */
				echo 'search : ' . wp_specialchars($s, 1);
			}else if( is_archive() ){/* アーカイブ */
				echo 'archive : ' . wp_specialchars($m, 1);
			}else{
				echo 'Home';
			}
		?>
	</h2>

<?php if( is_page() ): ?>	

	<?php if(have_posts()):the_post(); ?>
			<?php if( get_post_status()!='private' && get_post_status()!='pending' ): ?>	
				<?php get_template_part("original-page-$page_id"); ?>
			<?php endif; ?>
	<?php endif; ?>

<?php else: ?>

	<?php if(have_posts()):while(have_posts()):the_post(); ?>
		<?php if( get_post_status()!='private' && get_post_status()!='pending' && !is_page() ): ?>	
			<article>
				<div class="article-inner link-dotted word-break">
					<div class="article-header">
						<a href="<?php the_permalink(); ?>">
							<strong><?php the_title(); ?></strong>
						</a>
						<small class="article-time"><?php the_time('Y年n月j日'); ?></small>
					</div>

					<div class="article-text">
						<?php
							the_content();
						?>
					</div>

					<div class="article-footer clearfix">
						<?php get_template_part('footer-article'); ?>
					</div><!--article-footer-->
				</div><!--article-inner-->
			</article>
		<?php endif; ?>
		<?php endwhile;?>
	<?php else: ?>
			<article>
				<div class="article-inner link-dotted word-break">
					<div class="article-header">
						<img class="article-image" src="<?php echo get_template_directory_uri().'/icon/icon-404.png' ?>"/>
						<strong>
							<?php if (!have_posts() ): ?>
								[Not Found]見つかりませんでした。
							<?php endif; ?>
						</strong>
						<small class="article-time"><?php echo date('Y年m月d日'); ?></small>
					</div>
					<div class="article-text">
						[Not Found]見つかりませんでした。
					</div>
					<div class="article-footer clearfix">
						
					</div><!--article-footer-->
				</div><!--article-inner-->
			</article>
	<?php endif; ?>

<?php endif; ?>

</div><!--content-->

<?php get_sidebar('R'); ?>

</div><!--main-->

<?php get_footer(); ?>

</body>
</html>

header-Meta.php

<head>
	<meta charset="UTF-8" />
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title></title>
	<meta name="author" content="ktyr report" /> 
	<meta name="copyright" content="ktyr report" />
	<meta name="keywords" content="">
	<meta name="description" content="">
	<link href="<?php echo get_stylesheet_directory_uri(); ?>/icon/favicon.ico?t=2" type="image/x-icon" rel="icon" />
	<link href="<?php echo get_stylesheet_directory_uri(); ?>/icon/favicon.ico?t=2" type="image/x-icon" rel="shortcut icon" />
	<link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_directory_uri(); ?>/common/css/common.css" media="screen" />
	<script type="text/javascript" src="<?php echo get_stylesheet_directory_uri().'/common/js/jquery-2.0.3.min.js'; ?>"></script>
</head>

header-Top.php

<header>
	<div class="header-inner">
		<div class="header-inner-inner">
			<h1 id="blog-name"><a href="<?=home_url()?>">wp template</a></h1>
		</div>
	</div>
</header>

sidebar-L.php

<div id="sidebarL">
	<section class="relative">
		<form class="mb-14" role="search" method="get" id="searchform" action="<?=home_url()?>" >
			<input type="search" class="" id="s" name="s" placeholder="Search">
			<span class="search-icon">
				<button type="submit">
					<span class="hidden">検索</span>
				</button>
			</span>
		</form>
	</section>
	<section>
		<select class="w-sidebarL" name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'> 
			<option value="">archives</option> 
			<?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?>
		</select>
	</section>
</div>

sidebar-R.php

<div id="sidebarR">
	<div class="roundbox">
	</div>
</div>

footer.php

<footer>
	<div class="footer-inner">powered by <a href="http://wordpress.org/" target="_blank">WordPress</a></div>
</footer>

footer-article.php

<input type="text" value="<?php the_permalink(); ?>" readonly>

original-page-2.php

オリジナルページ2