[PHP]Change Language and while statements.
I have this here. I want to make it to where when they select a language, it changes the language across the site. It works on the index page, but when I click on any other links, it changes back to default.
How would I go about doing this?
PHP Code:
$langset = anti_injection($_POST['langset']);
if($langset == "espanol"){
$language = "espanol";
include("language/$language.php");
}
else{
$language = "english";
include("language/$language.php");
}
<form action="#" method="post">
Language:
<?php
if($langset == "espanol")
{
?>
<select name="langset" class="tbl_colbor" onChange="submit()">
<option value="espanol">Espanol</option>
<option value="english">English</option>
</select>
<?php
}
else
{
?>
<select name="langset" class="tbl_colbor" onChange="submit()">
<option value="english">English</option>
<option value="espanol">Espanol</option>
</select>
<?php
}
?>
</form>
Also, is there a way to use the same query on multiple while statements?
I have multiple while statements using the same query $items, but only the first one works, the rest are still blank with just the default option I set outside the while statement.
PHP Code:
<?php
$CID = anti_injection($_GET['CID']);
$info = mssql_query("SELECT CID, Name, Class FROM Character WHERE CID = '$CID'");
$getinfo = mssql_fetch_array($info);
$items = mssql_query("SELECT Name, ItemID FROM Items WHERE Class='".$getinfo['Class']."' AND SubItem=0");
?>
<tr>
<td>Slot 1 :</td>
<td>
<select name="slot1" id="sub" onchange="showsub()">
<option value="">Select Item</option>
<? while($row = mssql_fetch_array($items))
{
echo '<option value="'.$row['ItemID'].'">'.$row['Name'].'</option>';
}
?>
</select>
</td>
<td>
<span id="showsub">
</span>
</td>
</tr>
Re: [PHP]Change Language and while statements.
You can use cookies for the language. PHP: setcookie - Manual
Store it in the cookie one time, then after the user reloads his page again the cookie will be accessible on all pages of that domain.
I don't really understand the second question. Maybe show some more code? Because the script you show me looks fine.
Re: [PHP]Change Language and while statements.
Quote:
Originally Posted by
SuperWaffle
You can use cookies for the language.
PHP: setcookie - Manual
Store it in the cookie one time, then after the user reloads his page again the cookie will be accessible on all pages of that domain.
I don't really understand the second question. Maybe show some more code? Because the script you show me looks fine.
PHP Code:
<?php
$CID = anti_injection($_GET['CID']);
$info = mssql_query("SELECT CID, Name, Class FROM Character WHERE CID = '$CID'");
$getinfo = mssql_fetch_array($info);
$items = mssql_query("SELECT Name, ItemID FROM Items WHERE Class='".$getinfo['Class']."' AND SubItem=0");
echo '<span style="color:#0C0;">'.$getinfo['Name'].'s Items :</span>';
?>
<br />
<br />
<? echo '<form action="index.php?do=characters&sub=items&CID='.$CID.'" method="post">'; ?>
<table>
<tr>
<td>Slot 1 :</td>
<td>
<select name="slot1" id="sub" onchange="showsub()">
<option value="">Select Item</option>
<? while($row = mssql_fetch_array($items))
{
echo '<option value="'.$row['ItemID'].'">'.$row['Name'].'</option>';
}
?>
</select>
</td>
<td>
<span id="showsub">
</span>
</td>
</tr>
<tr>
<td>Slot 2 :</td>
<td>
<select name="slot2" id="sub" onchange="showsub()">
<option value="">Select Item</option>
<? while($row = mssql_fetch_array($items))
{
echo '<option value="'.$row['ItemID'].'">'.$row['Name'].'</option>';
}
?>
</select>
</td>
<td>
<span id="showsub">
</span>
</td>
</tr>
<tr>
<td>Slot 3 :</td>
<td>
<select name="slot3" id="sub" onchange="showsub()">
<option value="">Select Item</option>
<? while($row = mssql_fetch_array($items))
{
echo '<option value="'.$row['ItemID'].'">'.$row['Name'].'</option>';
}
?>
</select>
</td>
<td>
<span id="showsub">
</span>
</td>
</tr>
<tr>
<td>Slot 4 :</td>
<td>
<select name="slot4" id="sub" onchange="showsub()">
<option value="">Select Item</option>
<? while($row = mssql_fetch_array($items))
{
echo '<option value="'.$row['ItemID'].'">'.$row['Name'].'</option>';
}
?>
</select>
</td>
<td>
<span id="showsub">
</span>
</td>
</tr>
<tr>
<td>Slot 5 :</td>
<td>
<select name="slot5" id="sub" onchange="showsub()">
<option value="">Select Item</option>
<? while($row = mssql_fetch_array($items))
{
echo '<option value="'.$row['ItemID'].'">'.$row['Name'].'</option>';
}
?>
</select>
</td>
<td>
<span id="showsub">
</span>
</td>
</tr>
<tr><td align="center" colspan="4"><input type="submit" name="changeitems" value="Change Items" /></td></tr>
</table>
</form>
I'm using the same query in all of the while statements, but only the first selection box has any other option than Select Item (This is for our GunZ 2 website)
It only seemed to work when I changed each array of $item to something unique like $items, $items2, $items3, etc.
Re: [PHP]Change Language and while statements.
You probably have to change $row to something else in each while loop.
Easier (and faster) alternative:
Generate the option values once, store them in a variable and output them in each select form item.
PHP Code:
<?php
$CID = anti_injection($_GET['CID']);
$info = mssql_query("SELECT CID, Name, Class FROM Character WHERE CID = '$CID'");
$getinfo = mssql_fetch_array($info);
$items = mssql_query("SELECT Name, ItemID FROM Items WHERE Class='".$getinfo['Class']."' AND SubItem=0");
// Generate the option values.
$values = '';
while($row = mssql_fetch_array($items))
{
$values .= '<option value="'.$row['ItemID'].'">'.$row['Name'].'</option>';
}
echo '<span style="color:#0C0;">'.$getinfo['Name'].'s Items :</span>';
?>
<br />
<br />
<? echo '<form action="index.php?do=characters&sub=items&CID='.$CID.'" method="post">'; ?>
<table>
<tr>
<td>Slot 1 :</td>
<td>
<select name="slot1" id="sub" onchange="showsub()">
<option value="">Select Item</option>
<?php echo $values; ?>
</select>
</td>
<td>
<span id="showsub">
</span>
</td>
</tr>
<tr>
<td>Slot 2 :</td>
<td>
<select name="slot2" id="sub" onchange="showsub()">
<option value="">Select Item</option>
<?php echo $values; ?>
</select>
</td>
<td>
<span id="showsub">
</span>
</td>
</tr>
<tr>
<td>Slot 3 :</td>
<td>
<select name="slot3" id="sub" onchange="showsub()">
<option value="">Select Item</option>
<?php echo $values; ?>
</select>
</td>
<td>
<span id="showsub">
</span>
</td>
</tr>
<tr>
<td>Slot 4 :</td>
<td>
<select name="slot4" id="sub" onchange="showsub()">
<option value="">Select Item</option>
<?php echo $values; ?>
</select>
</td>
<td>
<span id="showsub">
</span>
</td>
</tr>
<tr>
<td>Slot 5 :</td>
<td>
<select name="slot5" id="sub" onchange="showsub()">
<option value="">Select Item</option>
<?php echo $values; ?>
</select>
</td>
<td>
<span id="showsub">
</span>
</td>
</tr>
<tr><td align="center" colspan="4"><input type="submit" name="changeitems" value="Change Items" /></td></tr>
</table>
</form>
In case short open tags is turned on, you can use <?=$values?> to make it shorter.
Re: [PHP]Change Language and while statements.
Quote:
Originally Posted by
SuperWaffle
Store it in the cookie one time, then after the user reloads his page again the cookie will be accessible on all pages of that domain.
Note that by Dutch law you may not store cross-domain cookies unless you have asked for the user's permission.
Quote:
Originally Posted by
SuperWaffle
Generate the option values once, store them in a variable and output them in each select form item.
This, or push them into an array and then foreach through that array everytime you need the values. As far as I know PHP isn't able to reset a MSSQL result set.
Quote:
Originally Posted by
SuperWaffle
In case short open tags is turned on, you can use <?=$values?> to make it shorter.
Shorttags are deprecated and vulnerable so I wouldn't recommend using them.
To get back at your language selection, why don't you simply use a session or add a parameter to the url? For example index.php?lang=en or index.php?lang=es.
PHP Code:
<?php
$g_rgLanguages = array (
"es" => "Espanol",
"en" => "English"
);
// No need to anti_inject since XSS / SQL injection won't do shit to a string comparison
// Also added file_exists for obvious reasons
if (isset($_GET['lang']) && array_key_exists($_GET['lang'], $g_rgLanguages) && file_exists("language/" . $g_rgLanguages[strtolower($_GET['lang'])] . ".php")) {
include("language/" . $g_rgLanguages[strtolower($_GET['lang'])] . ".php");
}
else {
// Default language
include("language/english.php");
}
foreach ($g_rgLanguages as $strKey => $strValue) {
// Skip current language
if (isset($_GET['lamg']) && $_GET['lang'] == $strKey) {
continue;
}
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?lang=" . $strKey . "\">" . $strValue . "</a> ";
}
?>
Re: [PHP]Change Language and while statements.
Quote:
Originally Posted by
Wizkidje
Note that by Dutch law you may not store cross-domain cookies unless you have asked for the user's permission.
This, or push them into an array and then foreach through that array everytime you need the values. As far as I know PHP isn't able to reset a MSSQL result set.
Shorttags are deprecated and vulnerable so I wouldn't recommend using them.
To get back at your language selection, why don't you simply use a session or add a parameter to the url? For example index.php?lang=en or index.php?lang=es.
PHP Code:
<?php
$g_rgLanguages = array (
"es" => "Espanol",
"en" => "English"
);
// No need to anti_inject since XSS / SQL injection won't do shit to a string comparison
// Also added file_exists for obvious reasons
if (isset($_GET['lang']) && array_key_exists($_GET['lang'], $g_rgLanguages) && file_exists("language/" . $g_rgLanguages[strtolower($_GET['lang'])] . ".php")) {
include("language/" . $g_rgLanguages[strtolower($_GET['lang'])] . ".php");
}
else {
// Default language
include("language/english.php");
}
foreach ($g_rgLanguages as $strKey => $strValue) {
// Skip current language
if (isset($_GET['lamg']) && $_GET['lang'] == $strKey) {
continue;
}
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?lang=" . $strKey . "\">" . $strValue . "</a> ";
}
?>
It seems like it would be easier with a session so we wouldn't have to update every link? Though, that could be easily done by adding it to the page load function if set language is not default.
How would you change the session data? Or at least destroy the old language session and set a new one.
I got the item script working except for one problem, though I'm going to test if my idea is going to work lol.
Re: [PHP]Change Language and while statements.
Quote:
Originally Posted by
wesman2232
It seems like it would be easier with a session so we wouldn't have to update every link? Though, that could be easily done by adding it to the page load function if set language is not default.
How would you change the session data? Or at least destroy the old language session and set a new one.
I got the item script working except for one problem, though I'm going to test if my idea is going to work lol.
Pretty much the same except you read your data from $_SESSION rather than $_GET.