สอบถาม PATH URL PHP 2 ภาษา ดึงค่ามาจากฐานข้อมูลไม่ได้ค่ะ

สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ คนที่มีความรู้ แบ่งปันคนอื่นบ้างนะ ปัญหาการเขียนโปรแกรม แบบ OOP Session Cookies php network

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
pprn
PHP Super Hero Member
PHP Super Hero Member
โพสต์: 565
ลงทะเบียนเมื่อ: 02/07/2018 10:45 am

สอบถาม PATH URL PHP 2 ภาษา ดึงค่ามาจากฐานข้อมูลไม่ได้ค่ะ

โพสต์ที่ยังไม่ได้อ่าน โดย pprn »

ต้องการทำหน้าเว็บ 2 ภาษา จากภาษาไทยเป็นภาษาอังกฤษ

ถ้ากำหนด 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];

?>
2. ไฟล์ index.php ที่ต้องการแปล 2 ภาษา

โค้ด: เลือกทั้งหมด

<?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>
ผลลัพธ์ไฟล์ index.php ภาษาไทย
  • th1.JPG
    th1.JPG (53.84 KiB) Viewed 2953 times
  • - เมื่อกดปุ่ม Calculate และกรอกข้อมูลในช่องว่างแล้วจะแสดงผลลัพธ์ออกมา ในตัวอย่างจะแสดง จำนวนเงินในปัจจุบัน (PV) 5
  • th2.JPG
    th2.JPG (61.79 KiB) Viewed 2953 times
ผลลัพธ์ไฟล์ index.php ภาษาอังกฤษ
  • en1.JPG
    en1.JPG (49.66 KiB) Viewed 2952 times
  • - เมื่อกดปุ่ม Calculate และกรอกข้อมูลในช่องว่างแล้วจะแสดงผลลัพธ์ออกมา ในตัวอย่างจะแสดง Present Value or PV 9
  • en2.JPG
    en2.JPG (55.61 KiB) Viewed 2952 times
3.ไฟล์ showrate.php คือ ไฟล์ที่ต้องการแสดงค่าที่ดึงมาจากฐานข้อมูล จะแสดงค่าที่ fetch มาจาก database เมื่อผู้ใช้กดปุ่ม view จากหน้า index.php

โค้ด: เลือกทั้งหมด

<?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 ภาษาไทย
  • th3.JPG
    th3.JPG (66.46 KiB) Viewed 2952 times
ปัญหา
แต่เมื่อเรียกใช้ 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 ดังภาพ
  • en3.JPG

ซึ่งบรรทัดที่ 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
   }
   ?>
สอบถามว่ามี code ตรงไหนในส่วนของหน้า showrate ที่ดึงค่าจาก database มาผิดในส่วนไหนคะ
ภาพประจำตัวสมาชิก
thatsawan
PHP VIP Members
PHP VIP Members
โพสต์: 28508
ลงทะเบียนเมื่อ: 31/03/2014 10:02 am
ติดต่อ:

Re: สอบถาม PATH URL PHP 2 ภาษา ดึงค่ามาจากฐานข้อมูลไม่ได้ค่ะ

โพสต์ที่ยังไม่ได้อ่าน โดย thatsawan »

ส่ง Get 1 ตัวขึ้นไป จะใช้ & ช่วย

โค้ด: เลือกทั้งหมด

showrate.php?id=133&lang=en

ตัวอย่าง
https://www.mindphp.com/forums/viewtopic.php?f=6&t=50055

โค้ด: เลือกทั้งหมด

viewtopic.php?f=6&t=50055
ภาพประจำตัวสมาชิก
pprn
PHP Super Hero Member
PHP Super Hero Member
โพสต์: 565
ลงทะเบียนเมื่อ: 02/07/2018 10:45 am

Re: สอบถาม PATH URL PHP 2 ภาษา ดึงค่ามาจากฐานข้อมูลไม่ได้ค่ะ

โพสต์ที่ยังไม่ได้อ่าน โดย pprn »

thatsawan เขียน:ส่ง Get 1 ตัวขึ้นไป จะใช้ & ช่วย

โค้ด: เลือกทั้งหมด

showrate.php?id=133&lang=en

ตัวอย่าง
https://www.mindphp.com/forums/viewtopic.php?f=6&t=50055

โค้ด: เลือกทั้งหมด

viewtopic.php?f=6&t=50055

ได้แล้วค่ะ ขอบคุณค่ะ :gfb:
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 113