In class on Friday (Week 11)

Project 03 - the final project - HireMe/index.php


Goal Assignment Grading Rubric

New material to learn before class on Friday

Video/Transcript: Introduction to PHP (Static vs Dynamic)

(watch this video in a new tab or window)

    PHP is a popular general-purpose scripting language that is especially suited to web development.  It is very powerful and is widely used.  Up to now, all of our content has been static, which means you typed something, saved it, and viewed it on the web.  With PHP, you will begin to use dynamically created content.  You can type something like:     
    
<?php     
echo date('Ymd');     
?>     
    
on your web page and when you view it you'll see today's date in year month day format.     

Video/Transcript: PHP is not very forgiving

(watch this video in a new tab or window)

People new to PHP find that PHP is not very forgiving, especially compared to HTML. Let's look at a page. Here I have a page with some very basic HTML in PHP. In my web folder I don't have an error log. I'll create an error by adding an extraneous single quote. When I refresh my page, it says 'This page isn't working'. As soon as I create an error and refresh my site (I'm using an FTP client called Cyberduck during this video, if you're connected via WebDAV the error_log file will show up in a little bit). The error_log will show a line number near where the problem is. The problem may be before the number. Make little changes in your code and reload your pages often to make troubleshooting go more quickly than making lots of changes at a time. I'll delete the extra single quote, save the page, and reload it in the browser to verify that it's functioning properly again.

Video/Transcript: Introduction to PHP

(watch this video in a new tab or window)

    PHP needs to be installed on your website.  At Miami, it's already been installed.  To create a new PHP document, you can just copy an existing document and change the extension to php.  You can begin and end PHP multiple times in the document if you want to, or you can wrap the whole thing in PHP.  To begin PHP type '<?php' and to end PHP type '?>'.  When you're in PHP, you can still create HTML by using 'echo'.  The following will say 'Hello World':     
    
<?php     
echo 'Hello World';     
?>     
    
Now let's put that in bold:     
    
<?php     
echo '<b>Hello World</b>';     
?>     
    
Now let's make it a header:     
    
<?php     
echo '<h1>Hello World</h1>';     
?>     
    
Now let's make a list:     
    
<?php     
echo '<ul><li>John<li>Bob<li>Bill<li>Rick<li>Steve</ul>';     
?>     
    
Here's the same list a few different ways, all with different code formatting but with the same HTML formatting:     
    
<?php     
  echo '<ul>     
        <li>John     
        <li>Bob     
        <li>Bill     
        <li>Rick     
        <li>Steve     
        </ul>     
       ';     
?>     
    
    
or     
    
    
<?php     
  echo '<ul>     
          <li>John</li>     
          <li>Bob</li>     
          <li>Bill</li>     
          <li>Rick</li>     
          <li>Steve</li>     
        </ul>     
       ';     
?>     
    
    
or     
    
    
<?php     
  echo '<ul>';     
  echo '  <li>John</li>';     
  echo '  <li>Bob</li>';     
  echo '  <li>Bill</li>';     
  echo '  <li>Rick</li>';     
  echo '  <li>Steve</li>';     
  echo '</ul>';     
?>     
    
    
or     
    
    
<?php     
  echo '<ul>';     
  echo '  <li>John</li>';     
  echo '  <li>Bob</li>';     
  echo '  <li>Bill</li>';     
  echo '  <li>Rick</li>';     
  echo '  <li>Steve</li>';     
  echo '</ul>';     
?>     
    
Here's a loop to show you some of the power of PHP.     
    
$ThisManyRounds=3;     
for($round=1;$round<=$ThisManyRounds;$round++){     
  if($round==1){$Topic='American Literature';}     
  if($round==2){$Topic='Mathematics';}     
  if($round==3){$Topic='World History';}     
  echo $round.') '.$Topic.'<br>';     
}     
    
See if you can change the loop to show the dates of the last 180 days in the following format:     
Day of the Week, Month-Day-Year     
Good luck.     
    
Here's code to get you started:     
$ShowThisManyDays=30;     
for($day=-10;$day<=$ShowThisManyDays;$day++){     
  $TheDay=date('F m-d-Y, l',     
               mktime(0,     
                      0,     
                      0,     
                      date('m'),     
                      (date('d')+$day),     
                      date('Y')     
                     )     
              );     
  echo $TheDay.'<br>';     
}     

Assignment due by Thursday 04-08-2021 at 11:59pm

Goal Assignment Grading Rubric
Video/Transcript: WebPage11

(watch this video in a new tab or window)

With WebPage11 we're going to practice some PHP. Ultimately we want to show 'Today is' with the date in month dash day dash year format. 'The time is' in hour colon minute am or pm format. 'The second hand is' odd or even based on the current second. Reloading the class page shows the data updating like your page should update.

Let's look at my code. At the top of the page I have standard HTML. Since I have a favicon, I include it in my HTML. You should have your name in place of mine. Web Page 11 is still a good title to have. The meat of the page is from lines 12 through 27. On line 12 I begin PHP and on line 27 I end PHP. On line 13 I set the timezone. Line 14 is an H1 with a page header hard-coded (i.e. static, it doesn't change automatically). On line 16 I have a break followed by 'Today is'. I can add text, save it, reload my page to see how it changes. Break out of HTML and into PHP with a period. I have a PHP date function in double-quotes, it could also be in single quotes. I have m dash d dash capitol Y to show in the format required for this assignment. When I reload my page, nothing changed, because I forgot to save my changes. I saved my changes now and reload the page to get an updated page.

To find PHP date format information I did a Google search for 'PHP date'. I like to use the PHP manual and W3Schools. I'll use the date manual today. Let's try a capitol D. It shows 'Mon'. A lowercase 'l' shows 'Monday'. I'll set it to m dash d dash capitol Y for this assignment.

The 2nd requirement for the assignment is to say 'The time is now' with the current time. You can't just type the date, the time, and the seconds because they need to be current every time the page is reloaded. That's why we are using PHP. Single quotes and double quotes can often be used interchangeably in PHP but you need to use them in pairs and it's helpful to be consistent.

On line 20 I created a variable called Seconds. The dollar sign in front of the word makes it a variable. A variable is something that can change. This variable will be set equal to the current seconds (in the minute) using PHP's date function with a lowercase s. Line 21 shows 'The second hand is' and displays the current seconds in parenthesis. Reloading the page will show it changing. Line 26 helps determine if the variable is even or odd; using code that I found and modified by doing a Google search for 'PHP odd even'. I used StackOverflow for my example. I modified what I found and added an 'else' to show 'odd' if the variable is odd. Re-running the program (i.e. reloading the page) will show the results.