|
 |
|
คือผมจะทำ listmenu เว็บ2ภาษา โดยดึงข้อมูลจากฐานข้อมูลมาโชว์ |
|
 |
|
|
 |
 |
|
ถ้าจะให้ง่ายก็ใช้ session กับ if ครับ
Code (PHP)
<?
session_start();
if($_SESSION["lang"] == "EN")
{
include("en.php");
}
else
{
include("th.php");
}
?>
Code (th.php)
<?
$strTitle = "ยินดีต้อนรับเข้าสู่เว็บไซต์ของฉัน";
$strHome = "หน้าแรก";
$strService = "บริการของเรา";
$strAbout = "เกี่ยวกับเรา";
$strContact = "ติดต่อเรา";
//** สมมุติค่ามาจาก Database ที่ได้จากการ Query ***//
$result["DETAIL_TH"] = "นี้คือเนื้อหาภาษาไทย";
$strDetail = $result["DETAIL_TH"];
?>
Code (en.php)
<?
$strTitle = "Welcome to My Web Site";
$strHome = "Home";
$strService = "Service";
$strAbout = "About Us";
$strContact = "Contact Us";
//** สมมุติค่ามาจาก Database ที่ได้จากการ Query ***//
$result["DETAIL_EN"] = "This is content English";
$strDetail = $result["DETAIL_EN"];
?>
Go to : PHP Multiple Language การใช้ PHP ทำเว็บ 2 ภาษาแบบง่าย ๆ
|
 |
 |
 |
 |
Date :
2011-08-29 13:04:54 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อย่าหาว่าผมโง่เลยนะครับตรงไฟล์ th.php กับ en.php ที่บอกว่า
//** สมมุติค่ามาจาก Database ที่ได้จากการ Query ***//
$result["DETAIL_EN"] = "This is content English";
$strDetail = $result["DETAIL_EN"];
ไม่ทราบว่ามันจะเขียนคิวรี่ออกมายังไงครับผม ยังงงๆการส่งค่าอ่ะครับ ไม่ทราบว่าท่านใดพอจะรู้บ้างอ่ะครับ
ขอบคุณครับ 
|
ประวัติการแก้ไข 2011-08-29 22:55:41
 |
 |
 |
 |
Date :
2011-08-29 22:54:34 |
By :
teedesign |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำไมผมส่งค่า $strDetail ไปหน้า menu.php แล้วมันวนลูปชื่อสินค้าออกมาแค่อันเดียวครับ แต่พอ echo $strDetail ในหน้า th.php กับ en.php มันทำไมวนลูปชื่อสินค้าออกมาหมดอ่ะครับ ไม่ทราบว่าเป็นเพราะอะไรครับ 
connect.php
<?php
$host="localhost";
$user="root";
$pw="root";
$dbname="translate";
$c = mysql_connect($host, $user, $pw);
mysql_query("SET NAMES UTF8") or die(mysql_error());
if (!$c) {
echo "<h3>ERROR : ไม่สามารติดต่อฐานข้อมูลได้</h3>";
exit();
}
?>
th.php
<?
include ('connect.php');
$strTitle = "ยินดีต้อนรับเข้าสู่เว็บไซต์ของฉัน";
$strHome = "หน้าหลัก";
$strService = "บริการ";
$strAbout = "เกี่ยวกับเรา";
$strContact = "ติดต่อเรา";
//** สมมุติค่ามาจาก Database ที่ได้จากการ Query ***//
$sql = "select * from type_th";
$result=mysql_db_query($dbname,$sql);
while ($rs=mysql_fetch_array($result)){
$strDetail=$rs[name_th];
}
?>
en.php
<?
include ('connect.php');
$strTitle = "ยินดีต้อนรับเข้าสู่เว็บไซต์ของฉัน";
$strHome = "หน้าหลัก";
$strService = "บริการ";
$strAbout = "เกี่ยวกับเรา";
$strContact = "ติดต่อเรา";
//** สมมุติค่ามาจาก Database ที่ได้จากการ Query ***//
$sql = "select * from type_th";
$result=mysql_db_query($dbname,$sql);
while ($rs=mysql_fetch_array($result)){
$strDetail=$rs[name_en];
}
?>
change.php
<?
session_start();
$_SESSION["lang"] = $_GET["lang"];
session_write_close();
header("location:menu.php");
?>
menu.php
<?
session_start();
if($_SESSION["lang"] == "EN")
{
include("en.php");
}
else
{
include("th.php");
}
?>
<html>
<head>
<title><?=$strTitle;?></title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874">
</head>
<body>
<table width="452" border="1" cellpadding="1" cellspacing="1">
<tr>
<td><?=$strHome;?></td>
<td><?=$strService;?></td>
<td><?=$strAbout;?></td>
<td><?=$strContact;?></td>
</tr>
</table>
<br>
<br>
<?=$strDetail;?>
<br>
<br>
<br>
<strong>Language</strong><br>
<a href="change.php?lang=TH">TH</a> , <a href="change.php?lang=EN">EN</a>
</body>
</html>
|
ประวัติการแก้ไข 2011-08-30 11:20:43 2011-08-30 11:21:59
 |
 |
 |
 |
Date :
2011-08-30 11:19:40 |
By :
teedesign |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|