drd94483 2014-02-26 22:59
浏览 46
已采纳

过滤/排序用户评论PHP

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 &amp 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 &amp Green-Wilson Huts">Tagert &amp 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');
?> &nbsp <?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 &amp 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 &amp Green-Wilson Huts">Tagert &amp 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'); ?>
  • 写回答

2条回答 默认 最新

  • duannengling4705 2014-02-26 23:06
    关注

    as @Fabio said

    LIKE must be enclosing with quotes.

     SELECT     name, comment, date, hutname 
     FROM       `comments` 
     WHERE      hut LIKE 'hutname' 
     ORDER BY   date DESC, time DESC
    

    or the wildcard if you need more generals results

     SELECT     name, comment, date, hutname 
     FROM       `comments` 
     WHERE      hut LIKE '%hutname%' 
     ORDER BY   date DESC, time DESC
    

    Read the LIKE section in the manual

    Note: try eneable display_errors in php in the dev enviroment using

    error_reporting(-1);
    ini_set('display_errors', true);
    

    or in the php.ini to get the errors and should be more easy the debug proccess

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写uniapp时遇到的问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流