<?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 label LIKE 'Universal%' AND rating = 'G' AND genre = 'Adventure' ";

// 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 -->";
}

// 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

?>

<html>
<head>
    <title></title>
    <style>
        body { background-color:Orange; width: 600px; margin:auto;}
        h3 { background-color: Coral; text-align:center; }
        table { width: 100%; background-color:Gray;}
        th, td { border: 1px solid black; padding: 8px; text-align: left; }
        th { background-color: Coral; }
    </style>
</head>
<body>

<h3>Universal Studios Horror Films</h3>

<?= $results->num_rows ?> Universal Studios films that are Rated G and are Adventure genres
<hr>

<table>
    <tr>
<th>Movie Title</th>
        <th>Rating</th>
        <th>Studio Label</th>
        <th>Genre</th>
    </tr>
    
    <?php while( $currentrow = $results->fetch_assoc() ) { ?>
        <tr>
            <td><?= $currentrow["title"] ?></td>
            <td><?= $currentrow["rating"] ?></td>
            <td><?= $currentrow["label"] ?></td>
            <td><?= $currentrow["genre"] ?></td>
        </tr>
    <?php } ?>
</table>

</body>
</html>