如果自上次访问该注册用户以来创建了sql记录,并且如果未满足条件,则可以创建一个将更改的图像。
记录表为matningar
,日期字段为datum
。
也许下面的代码接近实现这个?!
<?php
$stmt = "SELECT * FROM matningar WHERE `datum` > date("F j, Y, g:i a", $database- >getLastUserRegisteredDate()";
$result=mysql_query($stmt);
foreach($result as $rec){
echo "<img src=\"images/somepicture.png\" />";
}
?>
非常感谢有关如何继续的一些意见!
答案 0 :(得分:2)
我认为最好的方法是在创建表时使用此方法存储更新的记录时间。
updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
这样当您从数据库中选择记录并在页面上显示它们时,您可以执行类似的操作,根据当前用户上次访问显示单独的图像:
foreach($results as $record) {
if($record['updated'] > $currentUser->getLastVisited()) {
echo "<img .... />"; // Has been modified since last visit
} else {
echo "<img .... />"; // Not been modified since last visit
}
// Display rest of this record
}
答案 1 :(得分:1)
您可以计算返回的记录并使用条件语句来确定要显示的图像。例如,使用您的代码:
/* Using a mysql query which is discouraged and will be depreceated in future */
// declare variables
$i = 0;
$lastRegisteredDate = date("F j, Y, g:i a", $database->getLastUserRegisteredDate());
// declare statement string
$stmt = "SELECT * FROM matningar WHERE `datum` > $lastRegisteredDate";
// execute query
$result=mysql_query($stmt);
// make sure query executed properly
if (!$result) {
die('Invalid query: ' . mysql_error());
}
// manually count the number of results
while ($row = mysql_fetch_assoc($result)) {
$i++;
}
// display image based on conditions
if($i == 0) {
// display one image
}
else {
// display another image
}
正如附注所述,mysql函数将在即将发布的PHP版本中被删除,因此我将开始考虑使用PDO或mysqli库进行mysql查询。
/* Using the PDO library */
// declare variables
$i = 0;
$lastRegisteredDate = date("F j, Y, g:i a", $database->getLastUserRegisteredDate());
// declare database handler
$DBH = new PDO( "mysql:host=$host;dbname=$dbname", $user, $pass );
// prepare query
$STH = $DBH->prepare( "SELECT * FROM matningar WHERE `datum` > ?" );
// execute query
$STH->execute( array( $lastRegisteredDate ) );
// set fetch mode
$STH->setFetchMode( PDO::FETCH_OBJ );
// manually count the number of results
while ( $row = $STH->fetch() ) {
$i++;
}
// display image based on conditions
if($i == 0) {
// display one image
}
else {
// display another image
}