|
 |
|
ใช้ function(); ใน str_replace ไม่ได้หรอครับ ช่วยผมหน่อยนะครับ |
|
 |
|
|
 |
 |
|
คือผมกำลัง หัดทำ php แบบเปลี่ยน template ได้อยู่อ่ะครับ
ไฟล์ template (templates/test1/sidebar.html)
Code
<h2>Category</h2>
<ul class="sidemenu">
{categories}
</ul>
<h2>Recent Blogs</h2>
<ul class="sidemenu">
{recentblogs}
</ul>
<h2>Popular Tags</h2>
<ul class="sidemenu">
<p>{tags}</p>
</ul>
ไฟล์ index.php
Code (PHP)
<?
include "config.php";
include "functions.php";
$template = "test1";
$categories = "<? categories(); ?>";
$recentblogs = "<? recentblogs(); ?>";
$tags = "<? include 'tags.php'; ?>";
$sidebar = file_get_contents("templates/$template/sidebar.html");
$sidebar = str_replace("{categories}", $categories ,$sidebar);
$sidebar = str_replace("{recentblogs}", $recentblogs ,$sidebar);
$sidebar = str_replace("{tags}", $tags ,$sidebar);
echo $sidebar;
?>
ผมไม่รู้ว่าผิดตรงไหน ยังหาไม่เจอ เวลา รันดูแล้วว่างเปล่าครับ
อ้อลองเอา function ไปดูด้วยนะครับ (functions.php)
Code (PHP)
<?
//categories
function categories()
{
$categories=mysql_query("SELECT * FROM categories");
while ($category=mysql_fetch_array($categories))
{
echo "<li><a href=\"?site=category&cat=$category[name]\">$category[name]</a></li>";
}
}
//function to Recent Blogs
function recentblogs()
{
$num_recent = "5";
$recentblogs=mysql_query("SELECT * FROM news order by id desc LIMIT 0 , $num_recent");
while ($recent=mysql_fetch_array($recentblogs))
{
$recent[title] = strip_tags($recent[title]);
echo "<li><a href='?site=news&id=$recent[id]' title='$recent[title]'>$recent[title]</a></li>";
}
}
?>
ไฟล์ tags.php
Code (PHP)
<?php
$values = '';
$result = mysql_query("SELECT tags FROM news");
while($row = mysql_fetch_array($result))
$values .= $row['tags']." ";
$values = explode(' ', $values);
$values=(array_count_values($values));
array_multisort($values, SORT_DESC);
$values=array_slice($values, 0,10);
//SHUFFLE
$randomized_keys = array_rand($values, count($values));
foreach($randomized_keys as $current_key) {
$values2[$current_key] = $values[$current_key];
}
//ZA IZRACUN
foreach ($values2 as $name=>$value)
{
$kolk+=$value;
$ss+=1;
}
//ZA IZRACUN
//ZA IZPIS
foreach ($values2 as $name=>$value)
{
$size=$tagsize*$value/$kolk;
echo "<a href='$urlweb/?site=search&keyword=$name'><strong>$name</strong></a> ";
}
//ZA IZPIS
?>
Tag : - - - -

|
|
 |
 |
 |
 |
Date :
2009-03-18 17:06:36 |
By :
Bung |
View :
1676 |
Reply :
8 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
$sidebar = str_replace("{categories}", $categories ,$sidebar);
$sidebar = str_replace("{recentblogs}", $recentblogs ,$sidebar);
$sidebar = str_replace("{tags}", $tags ,$sidebar);
เครื่องหมาย ตามกฎของ Regexp ต้องใส่ escage string นะครับ คงรู้นะว่าตัวไหนต้องใส่ 
|
 |
 |
 |
 |
Date :
2009-03-18 18:55:16 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตรงนี้คับ
$sidebar = str_replace("\{categories\}", $categories ,$sidebar);
$sidebar = str_replace("\{recentblogs\}", $recentblogs ,$sidebar);
$sidebar = str_replace("\{tags\}", $tags ,$sidebar);
http://gunner.freetzi.com/newVer
|
 |
 |
 |
 |
Date :
2009-03-19 10:42:06 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมไม่เข้าใจ
$sidebar = file_get_contents("templates/$template/sidebar.html");
$sidebar = str_replace("{categories}", $categories ,$sidebar);
$sidebar = str_replace("{recentblogs}", $recentblogs ,$sidebar);
$sidebar = str_replace("{tags}", $tags ,$sidebar);
หมายความว่าไงครับ คืนค่ามาแล้วไม่ใช้
นี่คับรูปแบบคำสั่ง
mixed str_replace ( mixed search, mixed replace, mixed subject [, int &count] )
|
 |
 |
 |
 |
