0

I have a script that select a list of letter from the array but the problem is it only display the last element on the array.

PHP:

<?php

$letters = array("A", "B", "C");

foreach($letters as $letter)
{
    $data['#LETTER#'] = $letter;
}

$html = file_get_contents('test.html');

echo $html = str_replace(array_keys($data),array_values($data),$html);
?>

HTML:

<html>
<head>
<title>TRY</title>
</head>
<body>
<select>
<option>#LETTER#</option>
</select>
</body>
</html>

the output above the script is only C which is the last element in the array and I can only select the letter C not A B C

DESIRED OUTPUT: A B C

why is this happen? can anyone help? I appreciate with explanation. I'm currently new on php.

1
  • You assing every element to the same key in $data. Nemely the key "#LETTER#". On a second note, your method of outputting the array seems rather complex? Commented Nov 18, 2013 at 9:01

5 Answers 5

2

You should make it like this:

HTML:

<html>
<head>
<title>TRY</title>
</head>
<body>
<select>
#LETTER_SELECT#
</select>
</body>
</html>

PHP:

$letters = array("A", "B", "C");
$replace_select = "";
foreach($letters as $letter)
{
    $replace_select .= "<option>".$letter."</option>";
}

$html = file_get_contents("test.html");
echo $html = str_replace("#LETTER_SELECT#",$replace_select);
Sign up to request clarification or add additional context in comments.

4 Comments

what do you mean? you want 3 select options but only 1 option, that isn't possible.
You can't have 3 options into 1 option tag, for each select element you need another option tag.
Yes, but this will make the select tag only display 1 element
1

try to use like. You have single key for all characters, you need to use string concat of php to make a string.

foreach($letters as $letter)
{
        $data['#LETTER#'] .= "<option value=$letter>".$letter."</option>";
}

<select>
#LETTER#
</select>

5 Comments

it just output ABC in one option it didn't make me selections :(
concat space " " with it and finally use rtrim to remove last space
define $data = array(); before foreach loop
is there no other way to remove option tag o the script?
you can use this script in html between <select></select> tag with echo every line. But there is no way
0

i dont actually get what you're trying to do here, but it seems that your loop is always assign values to 1 element of array only, which is #LETTER#.. why not use

$data[] = $letter

instead and loop it inside the select tag

Comments

0

You are overwriting the array element in $data with each iteration.

You could implement the PHP within the HTML.

<select>

<?php
foreach($letters as $letter)
{
<option> $letter </option>
}
?>

</select>

Comments

0
<?php

    $letters = array("A", "B", "C");

    foreach($letters as $letter)
    {
        $data['#LETTER#'] = $letter;
    }

    foreach ($data as $key => $value)
    {
        echo "Key: $key; Value: $value<br />\n";
    }

?>

Result:
Key: #LETTER#; Value: C

You have only one element in $data array. Try $data['#LETTER'.$letter.'#'] = $letter;

1 Comment

You might want to change your post from just showing the error to only showing a solution. Someone who just copies it will not archive anything here. (as op just did)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.