Codecademy Forums

Loops in HTML > project

Hi,
I have a problem with the foreach loop, the values of the variables don’t echo in the webpage.
What print normally :
the values of the variables in the lessons,
and the other loops in the lessons and in the projects.
But the foreach loops in the projects (“Repetitive cafe” and “FizzBuzz”), don’t print at all.
Thanks if someone has an idea :slight_smile:

My code :

<?php
$drinks = [
  "cafe"=>1.5,
  "chocolate"=>1.5,
  "noisette"=>1.5,
  "latte"=>1.5,
  "coca"=>1.5,
  "sirop citron"=>1,
];

$pastries = [
  "tarte pommes",
  "eclair chocolat",
  "muffin chocolat",
  "cookie",
  "croissant maxi",
  "pain au chocolat maxi",
  "donut chocolat"
];
?>

<h1>Welcome to the Repetitive Cafe</h1>

<h3>Drinks!</h3>
<ul>
<?php foreach($drinks as $drink=>$price):
	<li><?=$drink?> at <?=$price?>€</li><br>
<?php endforeach?>
</ul>

Not sure about your syntax on the loop.

<ul>
<?php 
  foreach($drinks as $drink=>$price):
    echo "<li>$drink at $price €</li>";
  endforeach
 ?>
</ul>

or optionally,

<ul>
<?php 
  foreach($drinks as $drink=>$price):
    echo "<li>{$drink} at {$price}€</li>";
  endforeach
 ?>
</ul>

Above we use variable interpolation which can only be done within double quotes.


To be able to write it as you have,

<ul>
<?php foreach($drinks as $drink=>$price):
	<li><?=$drink?> at <?=$price?>€</li><br>
<?php endforeach?>
</ul>

the signature line should have a close tag.

<ul>
<?php foreach($drinks as $drink=>$price): ?>
  <li><?=$drink?> at <?=$price?>€</li>
<?php endforeach ?>
</ul>
1 Like

I cannot go to the page of the project, the dam…ed program il too authoritarian ! :wink:
I’ll compare your propositions with my code as soon as possible, perhaps in another project.
Thanks, I’ll tell if it prints.

2 Likes

I was able to open the project and test all three loops with the following results…

<h3>Drinks!</h3>

<ul>
<?php 
  foreach($drinks as $drink=>$price):
    echo "<li>$drink at $price €</li>";
  endforeach
 ?>
</ul> 
<ul>
<?php 
  foreach($drinks as $drink=>$price):
    echo "<li>{$drink} at {$price}€</li>";
  endforeach
 ?>
</ul>
    
<ul>
<?php foreach($drinks as $drink=>$price): ?>
  <li><?=$drink?> at <?=$price?>€</li>
<?php endforeach?>
</ul>
1 Like

I don’t know how to enter in a course already finished, thanks for the link.

In any case, the code, like with the closed tag, don’t print anything.

type <?php foreach($drinks as $drink=>$price):?>
	<li><?=$drink?> at <?=$price?>€</li><br>
<?php endforeach?>
</ul> paste code here

Nothing printed, the desperate blank page of PHP :’(

1 Like

Does your code look exactly like this?

<ul>
<?php foreach($drinks as $drink=>$price): ?>
  <li><?=$drink?> at <?=$price?>€</li>
<?php endforeach?>
</ul>

Note there is no <br> needed unless you want double line spacing.

1 Like

Yes my code looks exactly like this. Often I have to copy-paste the hint’s code because the website prints an error. Everytimes I compare the corection with my code, but I don’t see any difference.
In the “FizzBuzz” project the foreach didn’t print, I have “copy-paste” the hints and it prints. Again I don’t see any difference with my code.
I marked this topic as “resolved”. I’ll test on my local server to see if my code runs, before copy-paste the hints and compare.
Thanks a lot for your attention :slight_smile:

Edit : Concerning the acces to a lesson finished, I have to look into the code HTML and CSS , change the display to “block” (it was to “none”) and the opacity to “1” (it was to “0”). All the lessons are closed but the 1st, and click on the buttons don’t open the menus, nor close the first.
Perhaps it’s due to my browser. Do I write in the “Bugs” forum? I think I disabled all the extensions that could block anything.

1 Like