<!DOCTYPE html>
<html  xmlns="http://www.w3.org/1999/xhtml">

<!--
    demo connecting from PHP on nrs-projects
    to the Oracle student database on cedar

    adapted from an example by Peter Johnson
    adapted by: Sharon Tuttle
    last modified: 2016-03-06
-->

<head>
    <title> Connecting to Oracle! </title>
    <meta charset="utf-8" />

    <link href="http://users.humboldt.edu/smtuttle/styles/normalize.css" 
          type="text/css" rel="stylesheet" />
    <link href="lab07.css" type="text/css" rel="stylesheet" />
</head> 

<body>

<h1> Connecting PHP to Oracle </h1>

<?php
    // do you need to ask for username and password?

    if ( ! array_key_exists("username", $_POST) )
    {
        // no username in $_POST? they need a login
        //     form!
        ?>
  
        <form method="post" 
              action="<?= htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES) ?>">
        <fieldset>
            <legend> Please enter Oracle username/password: </legend>

	        <label for="username"> Username: </label>
            <input type="text" name="username" id="username" /> 

            <label for="password"> Password: </label>
            <input type="password" name="password" 
                   id="password" />

            <div class="submit">
                <input type="submit" value="Log in" />
            </div>
        </fieldset>
        </form>
    <?php
    }      

    // otherwise, handle the submitted login form 
    //    (or try to) -- and show the user some
    //    lovely employee information

    else
    {
        // I am a little paranoid -- I'm stripping
        //   tags from the username 

        $username = strip_tags($_POST['username']);

        // the ONLY thing I am doing with this is
        //    try to log in... so I HOPE this is OK!

        $password = $_POST['password'];

        // set up db connection string

        $db_conn_str = 
            "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)
                                       (HOST = cedar.humboldt.edu)
                                       (PORT = 1521))
                            (CONNECT_DATA = (SID = STUDENT)))";

        // let's try to log on using this string!

        $conn = oci_connect($username, $password, $db_conn_str);

        // exiting if can't log in

        if (! $conn)
        {
        ?>
            <p> Could not log into Oracle, sorry. </p>

            <?php
            /*----- 
                why am I using this footer file? Because I *want* its
                closing tags before exiting PHP...

                (can imagine some nice refactorings to this, BUT need
                to get this example POSTED...!)
            -----*/

            require_once("328footer.txt");
            exit;        
        }
       
        // if I get here -- I connected!!

        // set up a query and execute it!

        $empl_query = 'select empl_last_name, hiredate, '.
                              'salary, commission '.
                      'from empl';
        $stmt = oci_parse($conn, $empl_query);
        oci_execute($stmt, OCI_DEFAULT);
        ?>

        <table>
        <caption> Employee information </caption>
        <tr> <th scope="col"> Employee Name </th>
             <th scope="col"> Hire Date </th>
             <th scope="col"> Salary </th>
             <th scope="col"> Commission </th> </tr>

        <?php
        while (oci_fetch($stmt))
        {
            $curr_empl_name = oci_result($stmt, "EMPL_LAST_NAME");
            $curr_hiredate = oci_result($stmt, "HIREDATE");
            $curr_salary = oci_result($stmt, "SALARY");
            $curr_commission = oci_result($stmt, "COMMISSION");

            if ($curr_commission === NULL)
            {
                $curr_commission = "no commission";
            }
            ?>

            <tr> <td> <?= $curr_empl_name ?> </td> 
                 <td> <?= $curr_hiredate ?> </td>
                 <td class="numeric"> <?= $curr_salary ?> </td>
                 <td class="numeric"> 
                     <?= $curr_commission ?> 
                 </td>
            </tr>
            <?php
        }
        ?>
        </table>

        <?php
        // FREE your statement, CLOSE your connection!

        oci_free_statement($stmt);
        oci_close($conn);
    }

    require_once("328footer-better.html");
?>  

</body>
</html>