我通过表单发布了两个值:
$lang = "fr"
$about_xx = "hello, world!
我现在需要使用$about_xx
变量的值将$about_en
的名称更改为$lang
。这可能吗?
编辑:添加了代码以获得更多清晰度和帮助。此实例中的变量$lang
的值为"fr"
。比较部分需要修改以支持我希望创建的这些新变量,而不是结束_en
// Get POST Variables
$expo_id = $_POST['expo_id'];
$lang = $_POST['lang'];
${"new_expo_url_" . $lang} = $_POST['expo_url_lang'];
${"new_expo_venue_" . $lang} = $_POST['expo_venue_lang'];
${"new_expo_country_" . $lang} = $_POST['expo_country_lang'];
${"new_expo_about_" . $lang} = $_POST['expo_about_lang'];
// Get Current Variables
$update_expo = mysql_query("SELECT expo_id, expo_url_{$lang}, expo_venue_{$lang}, expo_country_{$lang}, expo_about_{$lang} FROM exhibitions WHERE expo_id = $expo_id LIMIT 1") or die(mysql_error());
if (mysql_num_rows($update_expo) == 0) {
header("Location: index.php?expo_id=" . $expo_id . "&i=no-expo");
exit();
}
// Compare
$update_lang_append = "";
while ($ula = mysql_fetch_assoc($update_expo)) {
if ($ula['expo_venue_en'] !== $new_expo_venue_en) {
$update_lang_append .= "expo_venue_en = '" . $new_expo_venue_en . "', ";
}
if ($ula['expo_country_en'] !== $new_expo_country_en) {
$update_lang_append .= "expo_country_en = '" . $new_expo_country_en . "', ";
}
if ($ula['expo_about_en'] !== $new_expo_about_en) {
$update_lang_append .= "expo_about_en = '" . $new_expo_about_en . "', ";
}
if (($ula['expo_url_en'] !== $new_expo_url_en) && ($new_expo_url_en !== "")) {
$update_lang_append .= "expo_url_en = '" . $new_expo_url_en . "', ";
}
if ($update_lang_append == "") {
header("Location: exhibition-edit.php?expo_id=" . $expo_id . "&i=no-change");
exit();
}
else {
$update_query_append = substr($update_query_append, 0, -2);
$update_query = mysql_query("UPDATE exhibitions SET " . $update_query_append . " WHERE expo_id = $expo_id") or die(mysql_error());
header("Location: index.php?i=success");
exit();
}
}
答案 0 :(得分:3)
是的,可能:
${"about_".$lang} = "hello, world!"
虽然variable variables通常被认为是不好的做法(我的意思是,看起来)。您应该为此使用数组。即:
$about = Array();
$about[$lang] = "hello, world";
OP编辑后
像这样创建你的数组:
$new_expo = Array();
$new_expo['url'][$lang] = $_POST['expo_url_lang'];
$new_expo['venue'][$lang] = $_POST['expo_venue_lang'];
以类似的方式致电您的钥匙,I.E:
$update_lang_append .= "expo_venue_en = '" . $new_expo['venue']['en'] . "', ";