1

I'm very new to PHP/Programming, so you can understand my frustration. Don't get me wrong I'm loving it.

My objective is to create an unordered list in PHP and display it in html.

I was able to successfully create the unordered list, and it shows up correctly.

My issue is when I try to display it to an HTML file using the fopen(); & fwrite();.

The error i'm getting is that it only display the last three values in my array when I switch over to my .html file. My code is below, any advice is appreciated.

$colors = array (
      0 => array ("red", "black", "white"),
      1 => array ("yellow", "blue", "green"),
      2 => array ("orange", "grey", "pink"),
      3 => array ("purple", "brown", "clear"),
);

foreach ($colors as $value) {
    echo "<ul>";
    $couleur = "<li>". implode ("</li><li>", $value) ."</li>";
    echo $couleur;
    echo "</ul>";
}

$y = fopen ("list.html", "w");
fwrite ($y, $couleur);
3
  • is there a special reason you need to display this on the .html file and not directly display it from the .php file? you are also overwriting $couleur on each foreach iteration. try using $couleur[] = ... Commented Apr 29, 2016 at 15:32
  • Yes, you're overwriting $couleur every iteration of your loop. Define $couleur aas an empty string before the loop; don't echo inside the loop, but use $couleur .= ... to build your string Commented Apr 29, 2016 at 15:32
  • Change $couleur = to $couleur .= for concatenation. Commented Apr 29, 2016 at 15:38

1 Answer 1

1

Instead of printing (echo), I think you want to store all data in a variable and write that data to file eventually. Otherwise you will see it when executing your PHP script, but it will not be written to file.

In your version, you're overwriting $couleur every iteration (as already pointed out by others), which explains why you only get the last array. You can append strings using the .= operator, which would make your code look like this, for example:

$colors = array (
    0 => array ("red", "black", "white"),
    1 => array ("yellow", "blue", "green"),
    2 => array ("orange", "grey", "pink"),
    3 => array ("purple", "brown", "clear"),
);

foreach ($colors as $value) {
    $couleur .= "<ul>";
    $couleur .= "<li>". implode ("</li><li>", $value) ."</li>";
    $couleur .= "</ul>";
}

$y = fopen ("list.html", "w");
fwrite ($y, $couleur);

Note that this provides you with four unordered lists. I'm not sure if that is what you want; you could also make one long list by moving the <ul> tags outside your loop:

$couleur = "<ul>";
foreach ($colors as $value) {
    $couleur .= "<li>". implode ("</li><li>", $value) ."</li>";
}
$couleur .= "</ul>";
Sign up to request clarification or add additional context in comments.

1 Comment

awesome, thank you guys. Such a simple solution, just what i needed.

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.