我正在尝试创建一个程序,通过它我可以显示任何特定的关键字(搜索词),在特定日期搜索了多少次。
我是否有人正在搜索Stackoverflow,所以我想显示其搜索统计数据的特定数据
例如;
at 2013-04-01 -> stackoverflow was searched for 15 times<br>
at 2013-04-02 -> stackoverflow was searched for 28 times<br>
at 2013-04-03 -> stackoverflow was searched for 10 times<br>
我希望你现在明白我想说的话。,
现在让我展示我为制作这个节目所做的工作。
请参阅此代码
<?php
$keyword = null;
if (!empty($_GET['s'])) {
$keyword = stripslashes($_GET['s']);
try {
$objDb = new PDO('mysql:dbname=test;charset=UTF-8', 'root', '');
$sql = "INSERT INTO `search` (`keyword`)
VALUES (:keyword)
ON DUPLICATE KEY UPDATE `times` = `times` + 1";
$statement = $objDb->prepare($sql);
$statement->execute(array(':keyword' => $keyword));
} catch(PDOException $e) {
echo $e->getMessage();
}
}
try {
$objDb = new PDO('mysql:dbname=test;charset=UTF-8', 'root', '');
$sql = "SELECT `s`.*,
IF (
(
SELECT `id`
FROM `search`
ORDER BY `date` DESC
LIMIT 0, 1
) = `s`.`id`,
1,
0
) AS `latest`
FROM `search` `s`
ORDER BY `date` DESC LIMIT 0, 50";
$statement = $objDb->query($sql);
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo $e->getMessage();
}
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Save search requests to database</title>
<meta name="description" content="Save search requests to database" />
<meta name="keywords" content="Save search requests to database" />
<link href="/css/core.css" rel="stylesheet" type="text/css" />
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<section id="wrapper">
<form method="get">
<input type="text" name="s" id="search" class="field" value="<?php echo $keyword; ?>" />
<input type="submit" class="button" value="Search" />
</form>
<?php if (!empty($result)) { ?>
<table cellpadding="0" cellspacing="0" border="0" class="tbl_repeat">
<tr>
<th>Keyword</th>
<th class="tar">Searched</th>
</tr>
<?php foreach($result as $row) { ?>
<tr<?php echo $row['latest'] == 1 ? ' class="active"' : null; ?>>
<td><?php echo $row['keyword']; ?></td>
<td class="tar"><?php echo $row['times']; ?></td>
</tr>
<?php } ?>
</table>
<?php } else { ?>
<p>There are currently no searches available.</p>
<?php } ?>
</section>
</body>
</html>
这是一个简单的程序,它将最近搜索过的关键字保存在数据库中并在网页上显示,它还显示关键字获取搜索的次数..实际上它只是用更新的时间戳替换当前时间戳。
现在任何人都可以帮助添加一个特定的条件来检查今天的日期,然后它将在数据库中添加条目,如果在同一天研究了那个特定的单词,那么它将只更新current_timestamp和更新的时间戳。
我只想根据日期划分最近的搜索查询。
这是在您的数据库中制作此表的代码
CREATE TABLE IF NOT EXISTS `search` ( <br>
`id` int(11) unsigned NOT NULL AUTO_INCREMENT, <br>
`keyword` varchar(100) NOT NULL, <br>
`times` int(11) unsigned DEFAULT '1', <br>
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, <br>
PRIMARY KEY (`id`), <br>
UNIQUE KEY `keyword` (`keyword`) <br>
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=301 ; <br>
请回复示例数据..
答案 0 :(得分:0)
怎么样
SELECT keywords, COUNT(*) as no_of_times, date
FROM search
GROUP BY date, keywords
会给你类似的东西
keywords | no_of_times | date |
------------------------------------------
stackoverflow | 25 | 2012-10-12 |
meta | 10 | 2012-10-12 |
答案 1 :(得分:0)
我有一个简单的解决方案,我现在可以想到。
$search_term = $_POST['search_term'];
$date = $_POST['date'];
$select = mysqli_query("select * from search where keyword='$search_term' and date='$date'");
$rowCheck = mysqli_num_rows($select);
if($rowCheck>0) {
$fetch = mysqli_fetch_array($select);
$times = $fetch['times'];
$update_query = // RUN UPDATE QUERY HERE
} else {
// RUN INSERT QUERY HERE
}
希望这有帮助