我正在尝试构建一个每周轮换一条消息的表单,如果不存在新消息,则返回表中的第一条消息并循环返回。我已经包含了对我已经尝试过的粗略编辑,并且它在一定程度上起作用。当前代码的问题在于它在第一周或循环一次后无法正常工作。对此非常陌生,所以希望有人可以帮助我找到更好的方法来做到这一点。
<?php
$host = "localhost";
$user = "root";
$pass = "pass";
$db = "forms";
$conn = mysql_connect($host, $user, $pass)or die("Cannot connect." . mysql_error());
mysql_select_db($db)or die("Cannot select DB." . mysql_error());
function datediff($interval, $datefrom, $dateto, $using_timestamps = false) {
/*
$interval can be:
yyyy - Number of full years
q - Number of full quarters
m - Number of full months
y - Difference between day numbers
(eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
d - Number of full days
w - Number of full weekdays
ww - Number of full weeks
h - Number of full hours
n - Number of full minutes
s - Number of full seconds (default)
*/
if (!$using_timestamps) {
$datefrom = strtotime($datefrom, 0);
$dateto = strtotime($dateto, 0);
}
$difference = $dateto - $datefrom; // Difference in seconds
switch($interval) {
case 'yyyy': // Number of full years
$years_difference = floor($difference / 31536000);
if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) {
$years_difference--;
}
if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) {
$years_difference++;
}
$datediff = $years_difference;
break;
case "q": // Number of full quarters
$quarters_difference = floor($difference / 8035200);
while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
$months_difference++;
}
$quarters_difference--;
$datediff = $quarters_difference;
break;
case "m": // Number of full months
$months_difference = floor($difference / 2678400);
while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
$months_difference++;
}
$months_difference--;
$datediff = $months_difference;
break;
case 'y': // Difference between day numbers
$datediff = date("z", $dateto) - date("z", $datefrom);
break;
case "d": // Number of full days
$datediff = floor($difference / 86400);
break;
case "w": // Number of full weekdays
$days_difference = floor($difference / 86400);
$weeks_difference = floor($days_difference / 7); // Complete weeks
$first_day = date("w", $datefrom);
$days_remainder = floor($days_difference % 7);
$odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?
if ($odd_days > 7) { // Sunday
$days_remainder--;
}
if ($odd_days > 6) { // Saturday
$days_remainder--;
}
$datediff = ($weeks_difference * 5) + $days_remainder;
break;
case "ww": // Number of full weeks
$datediff = floor($difference / 604800);
break;
case "h": // Number of full hours
$datediff = floor($difference / 3600);
break;
case "n": // Number of full minutes
$datediff = floor($difference / 60);
break;
default: // Number of full seconds (default)
$datediff = $difference;
break;
}
return $datediff;
}
$current_date = date("d F Y");
$date_difference = datediff('ww', '1 December 2015', $current_date, false);
$sql_count = "SELECT * FROM forms.quotes";
$query_count = mysql_query($sql_count);
$row_count = mysql_num_rows($query_count);
$key = true;
while($key)
{
if($row_count < $date_difference)
{
$date_difference = $date_difference - $row_count;
$key = true;
} else {
$key = false;
}
}
$sql = "SELECT * FROM forms.quotes WHERE id = '$date_difference'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "<body bgcolor='#4E2F91'><font color='#dfcd6b'><i>";
echo $row['message'];
echo "</i></font></body>";
}
echo mysql_error();
?>
答案 0 :(得分:0)
免责声明:请勿使用mysql_*
个功能。请改用mysqli
或PDO
(在示例中)。
首先你必须为当周选择合适的报价。您可以将显示的日期存储在数据库中:
$sth = $dbh->prepare('SELECT * FROM forms.quotes WHERE WEEK(date_shown)=:week_number AND YEAR(date_shown)=:year LIMIT 1');
$sth->bindParam(':week_number', date('W'));
$sth->bindParam(':year', date('Y'));
下一步如果在第一个选择语句中找不到任何内容,则必须使用当前日期更新最早显示的引号:
UPDATE forms.quotes SET shown.date = NOW() ORDER BY shown_date ASC LIMIT 1;
重复步骤#1 - 现在你应该得到一个结果。
如果要在select语句中操作结果(为窗帘周添加新引号并确保它将显示),可以在select子句中添加ORDER BY created_at DESC
语句或类似语句。