Main Menu

  Homework Pages This series of scripts will allow you to turn your standard display and edit pages into homework assignment pages. You will simply add the following scripts to your current page.php and page_edit.php files and rename those files to hw.php and hw_edit.php.

In addition, you will have to make some minor changes to your password_check.php and your add_subpage_php files so that your site can handle a new page filename, hw.php and hw_edit.php. In this description page, I am trying something different. In addition to having installation notes to the right of the scripts, I am embedding them directly into the scripts using comments. The advantage of this is that the notes will accompany the scripts into your web site where they will be readily available to you.

New Database Table

 
CREATE TABLE homework (
  id int(11) NOT NULL auto_increment,
  parent_id int(11) NOT NULL default '0',
  label_1 varchar(200) NOT NULL default '',
  assignment_1 text NOT NULL,
  label_2 varchar(200) NOT NULL default '',
  assignment_2 text NOT NULL,
  label_3 varchar(200) NOT NULL default '',
  assignment_3 text NOT NULL,
  label_4 varchar(200) NOT NULL default '',
  assignment_4 text NOT NULL,
  label_5 varchar(200) NOT NULL default '',
  assignment_5 text NOT NULL,
  label_6 varchar(200) NOT NULL default '',
  assignment_6 text NOT NULL,
  label_7 varchar(200) NOT NULL default '',
  assignment_7 text NOT NULL,
  label_8 varchar(200) NOT NULL default '',
  assignment_8 text NOT NULL,
  KEY id (id)
) TYPE=MyISAM;
			

Database Notes: This is a new database table. The command script to the left is written for MySQL. There are a number of ways to create this table. You can paste it in by telnetting to the database, logging in, and pasting the script in. It is easier if you have MySQLAdmin installed on the server so that you can paste the script into the Run SQL query/queries on database... textarea box.
  Homework Display Page It is recommended that you use your current standard display page for a beginning. One reason for this is that you already have standard scripts installed and working. The more important reason is that web users find it easier to work with web sites where all pages have a standard look and method of navigating.
 
<?
//                        Loading Assignments from              //
//                                the Assignments database      //
//    ------------------------------------------------------    //
//                                                              //
/*
     This module goes to a new database, the "homework"
     database.  It extracts the assignments fields where
     the "parent_id" field equals the "id" code of the
     page.
     
     This module must be placed beneath the standard page
     loading module and above the beginning HTML tag.
*/

//                  Opens database where the host name
//                  of your server and the database
//                  name and the database password.
    $db=mysql_connect("hostname","database name","database password");
    mysql_select_db("database name",$db);
//                  Selects the record where the 
//                  "parent_id" field equals the "id"
//                                   number of the page.
        $result=mysql_query("select * from homework where parent_id = $id",$db);
        While ($myrow = mysql_fetch_row($result))
            {
//                  Assigns variable names to
//                  the fields.  "label_[n]" is the
//                  variable for each of the eight
//                  possible assignments, and
//                  "assignment_[n]" is the variable
//                  for the assignment descriptions.
                 $homework_id = $myrow[0];
                 $label_[1] = $myrow[2];
                 $assignment_[1] = $myrow[3];
                 $label_[2] = $myrow[4];
                 $assignment_[2] = $myrow[5];
                 $label_[3] = $myrow[6];
                 $assignment_[3] = $myrow[7];
                 $label_[4] = $myrow[8];
                 $assignment_[4] = $myrow[9];
                 $label_[5] = $myrow[10];
                 $assignment_[5] = $myrow[11];
                 $label_[6] = $myrow[12];
                 $assignment_[6] = $myrow[13];
                 $label_[7] = $myrow[14];
                 $assignment_[7] = $myrow[15];
                 $label_[8] = $myrow[16];
                 $assignment_[8] = $myrow[17];
            }
//                  Normally, HTML will not display
//                  standard return characters.
//                  The "nl2br()' function converts all
//                  soft returns in the assignments to
//                  a <br> tag.
          $assignment_[1] = nl2br($assignment_[1]);
          $assignment_[2] = nl2br($assignment_[2]);
          $assignment_[3] = nl2br($assignment_[3]);
          $assignment_[4] = nl2br($assignment_[4]);
          $assignment_[5] = nl2br($assignment_[5]);
          $assignment_[6] = nl2br($assignment_[6]);
          $assignment_[7] = nl2br($assignment_[7]);
          $assignment_[8] = nl2br($assignment_[8]);
