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条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能