drd94483 2014-02-26 14: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 15: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 DevEco studio开发工具 真机联调找不到手机设备
  • ¥15 请教前后端分离的问题
  • ¥100 冷钱包突然失效,急寻解决方案
  • ¥15 下载honeyd时报错 configure: error: you need to instal a more recent version of libdnet
  • ¥15 距离软磁铁一定距离的磁感应强度大小怎么求
  • ¥15 霍尔传感器hmc5883l的xyz轴输出和该点的磁感应强度大小的关系是什么
  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交
  • ¥30 silcavo仿真,30分钟,只需要代码
  • ¥15 FastReport 怎么实现打印后马上关闭打印预览窗口