//                  Because there are characters (such
//                  as quotations) that have specific
//                  functions for PHP and other languages,
//                  the "addslashes()" will place a forward
//                  slash in front of such characters, in
//                  affect, canceling them out.  The slashes
//                  will be removed below with the 
//                  "stripslashes()" function.
          $assignment_[1] = addslashes($assignment_[1]);
          $assignment_[2] = addslashes($assignment_[2]);
          $assignment_[3] = addslashes($assignment_[3]);
          $assignment_[4] = addslashes($assignment_[4]);
          $assignment_[5] = addslashes($assignment_[5]);
          $assignment_[6] = addslashes($assignment_[6]);
          $assignment_[7] = addslashes($assignment_[7]);
          $assignment_[8] = addslashes($assignment_[8]);

//                                                              //
//    ------------------------------------------------------    //
?>


			
Display Page Notes:
Retrieve Assignments from Database
  1. Open your page.php file into your text editor and then resave it as hw.php.
  2. Copy the script to the left and then paste it beneath the Load Content from database script that is already there and above your beginning <HTML> tag.
 
Display Assignments

This script can be pasted almost anywhere on your page. It makes sense, though, to paste it directly beneath a current content element. The teacher can then use the content element to explain homework and project policies and other information that applies to all assignments.

 
 



