This is the P2PU Archive. If you want the current site, go to www.p2pu.org!

Introduction to PHP

Assignments for Week #4 - A Wide Array of Choices

Matthew Buscemi's picture
Sun, 2011-02-13 04:29
1) http://codepad.org/SfdUAjoo - Modify this code chunk according to the following parameters:
  1. The keys for this array are automatically generated, starting at 0.  Change the code so that the keys are specified.  Change the keys to start at 5 and end at 9 instead of starting at 0 and ending at 4.
  2. Now, change the array so that the value stored in the array is a random number from 1 to 5 multiplied times the key.

2) http://codepad.org/U4uV1QS5 - Using the PHP manual array function reference, modify this code chunk according to the following parameters:

  1. Order the animal names alphabetically.
  2. Order the animal names reverse alphabetically.
  3. Print the number of animal names in the array using a function from the array function reference.
  4. Order the animal names randomly.
  5. Find two different ways to add a new animal name to the array.  (Hint: There are at least three ways to do this.  At least one of these ways uses a function from the array function reference).

3) http://codepad.org/ftHAnlX2 - Do the following:

  1. Modify the array to include a ‘salads’ section and a ‘soups’ section.  Change the other items in the restaurant to whatever you want your restaurant to serve.
  2. Print the number of items in the section after the name of the section in parentheses.
  3. Add a background color to each <tr> in the table.
  4. Make each section of the menu (appetizers, entrees, etc.) have a different background color.
  5. Give each item a price and print it in a third table column.

4) Create an exercise for your classmates that requires the use of array variables.  The exercise can have your classmate analyze your code chunk on Codepad or provide instructions for them to build their own, or both.

Comments

Please will someone who has

Nathan Wrigley's picture
Nathan Wrigley
Wed, 2011-02-16 00:09

Please will someone who has the authority get in the (Drupal I assume) backend and enable full (or at least a wide range of harmless) HTML tags in comments - it's hard to describe code without the tags!!!

Hi there, I'm finding the

Nathan Wrigley's picture
Nathan Wrigley
Wed, 2011-02-16 00:13

Hi there,

I'm finding the output of the code for question 3 rather odd.

It appears to put in extra /tr tags where they ought not to be. For example I'm getting things like this:

excuse odd tags but Drupal keeps stripping out the tags!

[tr]
[td]appetizers (2)[/td]
[td]chicken wings[/td]
[/tr]
[td]fried cheese[/td]
[/tr]

As I don't really understand how you are iterating through the array with this code:

<?php for($g=0; $g<$numGroups; $g++): (it's the ' : ' at the end and the 'endfor' statements that have me confused)

Whatever I do, I can't make the code come out how I think that an HTML table should look.

Thanks.

The use of the ":" is the

intro vert's picture
intro vert
Fri, 2011-02-18 12:46

The use of the ":" is the alternative syntax for the control structures if, while etc. Looks like a cleaner way to structure and avoids all the curly braces. It is explained here: http://www.php.net/manual/en/control-structures.alternative-syntax.php

Like you, I am not sure how the output will produce a 'nice' table. Question for meeting time, I think.

Adding [/tr] after line "php

intro vert's picture
intro vert
Fri, 2011-02-18 13:05

Adding [/tr] after line "php echo $menu[$g]['name']" makes my output look much better.

Thanks for that - my

Nathan Wrigley's picture
Nathan Wrigley
Sat, 2011-02-19 12:51

Thanks for that - my solutions are here if you wanted to look...

https://docs.google.com/document/d/1u2x0DHRIuy9LP0VR9S7DxjLPPxZS8aKGzARD...