<?php

// create connection to database
$host = "webdev.iyaserver.com";
$userid = "dent_student";
$userpw = "code4Studentuse";
$db = "dent_movies";

$mysql = new mysqli(
    $host,
    $userid,
    $userpw,
    $db);

if($mysql->connect_errno) {
    echo "db connection error : " . $mysql->connect_error;
    exit();
}

// base query
$sql =      "SELECT * from view1 WHERE 1=1";

// ADD ON FILTER
$sql .= " AND title like 'R%' AND rating = 'PG-13' ";

// ADD ON sort order
$sql .= " ORDER BY title";

// submit query to database, store returned data as $results
$results = $mysql->query($sql);

if(!$results) {
    echo "SQL error: ". $mysql->error . " running query <hr>" . $sql . "<hr>";
    exit();
} else {
    // put sql into comment block of webpage
    echo "<!-- SQL: $sql -->";
}

/*
    Note: $results is a database "result set".
    It has a property $results->num_rows that is a number (how many rows of data)
*/

// Code to output one sample row with list of columns
$sample = $results->fetch_assoc();
echo "<!-- Sample first row of data <br>";
print_r($sample);
echo "</pre> -->";
$results->data_seek(0); // reset results

// close php block
?>


<html>
<head>
        <title>Horror Movies</title>
    <style>
        table { width: 900px; background-color: steelblue; cellpadding="9"; cellspacing="9";}
        tr { }
        td { width: 200px; background-color: lightsteelblue; }
        .header { color:white; background-color: steelblue;}
    </style>
</head>
<body>
<h3>Horror Movies</h3>

Movie Results:

<?= $results->num_rows ?> Universal Studios Horror films 
<hr>

<table >
<tr>
    <td class="header">Title<hr></td>
    <td class="header">Rating<hr></td>
    <td class="header">Genre<hr></td>
    <td class="header">Studio<hr></td>
</tr>


<?php //BACK into php
// create output loop around rows of database results
while( $currentrow = $results->fetch_assoc() ) { // OPEN/active php loop
    // while in repeating loop, back into html mode
?>
<tr>
    <td><strong><?= $currentrow["title"]?></strong></td>
    <td><?= $currentrow["rating"]?></td>
    <td><em><?= $currentrow["genre"]?></em></td>
    <td><?= $currentrow["label"]?></td>

<?php } ?>

</table>






</body>
</html>
