0

Well, I am confused now. I read this answer and tried to apply it in my case.

So I made a local PHP file:

<option value="afk" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'afk') {echo 'selected';} ?>>Afrikaans (Afrikaans)</option>
<option value="twi" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'twi') {echo 'selected';} ?>>Akan (Twi)</option>
<option value="csy" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'csy') {echo 'selected';} ?>>Çesky (Czech)</option>
<option value="dan" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'dan') {echo 'selected';} ?>>Dansk (Danish)</option>
<option value="deu" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'deu') {echo 'selected';} ?>>Deutsch (German)</option>
<option value="eti" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'eti') {echo 'selected';} ?>>Eesti (Estonian)</option>
<option value="eng" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'eng') {echo 'selected';} ?>>English (English)</option>
<option value="esp" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'esp') {echo 'selected';} ?>>Español (Spanish)</option>
<option value="fra" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'fra') {echo 'selected';} ?>>Français (French)</option>
<option value="gun" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'gun') {echo 'selected';} ?>>Gungbe (Gun)</option>
<option value="hrv" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'hrv') {echo 'selected';} ?>>Hrvatski (Croatian)</option>
<option value="ind" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'ind') {echo 'selected';} ?>>Indonesia (Indonesian)</option>
<option value="zul" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'zul') {echo 'selected';} ?>>IsiZulu (Zulu)</option>
<option value="ita" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'ita') {echo 'selected';} ?>>Italiano (Italian)</option>
<option value="lin" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'lin') {echo 'selected';} ?>>Lingala (Lingala)</option>
<option value="swk" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'swk') {echo 'selected';} ?>>Kiswahili (Swahili)</option>
<option value="htc" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'htc') {echo 'selected';} ?>>Kreyòl Ayisyen (Haitian Creole)</option>
<option value="mgy" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'mgy') {echo 'selected';} ?>>Malagasy (Malagasy)</option>
<option value="nld" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'nld') {echo 'selected';} ?>>Nederlands (Dutch)</option>
<option value="plk" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'plk') {echo 'selected';} ?>>Polski (Polish)</option>
<option value="ptb" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'ptb') {echo 'selected';} ?>>Português Brasil (Portuguese Brazil)</option>
<option value="rom" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'rom') {echo 'selected';} ?>>Română (Romanian)</option>
<option value="sqi" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'sqi') {echo 'selected';} ?>>Shqip (Albanian)</option>
<option value="slv" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'slv') {echo 'selected';} ?>>Slovenski (Slovenian)</option>
<option value="fin" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'fin') {echo 'selected';} ?>>Suomi (Finnish)</option>
<option value="sve" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'sve') {echo 'selected';} ?>>Svenska (Swedish)</option>
<option value="fpo" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'fpo') {echo 'selected';} ?>>Tagalog (Tagalog)</option>
<option value="trk" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'trk') {echo 'selected';} ?>>Türkçe (Turkish)</option>
<option value="vit" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'vit') {echo 'selected';} ?>>Việt Nam (Vietnamese)</option>
<option value="eng" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'eng') {echo 'selected';} ?>>العربية (Arabic)</option>
<option value="bgr" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'bgr') {echo 'selected';} ?>>Български (Bulgarian)</option>
<option value="chs" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'chs') {echo 'selected';} ?>>汉语(简化字) (Chinese Simplified)</option>
<option value="ell" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'ell') {echo 'selected';} ?>>Ελληνική (Greek)</option>
<option value="hin" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'hin') {echo 'selected';} ?>>हिंदी (Hindi)</option>
<option value="rus" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'rus') {echo 'selected';} ?>>Русский (Russian)</option>
<option value="tai" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'tai') {echo 'selected';} ?>>தமிழ் (Tamil)</option>
<option value="ukr" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'ukr') {echo 'selected';} ?>>Українська (Ukrainian)</option>

I then modified my test PHP document. Snippet:

<div style="position: absolute; top: 10px; right: 10px" >
    <p>Select Language:</p>
    <form method="post">
        <select name="SoundRotaLang" onchange="setlang(this.value)">
        <?php include('language_combo.php'); ?>
    </select></form>
</div>

I uploaded my pages and this a test. That page should end up looking like this really. But it doesn't.

In addition, Expression Web 4 is complaining:

Error

When I actually view my webpage in a browser I can see this at the bottom:

Parse error: syntax error, unexpected end of file in /customers/e/9/c/publictalksoftware.co.uk/httpd.www/msa/language_combo.php on line 38

It stops at the point my PHP code is included.

All I was trying to do was use PHP to build the combo and init the right combo entry via a URL parameter. This way I could replicate this little bit of code in my 37 PHP files and have just one place for the list that gets included.

Confused.

Community
  • 1
  • 1
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 1
    Syntax.... you're nesting 2 `if` statements, but missing a closing `}`; or by using one `if` with two conditions.... and it really does look ugly; having an array of languages, and using a loop could do this in much more easily-readable code – Mark Baker Feb 19 '17 at 10:13
  • @MarkBaker I guess the answer provided had corrected that mistake of mine. It is working now. Could you please show me what you mean about using a array and loop please? – Andrew Truckle Feb 19 '17 at 10:22