Date :
2009-03-19 18:13:21 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือว่า
Code (PHP)
<?
$sidebar = file_get_contents("templates/$template/sidebar.html"); // เอา code ใน sidebar.html
$sidebar = str_replace("{categories}", $categories ,$sidebar); // หาคำว่า {categories} จาก sidebar.html แล้วแทนด้วย categories();
$sidebar = str_replace("{recentblogs}", $recentblogs ,$sidebar); // หาคำว่า {recentblogs} จาก sidebar.html แล้วแทนด้วย recentblogs();
$sidebar = str_replace("{tags}", $tags ,$sidebar); // หาคำว่า {tags} จาก sidebar.html แล้วแทนด้วย include 'tags.php';
echo $sidebar; //แสดงผลทั้งหมดที่ทำมา
?>
|
 |
 |
 |
 |
Date :
2009-03-19 21:12:19 |
By :
Bang |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอโทษที ไม่ต้อง ใส่ เอสเคป สตริงครับ
ผมว่าโค้ดคุณถูกต้องแล้ว เหลือแต่ว่า ไฟล์ที่ดึงมา เป็น UTF8 ซึ่งเก็บเป็น 4 ไบต์
ซึ่ง ฟังชั่น str_replace อาจเป็น ไบนารี่
ซึ่งเรื่องนี้ ผมก็ไม่ค่อยแน่ใจ ลองใช้ preg_replace หรือ ereg_replace ดูนะครับ หรือไม่ก็ลอง iconv เพื่อแปลงเป็น ไบต์ก่อน
หรือลอง
ereg_replace("\{categories\}", $str_to_replace, $str_to_search);
หรือ
preg_replace("{recentblogs}", $str_to_replace, $str_to_search);
ไม่แน่เหมือนกัน ใช้กับ UTF8 ได้ป่าว
|
 |
 |
 |
 |
Date :
2009-03-19 22:53:45 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คือผมว่า มันน่าจะเกี่ยวกับ function() รึป่าวครับ เพราะผมลองทำ ส่วนของ header.html แล้ว มันได้นะครับ
ไฟล์ header.html ผม อยู่ใน (templates/test1/header.html)
Code
<html>
<head>
<meta name="Keywords" content="your, keywords" />
<meta http-equiv="Content-Type" content="text/html; charset=TIS-620" />
<link rel="stylesheet" href="templates/{template}/images/{template}.css" type="text/css" />
<title>{title} - {site}</title>
</head>
<body>
<div id="wrap">
<div id="header">
<h1 id="logo-text"><a href="index.php">{title}</a></h1>
<p id="slogan">{description}</p>
</div>
ผมลองทำ index ดู ก็ใช้ได้นะครับ แบบนี้
Code (PHP)
<?
include "config.php";
include "functions.php";
$template = "test1";
$header = file_get_contents("templates/$template/header.html");
$header = str_replace("{template}", $template ,$header);
$header = str_replace("{title}", $title ,$header);
$header = str_replace("{site}", $site ,$header);
$header = str_replace("{description}", $description ,$header);
echo $header;
?>
พวกค่า $title / $site / $description พวกนี้ มันก็มาจาก
include "config.php";
include "functions.php";
นะครับ ก็แสดงผลได้ คือ เอา โครง html ใน header.html มาแล้ว หาพวก {template} / {title} / {site} /...
แล้วแทน ตัวแปรลงไป พอ echo $header; ดูผล มันก็แสดง ถูกต้อง
คือ แสดงโครงสร้าง html พร้อม เปลี่ยน พวก {...} ตรงส่วนที่แทน ออกเป็นค่าของตัวแปรนั้นๆ
แต่ มาส่วน sidebar ที่ผมทำมีปัญหา คือว่าอย่างที่บอกอ่ะครับ
ตัวบน ใช้ str_replace("{......}", $ตัวแปร,$header);
ส่วน sidebar ที่มีปัญหา เพราะ
ใช้ str_replace("{......}", function(); ,$header);
เป็นเพราะใช้แบบนี้ไม่ได้ ผมก็เลยลอง ทำให้กลับเป็นตัวแปรเหมือน header
เลยได้เป็น
Code (PHP)
<?
$categories = "<? categories(); ?>";
$recentblogs = "<? recentblogs(); ?>";
$tags = "<? include 'tags.php'; ?>";
$sidebar = file_get_contents("templates/$template/sidebar.html");
$sidebar = str_replace("{categories}", $categories ,$sidebar);
$sidebar = str_replace("{recentblogs}", $recentblogs ,$sidebar);
$sidebar = str_replace("{tags}", $tags ,$sidebar);
echo $sidebar;
?>
แต่ก็ไม่สำเร็จอ่ะครับ รบกวนหน่อยนะครับ ><"
|
 |
 |
 |
 |
Date :
2009-03-20 12:12:50 |
By :
Bang |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 04
|