Difference between revisions of "PHP105 and a half"

From mi-linux
Jump to navigationJump to search
m
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
[[Main Page]] >> [[CP2228|Web Application Development]] >> [[PHP|Workbook]] >> Include
 +
 
A word about include statements
 
A word about include statements
  
Line 13: Line 15:
  
 
Create the following file and save it as "menu.php"
 
Create the following file and save it as "menu.php"
<nowiki>
+
<pre><nowiki>&lt;table border=1&gt;
<table border=1>
+
&lt;tr&gt;
  <tr>
+
&lt;td&gt;&lt;a href="page1.php"&gt;Page 1&lt;/a&gt;&lt;/td&gt;
    <td><a href="page1.php">Page 1</a></td>
+
&lt;td&gt;&lt;a href="page2.php"&gt;Page 2&lt;/a&gt;&lt;/td&gt;
    <td><a href="page2.php">Page 2</a></td>
+
&lt;td&gt;&lt;a href="page3.php"&gt;Page 3&lt;/a&gt;&lt;/td&gt;
    <td><a href="page3.php">Page 3</a></td>
+
&lt;/tr&gt;
  </tr>
+
&lt;/table&gt;</nowiki></pre>
</table></nowiki>
 
 
 
 
Now create the following and save it as "page1.php"
 
Now create the following and save it as "page1.php"
<nowiki>
+
<pre><nowiki>&lt;html&gt;&lt;head&gt;&lt;title&gt;Page1&lt;/title&gt;&lt;/head&gt;
<html><head><title>Page1</title></head>
+
  &lt;body&gt;
  <body>
+
  &lt;?php include ("menu.php"); ?&gt;
  <? include ("menu.php"); ?>
+
  &lt;p&gt;this is the content of page 1&lt;/p&gt;
  <p>this is the content of page 1</p>
+
  &lt;/body&gt;
  </body>
+
  &lt;/html&gt;</nowiki></pre>
  </html></nowiki>
+
<br>Now create the following and save it as "page2.php"
 
+
<pre><nowiki>&lt;html&gt;&lt;head&gt;&lt;title&gt;Page2&lt;/title&gt;&lt;/head&gt;
 
+
  &lt;body&gt;
Now create the following and save it as "page2.php"
+
  &lt;?php include ("menu.php"); ?&gt;
<nowiki>
+
  &lt;p&gt;this is the content of page 2&lt;/p&gt;
<html><head><title>Page2</title></head>
+
  &lt;/body&gt;
  <body>
+
&lt;/html&gt;</nowiki></pre>
  <? include ("menu.php"); ?>
 
  <p>this is the content of page 2</p>
 
  </body>
 
</html></nowiki>
 
 
 
 
'''EXERCISE: See if you can create the third page??'''
 
'''EXERCISE: See if you can create the third page??'''
  
Line 48: Line 43:
  
 
save the following as "style0.css"
 
save the following as "style0.css"
<nowiki>
+
<pre><nowiki>h1
h1
+
  { color:green; }</nowiki></pre>
  { color:green; }</nowiki>
 
 
 
 
save the following as "style1.css"
 
save the following as "style1.css"
<nowiki>
+
<pre><nowiki>h1
h1
+
  { color:blue; }</nowiki></pre>
  { color:blue; }</nowiki>
 
 
 
 
save the following as "style2.css"
 
save the following as "style2.css"
<nowiki>
+
<pre><nowiki>h1
h1
+
  { color:red; }</nowiki></pre>
  { color:red; }</nowiki>
 
 
 
 
save the following as "page4.php"
 
save the following as "page4.php"
<nowiki>
+
<pre><nowiki>&lt;html&gt;
  <html>
+
  &lt;head&gt;
  <head>
+
&lt;title&gt;Dynamic CSS&lt;/title&gt;
    <title>Dynamic CSS</title>
+
  &lt;?php
  <?
+
if (isset($_POST["style"])) {
  if (isset($_POST["style"]))
+
$thisStyle=$_POST["style"];
  {
+
} else {
    $thisStyle=$_POST["style"];
+
$thisStyle="0";
  }
+
}
  else
+
print "&lt;link rel=\"stylesheet\" href=\"style".$thisStyle.".css\"&gt;";
  {
+
  ?&gt;
    $thisStyle="0";
+
&lt;/head&gt;
  }
+
&lt;body&gt;
  print "<link rel=\"stylesheet\" href=\"style".$thisStyle.".css\">";
+
&lt;h1&gt;What colour is this heading?&lt;/h1&gt;
  ?>
+
&lt;form method="post" action="&lt;?= $_SERVER["PHP_SELF"]; ?&gt;"&gt;
  </head>
+
&lt;input type="submit" name="style" value="0"&gt;&lt;BR&gt;
  <body>
+
&lt;input type="submit" name="style" value="1"&gt;&lt;BR&gt;
    <h1>What colour is this heading?</h1>
+
&lt;input type="submit" name="style" value="2"&gt;
    <form method="post" action="<?= $_SERVER["PHP_SELF"]; ?>">
+
&lt;/form&gt;
      <input type="submit" name="style" value="0"><BR>
+
&lt;/body&gt;
      <input type="submit" name="style" value="1"><BR>
+
  &lt;/html&gt;</nowiki></pre>
      <input type="submit" name="style" value="2">
 
    </form>
 
  </body>
 
  </html>
 
</nowiki>
 
 
 
 
'''EXERCISE: See if you can create a third stylesheet??'''
 
'''EXERCISE: See if you can create a third stylesheet??'''
  

Latest revision as of 14:28, 29 June 2016

Main Page >> Web Application Development >> Workbook >> Include

A word about include statements

A useful function in PHP is to include the contents of fileX in fileY at a given point.

For example:

  • You could
    • include a menu file in each page
    • or a CSS file dynamically created from a database
    • or any number of other ideas

Menu Include Example

Create the following file and save it as "menu.php"

<table border=1>
 <tr>
 <td><a href="page1.php">Page 1</a></td>
 <td><a href="page2.php">Page 2</a></td>
 <td><a href="page3.php">Page 3</a></td>
 </tr>
</table>

Now create the following and save it as "page1.php"

<html><head><title>Page1</title></head>
 <body>
 <?php include ("menu.php"); ?>
 <p>this is the content of page 1</p>
 </body>
 </html>


Now create the following and save it as "page2.php"

<html><head><title>Page2</title></head>
 <body>
 <?php include ("menu.php"); ?>
 <p>this is the content of page 2</p>
 </body>
</html>

EXERCISE: See if you can create the third page??

Dynamic CSS?

Create the following 2 cascading style sheets:

save the following as "style0.css"

h1
 { color:green; }

save the following as "style1.css"

h1
 { color:blue; }

save the following as "style2.css"

h1
 { color:red; }

save the following as "page4.php"

<html>
 <head>
 <title>Dynamic CSS</title>
 <?php
 if (isset($_POST["style"])) {
 $thisStyle=$_POST["style"];
 } else {
 $thisStyle="0";
 }
 print "<link rel=\"stylesheet\" href=\"style".$thisStyle.".css\">";
 ?>
 </head>
 <body>
 <h1>What colour is this heading?</h1>
 <form method="post" action="<?= $_SERVER["PHP_SELF"]; ?>">
 <input type="submit" name="style" value="0"><BR>
 <input type="submit" name="style" value="1"><BR>
 <input type="submit" name="style" value="2">
 </form>
 </body>
 </html>

EXERCISE: See if you can create a third stylesheet??

EXERCISE: See if you can change the buttons for radio buttons or a drop down list??

Ready to move on?

PHP106 - Flow Control - Selection