2 Answers2

2

Expanding on what I commented.

Build an array of your languages like:

$languages = [
    'afk' => 'Afrikaans (Afrikaans)',
    'twi' => 'Akan (Twi)',
    ....
    'ukr'=> 'Українська (Ukrainian)',
];

and then replace your wall of code with a simple loop:

foreach($languages as $code => $name) {
    $selected = (isset($_GET['lang']) && $_GET['lang'] == $code) ? 'selected' : '';
    echo '<option value="', $code, '" ', $selected, '>', $name, '</option>';
}
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
1

replace language_combo.php with this

<option value="afk" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'afk') {echo 'selected';}} ?>>Afrikaans (Afrikaans)</option>
<option value="twi" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'twi') {echo 'selected';}} ?>>Akan (Twi)</option>
<option value="csy" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'csy') {echo 'selected';}} ?>>Çesky (Czech)</option>
<option value="dan" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'dan') {echo 'selected';}} ?>>Dansk (Danish)</option>
<option value="deu" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'deu') {echo 'selected';}} ?>>Deutsch (German)</option>
<option value="eti" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'eti') {echo 'selected';}} ?>>Eesti (Estonian)</option>
<option value="eng" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'eng') {echo 'selected';}} ?>>English (English)</option>
<option value="esp" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'esp') {echo 'selected';}} ?>>Español (Spanish)</option>
<option value="fra" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'fra') {echo 'selected';}} ?>>Français (French)</option>
<option value="gun" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'gun') {echo 'selected';}} ?>>Gungbe (Gun)</option>
<option value="hrv" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'hrv') {echo 'selected';}} ?>>Hrvatski (Croatian)</option>
<option value="ind" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'ind') {echo 'selected';}} ?>>Indonesia (Indonesian)</option>
<option value="zul" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'zul') {echo 'selected';}} ?>>IsiZulu (Zulu)</option>
<option value="ita" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'ita') {echo 'selected';}} ?>>Italiano (Italian)</option>
<option value="lin" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'lin') {echo 'selected';}} ?>>Lingala (Lingala)</option>
<option value="swk" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'swk') {echo 'selected';}} ?>>Kiswahili (Swahili)</option>
<option value="htc" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'htc') {echo 'selected';}} ?>>Kreyòl Ayisyen (Haitian Creole)</option>
<option value="mgy" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'mgy') {echo 'selected';}} ?>>Malagasy (Malagasy)</option>
<option value="nld" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'nld') {echo 'selected';}} ?>>Nederlands (Dutch)</option>
<option value="plk" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'plk') {echo 'selected';}} ?>>Polski (Polish)</option>
<option value="ptb" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'ptb') {echo 'selected';}} ?>>Português Brasil (Portuguese Brazil)</option>
<option value="rom" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'rom') {echo 'selected';}} ?>>Română (Romanian)</option>
<option value="sqi" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'sqi') {echo 'selected';}} ?>>Shqip (Albanian)</option>
<option value="slv" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'slv') {echo 'selected';}} ?>>Slovenski (Slovenian)</option>
<option value="fin" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'fin') {echo 'selected';}} ?>>Suomi (Finnish)</option>
<option value="sve" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'sve') {echo 'selected';}} ?>>Svenska (Swedish)</option>
<option value="fpo" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'fpo') {echo 'selected';}} ?>>Tagalog (Tagalog)</option>
<option value="trk" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'trk') {echo 'selected';}} ?>>Türkçe (Turkish)</option>
<option value="vit" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'vit') {echo 'selected';}} ?>>Việt Nam (Vietnamese)</option>
<option value="eng" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'eng') {echo 'selected';}} ?>>العربية (Arabic)</option>
<option value="bgr" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'bgr') {echo 'selected';}} ?>>Български (Bulgarian)</option>
<option value="chs" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'chs') {echo 'selected';}} ?>>汉语(简化字) (Chinese Simplified)</option>
<option value="ell" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'ell') {echo 'selected';}} ?>>Ελληνική (Greek)</option>
<option value="hin" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'hin') {echo 'selected';}} ?>>हिंदी (Hindi)</option>
<option value="rus" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'rus') {echo 'selected';}} ?>>Русский (Russian)</option>
<option value="tai" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'tai') {echo 'selected';}} ?>>தமிழ் (Tamil)</option>
<option value="ukr" <?php if (isset($_GET['lang'])) {if($_GET['lang'] == 'ukr') {echo 'selected';}} ?>>Українська (Ukrainian)</option>
SOFe
  • 7,867
  • 4
  • 33
  • 61
user259947
  • 59
  • 6
  • Thanks. That seems to work. I still get errors in Expression Web but it works. Thanks. Perhaps flesh out your answer with a example line being wrong and line being right to show my mistake? Thank you. – Andrew Truckle Feb 19 '17 at 10:21
  • 1
    You forgot to close first if statement - if (isset($_GET['lang'])) – user259947 Feb 19 '17 at 10:31