首页 > 解决方案 > 如何根据用户选择功能更改在 echo 脚本中使用的给定 $variable 值

问题描述

首先,我有一些 php 代码检查是否记录了哪些用户,以便为他们提供各自的帐户值。之后,有几个变量的一些值会导致数据库上的不同文件链接。在设置变量后,这些变量在脚本内用于回显输出上的映射。结论是我想在应用程序中实现语言更改,但为了做到这一点,变量上的值需要更改为相应的语言值。

For example: Initially the $barsis set to $row['barsgr'], what we want is that when the language is selected, the $bars will change to $row['barseng].

我尝试过使用案例,但我不知道如何在当前情况下实现它。

<?php
  $sql = "SELECT * FROM users WHERE uidUsers = '" . $_SESSION['userUid'] . "'";
  $result = mysqli_query($conn, $sql);
  $resultCheck = mysqli_num_rows($result);
  if ($resultCheck > 0) {
    while ($row = mysqli_fetch_assoc($result)){
      $bars = $row['barsgr'];
      $food = $row['foodgr']; 
echo "<script>I am a map script that uses the variable $bars to show a layer but want to change the value when another language is picked so I show the translated layer from another column</script>";
?>

我希望当在 php 代码之外选择语言选择器时,php 代码中的变量会更改为它们各自的语言。

例如有人选择英语, $bars = $row[barsgr];更改为$bars = $row[barseng];

标签: phpvariables

解决方案


也许这样的事情可以帮助:

<?php

  $lang= $_POST['lang'] // Or maybe $_SESSION['lang'] if you store it there
  if(!isset($lang)) {
    $lang = 'gr';
  }
  $sql = "SELECT * FROM users WHERE uidUsers = '" . $_SESSION['userUid'] . "'";
  $result = mysqli_query($conn, $sql);
  $resultCheck = mysqli_num_rows($result);
  if ($resultCheck > 0) {
    while ($row = mysqli_fetch_assoc($result)){
      $bars = $row['bars'.$lang];
      $food = $row['food'.$lang]; 
echo "<script>I am a map script that uses the variable $bars to show a layer but want to change the value when another language is picked so I show the translated layer from another column</script>";
?>

推荐阅读