Saturday, June 14, 2008

MVC コントローラー URLによる動的にモジュールを呼ぶ

----- test.php
<?php

/*
e.g
test.php/c=user_proile/m=register/

*/

$path_info = explode('/', $_SERVER['PATH_INFO']);
while(list($cnt,$path) = each($path_info)) {
if ($path == "") continue;
list($key,$value) = explode('=', $path);
if ($key =="c") {
$MyClass = $value;
continue;
}
if ($key =="m") {
$MyMethod = $value;
continue;
}

$_GET[$key] = $value;
}

// overloading is also ok, if PHP5.
if ($MyClass != "" && is_file($MyClass.".php")) {
include_once($MyClass.".php");
if (class_exists($MyClass)) {
$MyModel = new $MyClass;
if (method_exists($MyModel,"dispatch")) {
$MyModel->dispatch($MyMethod);
}
}
}


?>
-----user_proile.php

<?php
class user_proile
{
function dispatch($action)
{
echo $action;

}


}
?>

refer to:
2008/03/php-mvc-controller.html

2005/01/pathinfo.html

Thursday, June 12, 2008

PHP4 でのRSS リーダー sample

http://keithdevens.com/software/phpxml
からPHP XML Libraryをとってくる。


<?php // Load and parse the XML document

$url = 'http://hogehoge/blog/?feed=rss2';

$xml_data1 = file_get_contents($url) or die();

require_once('xml.php');
$xml_array = XML_unserialize($xml_data1);
$items = $xml_array['rss']['channel']['item'];
unset ($xml_array);
unset ($xml_data1);

foreach ($items as $item) {
echo "<h3><a href='" . $item['link'] . "'>" . $item['title'] . "</a></h3>";
echo "<p>" . mb_strimwidth($item['description'], 0, 60, "...",'utf8') . "</p>";
break;

}
unset ($items);


?>

php ファイル名の一覧の取得

<?php

$regex = "([^\/]+).mp3$";
$dir =".";
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ( preg_match ( "/".$regex."/", $file, $matches) ) {
echo $file;
$files[] = $file;
// $matches[1]で名前だけとるのもあり。
}
}
closedir($handle);
}

?>

Tuesday, June 10, 2008

rss reader by PHP5 (sample)

<?php

$feed = 'http://test.com/?feed=rss2';
$rss = simplexml_load_file($feed);
$title = $rss->channel->title;
?>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>

<h1><?php echo $title; ?></h1>

<?php
foreach ($rss->channel->item as $item) {
echo "<h3><a href='" . $item->link . "'>" . $item->title . "</a></h3>";
$pdate = $item->pubDate;
echo date("Y/m/d G:H:i",strtotime($pdate));
echo "<p>" . $item->description . "</p>";
echo "<p>" . $item->category . "</p>";
}
?>

</body>
</html>

Wednesday, June 04, 2008

WordPressで独自ページをつくるサンプル

<?php

define('WP_USE_THEMES', false);
require('wp-blog-header.php'); // 共通関数を使うため
get_header();// テンプレートを仕様

$sql = "SELECT category_count FROM " .$wpdb->categories . " WHERE cat_ID=1" ;
echo $wpdb->get_var($sql);
get_footer(); // テンプレートを仕様

?>

-------------------------------
<?php
require('wp-blog-header.php'); // 共通関数を使うため
$option = 'cat=3&showposts=100&year=2008&order=ASC';

?>
<?php query_posts($option); ?>
<?php if(have_posts()):while(have_posts()):the_post(); ?>
<p><?php the_time('Y.m.d'); ?></p>
<p><a href="detail.php?id=<?php the_ID(); ?>"><?php echo $post->post_title ; ?></a>
</p>
<p><?php the_content(); ?></p>

<?php endwhile;endif; ?>

---------

カテゴリ指定の年ごとのリンクの作成

$category_id = 3;
$sort =" ASC";

$arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS year, count(ID) as posts
FROM $wpdb->posts, $wpdb->term_relationships
WHERE
$wpdb->posts.ID = $wpdb->term_relationships.object_id
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->term_relationships.term_taxonomy_id = '$category_id'
GROUP BY YEAR(post_date) ORDER BY post_date " . $sort);

foreach ($arcresults as $arcresult) {
echo "<li><a href=\"/?year=".$arcresult->year."\">".$arcresult->year."年</a></li>";
}


----
bloggerの投稿するときの文字の確認を入力、読めない

Tuesday, June 03, 2008

PHP memcache のインストール

簡単にインストールできると思ったら失敗したのでここに記す。

pecl download memcache

pecl install memcache-2.2.3.tgz << 落としたものにする

インストールに設定したら、php.iniの編集

extension=memcache.so

動かない場合は インストール成功時に表示されるパスを書けばOK
extension="/usr/local/lib/php/extensions/no-debug-non-zts-20060613/memcache.so"

最後にapacheの再起動
再起動をしないとphpは反映されません。