12 days of christmas

12 days of christmas
0

#1

Need help with writing a code 12 days of Christmas in PHP i’m so lost and frustrated please help! :cold_sweat:

<?php

header("Content-Type: text/plain; charset=UTF-8");

$days = array(
    'first', 'second', 'third', 'fourth', 'fifth', 'sixth', 
	'seventh', 'eighth', 'ninth','tenth', 'eleventh', 'twelfth',
);

echo str_repeat("On the day of Christmas, 
my true love gave to me.",12); 

$presents = array(
    "A partridge in a pear tree",
    "Two turtle doves",
    "Three french hens",
    "Four calling birds",
    "Five golden rings",
    "Six geese a-laying",
    "Seven swans a-swimming",
    "Eight maids a-milking",
    "Nine ladies dancing",
    "Ten lords a-leaping",
    "Eleven pipers piping",
    "Twelve drummers drumming"
);

#2

Firstly, welcome to the forums.

While we are primarily here to help people with their Free Code Camp progress, we are open to people on other paths, too. Some of what you are asking is pretty trivial in the Free Code Camp context, so you might find that if you’re not getting the instruction and material you need in your current studies, the FCC curriculum will really help you get started. At a modest guess I’d say investing a 4-5 hours working through the curriculum here will really pay off. You can find the curriculum at https://freecodecamp.org.

With your current questions, we don’t have enough context to know what you already know or don’t know, so it is impossible to guide you without just telling you the answer (which we won’t do).

It is pretty typical on here for people to share a codepen / jsfiddle example of what they have tried so that anyone helping has more of an idea of what help is actually helpful.

Please provide some example of what you’ve tried and I’m sure you’ll get more help.

Happy coding :slight_smile:


#5
<?php
$days = array(
    'first', 'second', 'third', 'fourth', 'fifth', 'sixth', 
	'seventh', 'eighth', 'ninth','tenth', 'eleventh', 'twelfth',
);

$presents = array(
    "A partridge in a pear tree",
    "Two turtle doves",
    "Three french hens",
    "Four calling birds",
    "Five golden rings",
    "Six geese a-laying",
    "Seven swans a-swimming",
    "Eight maids a-milking",
    "Nine ladies dancing",
    "Ten lords a-leaping",
    "Eleven pipers piping",
    "Twelve drummers drumming"
);

$phrases = "";
foreach ($days as $key => $day) {
  $phrases = "  $presents[$key]\n$phrases";
  echo("On the $day day of Christmas, my true love gave to me...\n$phrases\n"); 
}
?>

OR using a single associate array:

<?php
$daysAndPresents = array(
  "first"=>"A partridge in a pear tree",
  "second"=>"Two turtle doves",
  "third"=>"Three french hens",
  "fourth"=>"Four calling birds",
  "fifth"=>"Five golden rings",
  "sixth"=>"Six geese a-laying",
  "seventh"=>"Seven swans a-swimming",
  "eighth"=>"Eight maids a-milking",
  "ninth"=>"Nine ladies dancing",
  "tenth"=>"Ten lords a-leaping",
  "eleventh"=>"Eleven pipers piping",
  "twelfth"=>"Twelve drummers drumming"
);

$phrases = "";
foreach ($daysAndPresents as $day => $present) {
  $phrases = "  $present\n$phrases";
  echo("On the $day day of Christmas, my true love gave to me...\n$phrases\n"); 
}
?>

Both versions produce:

On the first day of Christmas, my true love gave to me...
  A partridge in a pear tree

On the second day of Christmas, my true love gave to me...
  Two turtle doves
  A partridge in a pear tree

On the third day of Christmas, my true love gave to me...
  Three french hens
  Two turtle doves
  A partridge in a pear tree

On the fourth day of Christmas, my true love gave to me...
  Four calling birds
  Three french hens
  Two turtle doves
  A partridge in a pear tree

On the fifth day of Christmas, my true love gave to me...
  Five golden rings
  Four calling birds
  Three french hens
  Two turtle doves
  A partridge in a pear tree

On the sixth day of Christmas, my true love gave to me...
  Six geese a-laying
  Five golden rings
  Four calling birds
  Three french hens
  Two turtle doves
  A partridge in a pear tree

On the seventh day of Christmas, my true love gave to me...
  Seven swans a-swimming
  Six geese a-laying
  Five golden rings
  Four calling birds
  Three french hens
  Two turtle doves
  A partridge in a pear tree

On the eighth day of Christmas, my true love gave to me...
  Eight maids a-milking
  Seven swans a-swimming
  Six geese a-laying
  Five golden rings
  Four calling birds
  Three french hens
  Two turtle doves
  A partridge in a pear tree

On the ninth day of Christmas, my true love gave to me...
  Nine ladies dancing
  Eight maids a-milking
  Seven swans a-swimming
  Six geese a-laying
  Five golden rings
  Four calling birds
  Three french hens
  Two turtle doves
  A partridge in a pear tree

On the tenth day of Christmas, my true love gave to me...
  Ten lords a-leaping
  Nine ladies dancing
  Eight maids a-milking
  Seven swans a-swimming
  Six geese a-laying
  Five golden rings
  Four calling birds
  Three french hens
  Two turtle doves
  A partridge in a pear tree

On the eleventh day of Christmas, my true love gave to me...
  Eleven pipers piping
  Ten lords a-leaping
  Nine ladies dancing
  Eight maids a-milking
  Seven swans a-swimming
  Six geese a-laying
  Five golden rings
  Four calling birds
  Three french hens
  Two turtle doves
  A partridge in a pear tree

On the twelfth day of Christmas, my true love gave to me...
  Twelve drummers drumming
  Eleven pipers piping
  Ten lords a-leaping
  Nine ladies dancing
  Eight maids a-milking
  Seven swans a-swimming
  Six geese a-laying
  Five golden rings
  Four calling birds
  Three french hens
  Two turtle doves
  A partridge in a pear tree

#6

Thank You so much for your time!


#7

I took the liberty of marking this as source code for you. (Moderator)