I'm a php newb so you'll have to bear with me. I'm working on a user comment section where people can submit condition/snow reports for some backcountry cabins. I've got the submission and display part down but I'd like people to be able to filter the observations by hut so they can choose to see just the reports for a particular hut. I feel like I'm close but can't seem to get the code to work. This is still just an in house mockup so that is why there's no spam filter yet and some of the code may look a bit sloppy (inline styles, etc.) Thanks in advance, any suggestions are appreciated.
PHP Code
if (isset($_GET['search']))
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
try
{
$sql = "SELECT name, comment, date, hutname FROM `comments` WHERE hut LIKE hutname ORDER BY date DESC, time DESC";
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching comments: ' . $e->getMessage();
include 'error.html.php';
exit();
}
while ($row = $result->fetch())
{
$comments[] = array(
'name' => $row['name'],
'date' => $row['date'],
'hutname' => $row['hutname'],
'comment' => $row['comment']
);
}
}
if (isset($_POST['comment']))
{
try
{
$sql = 'INSERT INTO comments SET
comment = :comment,
name = :name,
contact = :contact,
hutname = :hutname,
date = CURDATE(),
time = CURTIME()';
$s = $pdo->prepare($sql);
$s->bindValue(':comment', $_POST['comment']);
$s->bindValue(':name', $_POST['name']);
$s->bindValue(':hutname', $_POST['hut']);
$s->bindValue(':contact', $_POST['email']);
$s->execute();
}
catch (PDOException $e)
{
$error = 'Error adding submitted observation: ' . $e->getMessage();
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
try
{
$sql = "SELECT name, comment, date, hutname FROM `comments` ORDER BY date DESC, time DESC";
$result = $pdo->query($sql);
}
catch (PDOException $e)
{
$error = 'Error fetching comments: ' . $e->getMessage();
include 'error.html.php';
exit();
}
while ($row = $result->fetch())
{
$comments[] = array(
'name' => $row['name'],
'date' => $row['date'],
'hutname' => $row['hutname'],
'comment' => $row['comment']
);
}
include 'comments.html.php';
HTML Code
<body>
<?php include('../includes/header.php'); ?>
<div class="row">
<div class="eight large-8 columns">
<h4>Here are the most current conditions:</h4>
<form action="" method="get">
<div class="row">
<div class=" large-5 columns">
<label>Sort by Hut:</label>
<select name="hut" id="hut">
<optgroup label="10th Mountain Huts">
<option value="10th Mountain Division Hut">10th Mountain Division Hut</option>
<option value="Benedict Huts">Benedict Huts</option>
<option value="Betty Bear Hut">Betty Bear Hut</option>
<option value="Continental Divide Cabin">Continental Divide Cabin</option>
<option value="Point Breeze Cabin">Point Breeze Cabin</option>
<option value="Eiseman Hut">Eiseman Hut</option>
<option value="Fowler/Hilliard Hut">Fowler/Hilliard Hut</option>
<option value="Harry Gates Hut">Harry Gates Hut</option>
<option value="Jackal Hut">Jackal Hut</option>
<option value="Margy's Hut">Margy's Hut</option>
<option value="MacNamara Hut">MacNamara Hut</option>
<option value="Peter Estin Hut">Peter Estin Hut</option>
<option value="Polar Star Inn">Polar Star Inn</option>
<option value="Carl's Cabin">Carl's Cabin</option>
<option value="Sangree M. Froelicher Hut">Sangree M. Froelicher Hut</option>
<option value="Shrine Mountain Inn">Shrine Mountain Inn</option>
<option value="Skinner Hut">Skinner Hut</option>
<option value="Uncle Bud's Hut">Uncle Bud's Hut</option>
<option value="Vance's Cabin">Vance's Cabin</option>
</optgroup>
<optgroup label="Summit Huts">
<option value="Francie's Cabin">Francie's Cabin</option>
<option value="Francie's Cabin">Janet's Cabin</option>
<option value="Francie's Cabin">Ken's Cabin</option>
<option value="Francie's Cabin">Section House</option>
</optgroup>
<optgroup label="Grand Huts">
<option value="Broome Hut">Broome Hut</option>
</optgroup>
<optgroup label="Braun & Friends Huts">
<option value="Barnard Hut">Barnard Hut</option>
<option value="Goodwin Greene Hut">Goodwin Greene Hut</option>
<option value="Lindley Hut">Lindley Hut</option>
<option value="Markley Hut">Markley Hut</option>
<option value="Opa's Taylor Hut">Opa's Taylor Hut</option>
<option value="Tagert & Green-Wilson Huts">Tagert & Green-Wilson Huts</option>
<option value="Friends Hut">Friends Hut</option>
</optgroup>
</select>
</div>
<button type="submit" value="search">Sort</button>
</div>
</form>
<?php foreach ($comments as $comment): ?>
<div class="hutConditions">
<h6><?php echo htmlspecialchars($comment ['date'], ENT_QUOTES, 'UTF-8');
?>   <?php echo htmlspecialchars($comment ['hutname'], ENT_QUOTES, 'UTF-8');
?></h6>
<p><?php echo htmlspecialchars($comment ['name'], ENT_QUOTES, 'UTF-8');
?></p>
<blockquote>
<p><?php echo htmlspecialchars($comment ['comment'], ENT_QUOTES, 'UTF-8');
?>
</p>
</blockquote>
</div>
<?php endforeach; ?>
</div>
<div class="four large-4 columns">
<h5>Submit an Observation:</h5>
<form action="?" method="post">
<div>
<label for="name">Name</label>
<input type="text" name="name" id="name">
<label for="email">Email</label>
<input type="text" name="email" id="email">
<label for="hut">Hut</label>
<select name="hut" id="hut">
<optgroup label="10th Mountain Huts">
<option value="10th Mountain Division Hut">10th Mountain Division Hut</option>
<option value="Benedict Huts">Benedict Huts</option>
<option value="Betty Bear Hut">Betty Bear Hut</option>
<option value="Continental Divide Cabin">Continental Divide Cabin</option>
<option value="Point Breeze Cabin">Point Breeze Cabin</option>
<option value="Eiseman Hut">Eiseman Hut</option>
<option value="Fowler/Hilliard Hut">Fowler/Hilliard Hut</option>
<option value="Harry Gates Hut">Harry Gates Hut</option>
<option value="Jackal Hut">Jackal Hut</option>
<option value="Margy's Hut">Margy's Hut</option>
<option value="MacNamara Hut">MacNamara Hut</option>
<option value="Peter Estin Hut">Peter Estin Hut</option>
<option value="Polar Star Inn">Polar Star Inn</option>
<option value="Carl's Cabin">Carl's Cabin</option>
<option value="Sangree M. Froelicher Hut">Sangree M. Froelicher Hut</option>
<option value="Shrine Mountain Inn">Shrine Mountain Inn</option>
<option value="Skinner Hut">Skinner Hut</option>
<option value="Uncle Bud's Hut">Uncle Bud's Hut</option>
<option value="Vance's Cabin">Vance's Cabin</option>
</optgroup>
<optgroup label="Summit Huts">
<option value="Francie's Cabin">Francie's Cabin</option>
<option value="Francie's Cabin">Janet's Cabin</option>
<option value="Francie's Cabin">Ken's Cabin</option>
<option value="Francie's Cabin">Section House</option>
</optgroup>
<optgroup label="Grand Huts">
<option value="Broome Hut">Broome Hut</option>
</optgroup>
<optgroup label="Braun & Friends Huts">
<option value="Barnard Hut">Barnard Hut</option>
<option value="Goodwin Greene Hut">Goodwin Greene Hut</option>
<option value="Lindley Hut">Lindley Hut</option>
<option value="Markley Hut">Markley Hut</option>
<option value="Opa's Taylor Hut">Opa's Taylor Hut</option>
<option value="Tagert & Green-Wilson Huts">Tagert & Green-Wilson Huts</option>
<option value="Friends Hut">Friends Hut</option>
</optgroup>
</select>
<label for="comment">Conditions/Observations:</label>
<textarea id="comment" name="comment" rows="3" cols="40">
</textarea>
</div>
<button type="submit" value="Add">Submit Observations</button>
</form>
</div>
</div>
<?php include('../includes/footer.php'); ?>