<?
//                              DISPLAY HOMEWORK ASSIGNMENTS       //
//     -------------------------------------------------------     //
//                                                                 //
/*
          This module has a counter that counts from one
          to eight.  For each count, it checks to see if 
          there is a homework label for that period or
          subject.  If there is, then it displays the label
          and the assignment with a table.  The table is 
          described in this routine, but it can be altered
          to your desired layout.
*/
echo("
<table>
     ");
     
//                                   The counter
for (     $position = 1;
          $position < 9;
          $position++)
//                  Begin the counting here.
     {
//                  Converts the variable name
//                  to a smaller string for
//                  convenience.
          $pstn = $position;
//                  Tests for label.  If there
//                  is no label, then the assignment
//                  display for the $pstn number
//                  will be skiped.
     if (strlen($label_[$pstn]) > 0)
          {
//                  Strips slashes out of the 
//                  assignment.  Slashes were inserted
//                  above to protect from dangerious
//                  characters such as quotation marks.
               $assignment_[$pstn] = stripslashes($assignment_[$pstn]);
echo("
     <tr>
          <td     colspan=2>
<!--                  Display the label of the assignment.
                  This could be a period number of the
                  name of a subject                    -->
               <b>$label_[$pstn]</b>
          </td>
     </tr>
     <tr>
          <td     width=20>
          </td>
          <td>
               <font     size=1
                         face=arial,helvetica>
<!--                              Display the assignment               -->
                    $assignment_[$pstn]</font></td>
     </tr>
     ");
          }     //                  End test for label here
     }           //                  End the counting here
echo("
</table>
     ");

//                                                              //
//    ------------------------------------------------------    //
?>



			
Display Assignments Notes: Table tags have already been inserted into the script to the left. These tags will create a fairly uniform layout of the assignments in a way that is easy to use and follow. However, feel free to adapt the table and further formate the text of the assignments in anyway that you like.
  Homework Edit Page: As with the homework display page, it will be easiest to start with your current standard page edit version. This will carry the same look as feel of your existing site over to the homework pages, and it will allow you to use scripts that have already been installed and are working.

The first thing that you will need to do is to add some script to your password check routine (password_check.php). Remember that the password check routine accepts the password and then forwards the user to the edit page, which then tests the password and continues if the password is correct, or returns the user to the password_check.php page if it does not match.

The problem is that we need to adapt the password check routine so that it knows when to to forward to page_edit.php and when to forward to hw_edit.php.

 
 



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
     <HEAD>
          <TITLE></TITLE>
          <META NAME="author" CONTENT="David Warlick">
          <META NAME="generator" CONTENT="Raw Code">
     </HEAD>

<BODY BGCOLOR="#FFFFFF">
     <table border=0 width=100% height=100%>
          <tr>
               <td align=center valign=middle>
<?
//                         P A S S W O R D                  //
//                         R O U T I N E                    //
//     --------------------------------------------         //
/*
     This module places a text box on the page
     with the name of the page being edited.
     After filling the password type textbox,
     the user is sent to the edit page where 
     the password is checked.  If it does not
     match the password in the database, the user
     is sent back here.
*/
// Access          
     $db=mysql_connect("hostname","database name","database password");
     mysql_select_db("database name",$db);
          $result=mysql_query("select * from page where id = $id",$db);
          While ($myrow = mysql_fetch_row($result))
               {
                    $page_title = $myrow[20];
                    $page_type = $myrow[30];
               }
//                  Test for page type.  if     
//                  the type is "homework"   
//                  then the form action    
//                  is directed to the         
//                  hw_edit.php file.          
//                  otherwise, it is sent    
//                  to the page_edit.php   
//                  file.                     
                    $filename = 'page_edit.php';
          if ($page_type == 'homework')
               {
                    $filename = 'hw_edit.php';
               } 
//                  The action file has been changed
//                  the reflect the filename tested  
//                  for above, hw_edit.php or     
//                  page_edit.php.  
echo("
<form action=$filename method=post>
<font face=arial,helvetica>
Editing <font color=#ff0000><b>$page_title</b></font><br>
<font size=2>Enter Password: 
<input type=password name=entered_password size=8 maxlength=8></font>
<input type=hidden name=id value=$id>
<input type=hidden name=page_filename value=$page_filename>
<input type=submit value=go>
</font>
</form>
     ");
?>
               </td>
          </tr>
     </table>
</BODY>
</HTML>


			
Notes: The script to the left already exists in your password check page. The items that are colored red are either new and should be added in the indicated position, or should be changed. The purpose of the changes is to determine whether it should forward to the standard page_edit.php file or the new hw_edit.php file.
  Updating Assignments Database:

If assignments or other content elements have been edited, the script below will update the assignments database record for this page.

 
 
<?
//          Updating homework assignment content                   //
//     ---------------------------------------------------------   //
//                                                                 //
/*
     this module will update the current record for this page
     on the assignments database.
*/
//                  Opens database where the host name
//                  of your server and the database
//                  name and the database password.
    $db=mysql_connect
    ("hostname","database name","database password");
    mysql_select_db("database name",$db);
        $sql="update homework 
            set    
                    label_1 = '$label_[1]',
                    assignment_1 = '$assignment_[1]',
                    label_2 = '$label_[2]',
                    assignment_2 = '$assignment_[2]',
                    label_3 = '$label_[3]',
                    assignment_3 = '$assignment_[3]',
                    label_4 = '$label_[4]',
                    assignment_4 = '$assignment_[4]',
                    label_5 = '$label_[5]',
                    assignment_5 = '$assignment_[5]',
                    label_6 = '$label_[6]',
                    assignment_6 = '$assignment_[6]',
                    label_7 = '$label_[7]',
                    assignment_7 = '$assignment_[7]',
                    label_8 = '$label_[8]',
                    assignment_8 = '$assignment_[8]'
            where id = $homework_id";
        $result =    mysql_query($sql); 
        
//                                                                 //
//     ---------------------------------------------------------   //
?>
			
Notes: This script should be pasted directly beneath the Updating content for the page script, after the link that begins with
$result =....
  Accessing Existing Assignments Routine:

The following routine will access the homework database record for the current page and collect assignment labels and descriptions so that they can be included in the web form for editing.

 
 
<?
//                        Loading Assignments from            //
//                                the Assignments database    //
//    ------------------------------------------------------  //
//                                                            //
/*
     This module goes to a new database, the "homework"
     database.  It extracts the assignments fields where
     the "parent_id" field equals the "id" code of the
     page.
     
     This module must be placed beneath the standard page
     loading module and above the beginning HTML tag.
*/

//               Opens database where the host name
//               of your server and the database
//               name and the database password.
    $db=mysql_connect("hostname","database name","database password");
    mysql_select_db("database name",$db);
//                  Selects the record where the 
//                  "parent_id" field equals the "id"
//                                   number of the page.
        $result=mysql_query("select * from homework where parent_id = $id",$db);
        While ($myrow = mysql_fetch_row($result))
            {
//               Assigns variable names to
//               the fields.  "label_[n]" is the
//               variable for each of the eight
//               possible assignments, and
//               "assignment_[n]" is the variable
//               for the assignment descriptions.
                 $homework_id = $myrow[0];
                 $label_[1] = $myrow[2];
                 $assignment_[1] = $myrow[3];
                 $label_[2] = $myrow[4];
                 $assignment_[2] = $myrow[5];
                 $label_[3] = $myrow[6];
                 $assignment_[3] = $myrow[7];
                 $label_[4] = $myrow[8];
                 $assignment_[4] = $myrow[9];
                 $label_[5] = $myrow[10];
                 $assignment_[5] = $myrow[11];
                 $label_[6] = $myrow[12];
                 $assignment_[6] = $myrow[13];
                 $label_[7] = $myrow[14];
                 $assignment_[7] = $myrow[15];
                 $label_[8] = $myrow[16];
                 $assignment_[8] = $myrow[17];
            }
?>
			
Notes: This module must be inserted above the beginning <HTML> tag.
 
Assignments Edit Routine:

This routine includes form tags that will produce a series of eight single-line and multiline text boxes. The single-line boxes will be for each assignment label (1st Period or Language Arts). The multiline text boxes will be used by the teacher to enter or edit homework assignments.

 
 


<?
//                    WRITING HOMEWORK ASSIGNMENTS              //
//    ------------------------------------------------------    //
//                                                              //
/*
    This module will present a web form of eight elements, each
    element asking for an assignment label (the period number of
    subject) and the assignment description.
    
    The module has a counter that will count to eight, creating
    the approriate web form for each element.
    
    This module must be placed within the beginning and ending
    form tags (<form></form>) of the standard content elements.
*/

//               The counter runs from one to eight
for (    $position = 1;
        $position < 9;
        $position++)
//               Counting starts here
    {
//               Converts value of "$position" to
//               "$pstn" for a shorter string.
//               The "addslashes() function protects
//               from dangerious characters such as
//               quotations.
            $pstn = $position;
echo("
<!--               Formating for a prompt that will
               tell the teacher what to enter -->
<font    size=4><b>Homework Assignments:</b></font><hr size=4>
<font    size=1
        face=arial,helvetica
        color=#808080>Label for the assignment $pstn 
        (subject or period number)</font><br>
<input    type=text
        size=40
        name=label_[$pstn]
        value="$label_[$pstn]"><br>
<font    size=1
        face=arial,helvetica
        color=#808080>Assignment including description, resources, 
and due date</font><br>
<textarea    name=assignment_[$pstn]
            cols=70
            rows=6
            wrap=virtual>$assignment_[$pstn]</textarea><hr size=1>
    ");
    }    //                    Counting ends here
echo("
<!--               Carries the id number for this
               homework page to the save routine -->
        <input    type=hidden
                name=homework_id
                value=$homework_id>
        <input    type=hidden
                name=type
                value=$type>
    ");


//                                                              //
//    ------------------------------------------------------    //
?>


			
Notes: This module generates the form for entering the the assignment labels and descriptions. It is important that this script be inserted inside of the current <form></form> tags, ideally above the submit button or <input type=submit... tag.

In addition, it is important to change the action attribute of the form tag from page_edit.php to hw_edit.php.

 
Page Submenu Routine:

Finally, since the homework pages use a different filename, (hw.php), the automatic menu on the standard pages needs some adapting so that they can detect which pages use the standard page.php filename, and which should linke to the new hw.php.

 
 
Assignments Edit Routine:

This routine includes form tags that will produce a series of eight single-line and multiline text boxes. The single-line boxes will be for each assignment label (1st Period or Language Arts). The multiline text boxes will be used by the teacher to enter or edit homework assignments.

 
 


<?
//                  Submenu Routine                          //
//   ------------------------------------------------------  //
//                                                           //
/*
   This module will search the page database for pages
   that have been added from this page -- sub pages of
   the current page.  Those page titles are accessed by
   this module and listed as a submenu.
*/
echo("
<table cellpadding=3 cellspacing=0 border=0>

   ");
// Access      
   $db=mysql_connect("host name","database name","database password");
   mysql_select_db("database name",$db);
      $result=mysql_query("select * from page where parent_id = $id",$db);
      While ($myrow = mysql_fetch_row($result))
         {
   $db=mysql_connect("host name","database name","database password");
   mysql_select_db("database name",$db);
      $resulta=mysql_query("select * from page where id = $myrow[0]",$db);
         While ($myrowa = mysql_fetch_row($resulta))
            {
               $sub_page_title = $myrowa[20];
               $sub_page_type = $myrowa[30];
            }   
         $filename = 'page.php';   
         if ($sub_page_type == 'homework')
            {
               $filename = 'hw.php';
            }
echo("
<tr>
<td>
<font size=1 face=arial,helvetica>
<a href=$filename?id=$myrow[0]>
$sub_page_title</a></font></td></tr>
   ");   
         }
echo("

</table>
   ");
//                                                           //
//   ------------------------------------------------------  //
?>
			
Notes: Make the changes indicated in red.
  New Add Sub-page Page With the edition of a new type of page, the need now exists for being able to specify whether you want a standard web page or a homework page when adding a new sub-page. The following code is a re-write of the Add Sub-page Page routine. It includes a set of radio buttons, which the web owner will use to indicate the type of sub-page desired.  
 



    

<?
//    -------------------------------------------------------    //
//                        A D D   S U B - P A G E                //
//    -------------------------------------------------------    //
/*
    This set is designed to allow users to create additional
    web pages that will be added as a links from the page
    that they are added from.  The result is a mini-website,
    and additional mini-websites within the original.
    
*/
?>
<?
//    -------------------------------------------------------    //
//                        Establishing new web page              //
//                        by adding preliminary                  //
//                        content.                               //
/*
    This module tests for readiness to add new page.  If
    the information has been added and the submit button
    was clicked, then this routine will add the page with
    the content.
    
    This module will also check for the id of the new page
    and redirect to the edit version of the added page.
    
    Note: do not add any codes, php or html that will output
    content to the browser before this routine.
*/
if ($mode == 'add')
    {
//                        accessing password        //
// Access        
    $db=mysql_connect("host name","database name","database password");
    mysql_select_db("database name",$db);
        $result=mysql_query("select * from page where id = $parent_id",$db);
        While ($myrow = mysql_fetch_row($result))
            {
                $password = $myrow[29];
            }
        $code = time();
// Add
//                            Added "type" field so that the    //
//                            page table knows that it is         //
//                            actually a homework page.           //
    $db=mysql_connect("host name","database name","database password");
    mysql_select_db("database name",$db);
            $sql="insert into page
                    (    name_contact,
                        page_title,
                        parent_id,
                        code, 
                        password,
                        type)
            values    (    '$name_contact',
                        '$page_title', 
                        '$parent_id',
                        '$code',
                        '$password',
                        '$type')";
            $result = mysql_query($sql);
// Access        
    $db=mysql_connect("host name","database name","database password");
    mysql_select_db("database name",$db);
        $result=mysql_query("select * from page order by id desc",$db);
        While ($myrow = mysql_fetch_row($result))
            {
                if ($myrow[28] == $code)
                    {
                        $id = $myrow[0];
                        break;
                    }
            }
//        Module for adding homework assignments record         //
//    ------------------------------------------------------    //
//                                                              //
/*
                            This module will test for intent
                            to create a homework page.  If true
                            then it will create a record in the
                            homework database table, and add the
                            id of the page record as "parent_id".
*/
    if ($type == 'homework')
        {
// Add
//                            Opens database table "homework"    //
//                            with the host name, database         //
//                            name, and password.                  //
//                            Then it adds the new record          //
//                            for homework assignments             //
            $db=mysql_connect("host name","database name","database password");
            mysql_select_db("database name",$db);
            $sql="insert into homework
                    (    parent_id)
            values    (    '$id')";
            $result = mysql_query($sql);
        }
//                                                              //
//    ------------------------------------------------------    //
//                                Test for homework or          //
//                                standard page, and direct     //
//                                to appropriate Edit page.     //
    if ($type == 'homework')
        {
Header("Location: hw_edit.php?id=$id");
        }    else
        {
Header("Location: page_edit.php?id=$id");
        }
    }
//                                                               //
//    -------------------------------------------------------    //
?>




<?
//    -------------------------------------------------------    //
//                    Form for Submitting New Sub-Page           //
/*
    This module will display a form for submitting pre-
    liminary content for addition of a sub-page for the
    current page.
*/

echo("
        <form    action=add_subpage.php
                method=post>
            Owner's Name:
                <input    type=text
                        size=30
                        name=name_contact
                        value='$name_contact'><br>
            Page Title:
                <input    type=text
                        name=page_title
                        value=$page_title><br>
                        
<!--                    Added radio buttons so that the teacher
                        has an option of creating a standard
                        content page or a homework assignments
                        page.                                -->
                <input    type=radio
                        name=type
                        value=standard
                        checked> Standard Page<br>
                <input    type=radio
                        name=type
                        value=homework> Homework Page<br>
                <input    type=hidden
                        name=mode
                        value='add'>
                <input    type=hidden
                        name=parent_id
                        value=$parent_id><hr>
                <input    type=submit
                        value='Add Page'>
        </form>
    ");
//                                                               //
//    -------------------------------------------------------    //
?>