[PHP,MT]phpでMTのデータ取得
phpでMTのデータ取得
public $basepath = ''; public $cfg_file = 'mt-config.cgi'; public $blog_id = null; public $mt; public function __construct($blog_id = null) { $this->basepath = dirname(dirname(dirname(__FILE__))) . '/mt'; set_include_path(get_include_path() . PATH_SEPARATOR . $this->basepath . '/php' . PATH_SEPARATOR . $this->basepath . '/php/extlib' . PATH_SEPARATOR . $this->basepath . '/MT/php/lib'); require_once('mt.php'); $this->init_mt($blog_id); } public function init_mt($blog_id = null) { $this->blog_id = $blog_id; $this->mt = MT::get_instance($blog_id, $this->cfg_file); } public function get_custom_fields_by_entry_id($entry_id) { $sql[] = "SELECT"; $sql[] = "entry_meta_entry_id as entry_id,"; $sql[] = "REPLACE(entry_meta_type,'field.','') as name,"; $sql[] = "(CASE"; $sql[] = "WHEN entry_meta_vchar IS NOT NULL THEN entry_meta_vchar"; $sql[] = "WHEN entry_meta_vchar_idx IS NOT NULL THEN entry_meta_vchar_idx"; $sql[] = "WHEN entry_meta_vdatetime IS NOT NULL THEN entry_meta_vdatetime"; $sql[] = "WHEN entry_meta_vdatetime_idx IS NOT NULL THEN entry_meta_vdatetime_idx"; $sql[] = "WHEN entry_meta_vinteger IS NOT NULL THEN entry_meta_vinteger"; $sql[] = "WHEN entry_meta_vinteger_idx IS NOT NULL THEN entry_meta_vinteger_idx"; $sql[] = "WHEN entry_meta_vfloat IS NOT NULL THEN entry_meta_vfloat"; $sql[] = "WHEN entry_meta_vfloat_idx IS NOT NULL THEN entry_meta_vfloat_idx"; $sql[] = "WHEN entry_meta_vblob IS NOT NULL THEN entry_meta_vblob"; $sql[] = "WHEN entry_meta_vclob IS NOT NULL THEN entry_meta_vclob"; $sql[] = "ELSE NULL END) as value"; $sql[] = "FROM mt_entry_meta WHERE entry_meta_entry_id " . $this->_get_sql_for_in($entry_id,true); $sql = implode(' ',$sql); $records = $this->mt->db()->execute($sql); $records = $this->get_array_by_records($records); $tmp = array(); if(!is_array($entry_id)){ foreach ($records as $rec) { $tmp[$rec['name']] = $rec['value']; } return $tmp; } return $records; } protected function _get_sql_for_in($values, $is_number = false) { if ($is_number) return is_array($values) && 1 < count($values) ? "IN (" . implode(",", $values) . ")" : (is_array($values) ? "= {$values[0]}" : "= $values"); return is_array($values) && 1 < count($values) ? "IN ('" . implode("','", $values) . "')" : (is_array($values) ? "= '{$values[0]}'" : "= '$values'"); } public function get_array_by_records($records) { $result = array(); if (!$records){ }else { while ($item = $records->fetchRow()) { $result[] = $item; } } return $result; }