ถ้ากำหนด Link ของปุ่มเปลี่ยนภาษา ดังนี้
ภาษาไทย => http://localhost/..../index.php?lang=th
ภาษาอังกฤษ => http://localhost/..../index.php?lang=en
โดยขั้นโค้ดมีดังนี้ค่ะ
1. ไฟล์ conn.php อยู่ภายในไฟล์ Joomla ในเครื่องค่ะ ภายในไฟล์จะเช็คค่า GET ที่ส่งมา และเก็บไว้ที่ตัวแปร ก่อนจะนำไปใช้เลือกข้อความตามภาษาที่กำหนด ดังนี้ค่ะ
โค้ด: เลือกทั้งหมด
<?php
require_once '../../configuration.php';
$joomla3 = new JConfig();
$host = $joomla3->host; // localhost
$username = $joomla3->user;
$password = $joomla3->password;
$db = $joomla3->db;
$data_rate = 'tools_rate';
$con = mysqli_connect($host,$username,$password,$db);
mysqli_query($con,'set names utf8');
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// ส่วน 2 ภาษา
$lang = isset($_GET['lang']) ? $_GET['lang'] : 'th' ;
$trans_here = array('th' => 'คุณอยู่ที่ : ', 'en' => 'You are here : ');
$here_trans = $trans_here[$lang];
$trans_pv = array('th' => 'จำนวนเงินในปัจจุบัน (PV)', 'en' => 'Present Value or PV');
$pv_trans = $trans_pv[$lang];
$trans_information = array('th' => 'ข้อมูลผู้ใช้', 'en' => 'Information');
$information_trans = $trans_information[$lang];
$trans_view = array('th' => 'ดูข้อมูลเพิ่มเติม', 'en' => 'View');
$view_trans = $trans_view[$lang];
?>
โค้ด: เลือกทั้งหมด
<?php
require_once "conn.php";
?>
<form method="post" action="" name="from">
<span class="input-group-addon alert-info" id="basic-addon1"><?php echo $pv_trans ?></span> <!--ระบุหรือไม่ก็ได้-->
<input type="number" required step="0.01" id="fv" name="pv" autocomplete="off" placeholder="0.00" value="">
<button class="btn btn-success" type="submit" id="r1" name="submit" >Calculate</button>
<br/>
<?php
if (isset($_POST['submit'])) {
$pv = $_POST['pv'];
?>
<h3><?php echo $information_trans?></h3> <!--Information-->
<?php echo $pv_trans ?> <!--PV-->
<?php echo $pv ; ?>
<?php
$sql = "INSERT INTO $data_rate (present_money)" . "VALUES ('$pv')";
mysqli_query($con, $sql) or die('*** ไม่สามารถบันทึกข้อมูลได้ ***');
}
?>
<div>
</div>
<br/>
<table>
<thead>
<tr>
<th>
<p class="text-center">ID</p>
</th>
<th>
<p class="text-center">PV</p>
</th>
<th>
<p class="text-center"><?php echo $view_trans?></p>
</th>
</tr>
</thead>
</table>
<?php
$sql = "SELECT * FROM $data_rate ORDER BY id desc Limit 10";
$result = mysqli_query($con, $sql);
while ($fetch = mysqli_fetch_assoc($result)) {
?>
<table>
<tbody>
<tr>
<td class="text-center">
<?php echo $fetch['id']; ?>
</td>
<td class="text-right">
<?php echo number_format($fetch['present_money'],2); ?>
</td>
<td class="text-center">
<a href="showrate.php?id=<?php echo $fetch['id']; ?>">
<i>View</i>
</a>
</td>
</tbody>
</table>
</tr>
<?php
}
?>
</table>
</form>
- - เมื่อกดปุ่ม Calculate และกรอกข้อมูลในช่องว่างแล้วจะแสดงผลลัพธ์ออกมา ในตัวอย่างจะแสดง จำนวนเงินในปัจจุบัน (PV) 5
- - เมื่อกดปุ่ม Calculate และกรอกข้อมูลในช่องว่างแล้วจะแสดงผลลัพธ์ออกมา ในตัวอย่างจะแสดง Present Value or PV 9
โค้ด: เลือกทั้งหมด
<?php
ob_start();
require_once'conn.php';
?>
<?php
if (isset($_GET['id'])) {
$sql = "SELECT * FROM $data_rate WHERE id = " . $_GET['id'];
$result = mysqli_query($con, $sql);
$fetch = mysqli_fetch_assoc($result); //on line 9
}
?>
<?php
$datamoney = (isset($fetch['present_money'])) ? $fetch['present_money'] : '';
?>
<form method="post" action="" name="from">
<span class="input-group-addon alert-info" id="basic-addon1"><?php echo $pv_trans ?></span> <!--ระบุหรือไม่ก็ได้-->
<input type="number" required step="0.01" id="fv" name="pv" autocomplete="off" placeholder="0.00" value="<?php echo $datamoney ?>">
<button class="btn btn-success" type="submit" id="r1" name="submit" >Calculate</button>
<?php
if (isset($_GET['id'])) {
echo '<input type = "hidden" name = "id" value = "' . $_GET['id'] . '" />';
}
if (isset($_POST['submit'])) {
$pv= $_POST['pv'];
$sql = "INSERT INTO $data_rate (present_money)" . "VALUES ('$pv')";
mysqli_query($con, $sql) or die('*** ไม่สามารถบันทึกข้อมูลได้ ***');
$last_id = mysqli_insert_id($con);
header("location: ../rate/showrate.php?id=$last_id");
exit(0);
} ?>
<h3><?php echo $information_trans?></h3> <!--Information-->
ID: <?php echo $fetch['id']; ?><br> <!--PV-->
<?php echo $pv_trans . $fetch['present_money']; ?>
</form>
<div class="col-md-13 table-responsive" class="text-center">
<table>
<thead>
<tr>
<th>
<p class="text-center">ID</p>
</th>
<th>
<p class="text-center">PV</p>
</th>
<th>
<p class="text-center"><?php echo $view_trans?></p>
</th>
</tr>
</thead>
<?php
$sql = "SELECT * FROM $data_rate ORDER BY id desc Limit 10";
$result = mysqli_query($con, $sql);
while ($fetch = mysqli_fetch_assoc($result)) {
?>
<tr>
<td class="text-center">
<?php echo $fetch['id']; ?>
</td>
<td class="text-right">
<?php echo number_format($fetch['present_money'],2); ?>
</td>
<td class="text-center">
<a href="showrate.php?id=<?php echo $fetch['id']; ?>">
<i>View</i>
</a>
</td>
</tr>
<?php
}
?>
</table>
</div>
<?php include '../footer.php';?>
แต่เมื่อเรียกใช้ showrate.php จากหน้า index.php?lang=en ผลลัพธ์ที่ได้เป็นหน้า showrate.php ภาษาไทยและเมื่อพิมพ์ url showrate.php?id=133?lang=en ดังภาพจะขึ้น Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in .... showrate.php on line 9 ดังภาพ
ซึ่งบรรทัดที่ 9 ของ showrate.php คือ
โค้ด: เลือกทั้งหมด
<?php
if (isset($_GET['id'])) {
$sql = "SELECT * FROM $data_rate WHERE id = " . $_GET['id'];
$result = mysqli_query($con, $sql);
$fetch = mysqli_fetch_assoc($result); //on line 9
}
?>