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

<!--
    demo connecting from PHP on nrs-projects using OCI
    to the Oracle student database - NO-login version,
    using a helper function hum_conn_no_login,
    and trying a query that returns multiple rows

    adapted from an example by Peter Johnson
    adapted by: Sharon Tuttle
    last modified: 2025-04-06

    uses: try-oracle.css, hum_conn_no_login.php, 328footer-plus-end.html

    you can run this using the URL:
    https://nrs-projects.humboldt.edu/~st10/s25cs328/empl-ex.php
-->

<head>
    <title> Current Employees </title>
    <meta charset="utf-8" />

    <?php
        ini_set('display_errors', 1);

        require_once("hum_conn_no_login.php");
    ?>
    
    <link href="https://nrs-projects.humboldt.edu/~st10/styles/normalize.css"
          type="text/css" rel="stylesheet" />

    <link href="try-oracle.css" type="text/css" rel="stylesheet" />
</head>

<body>
    <h1> Current Employees </h1>

    <?php
        // using our new function to streamline connecting
        //    in this no-user-login-required situation

        $conn = hum_conn_no_login();

        // if reach here, I connected!

        // REMEMBER: ***NO*** semicolon in the SQL string!!!!!

        $query_stmt = oci_parse($conn, "select empl_last_name, hiredate, salary, 
                                               commission
                                        from empl
                                        order by empl_last_name");

        oci_execute($query_stmt, OCI_DEFAULT);

        // start the table element to hold the query results
        ?>
        <table>
            <caption> Employee Info </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
        // when the statement is a select, use oci_fetch with the
        //     statement object to get access to the next row in
        //     select's result --
        // when the select result MIGHT have multiple rows,
        //     using it to control a while loop works well!
        //     (its returned result is truthy if could give access to
        //     a next row, and falsey if it could not)

        while (oci_fetch($query_stmt))
        {
            // use oci_result with the statement object and either the desired
            //     projected column's (1-based) position or name (in ALL CAPS) to get the
            //     value of that column in the current row
            // (trying both approaches here, for fun/for demo purposes!)

            $curr_empl_name = oci_result($query_stmt, "EMPL_LAST_NAME");
            $curr_hiredate = oci_result($query_stmt, 2);
            $curr_salary = oci_result($query_stmt, "SALARY");
            $curr_commission = oci_result($query_stmt, 4);

            // making a more-attractive output value for often-null
            //    commissions

            if ($curr_commission === NULL)
            {
                $curr_commission = "no commission";
            }
            else
            {
                $curr_commission = "\${$curr_commission}";
            }

            // now ready to output the table row for displaying this empl
            ?>

            <tr> <td> <?= $curr_empl_name ?> </td>
                 <td> <?= $curr_hiredate ?> </td>
                 <td> $<?= $curr_salary ?> </td>
                 <td> <?= $curr_commission ?> </td> </tr>
            <?php
        }

        // don't forget to end your table element AFTER the loop! 
        ?>
        </table>

        <?php

        // done with your statement object? FREE it!

        oci_free_statement($query_stmt);

        // done with your connection object? CLOSE it!

        oci_close($conn);

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