我正在尝试制作一个程序,通过它我可以显示任何特定的关键字(搜索词),在特定日期搜索了多少次。
如果有人正在搜索 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>
请用示例数据回复..