如何使用php和mysql显示未来30天的数据?

时间:2011-07-14 14:58:59

标签: php javascript jquery mysql

有一个jquery.ui.datepicker,如下所示:

<script type="text/javascript">
$(function() {
$("#datepicker").datepicker({
minDate: 'today',
maxDate: "+90D",
showOn: "button",
buttonImage: "images/calendar-new2.jpg",
buttonImageOnly: true,
dateFormat: "D, dd M, yy"   
});
});
</script>

并在表单中包含日期datepicker,如下所示:

<form method="post"><input align="center" type="hidden" id="datepicker" onChange="this.form.submit();" name="datepicker" value=""/></form>

以下从mysql表中调用数据:

<?php
$today = date('D, d M, Y');
$sql = "SELECT * FROM postings WHERE day1 = '$today' AND city = 'New York' OR day2 = '$today' AND city='New York' OR day3 = '$today' AND city='New York' OR day4 = '$today' AND city='New York' OR day5 = '$today' AND city='New York' OR day6 = '$today' AND city='New York' OR day7 = '$today' AND city='New York' OR day8 = '$today' AND city='New York' OR day9 = '$today' AND city='New York' OR day10 = '$today' AND city='New York'";
if($_POST!=""){
$mydate = mysql_real_escape_string($_POST['datepicker']);
if($mydate!=""){    
$sql = "SELECT * FROM postings WHERE day1 = '$mydate' AND city = 'New York' OR day2 = '$mydate' AND city='New York' OR day3 = '$mydate' AND city='New York' OR day4 = '$mydate' AND city='New York' OR day5 = '$mydate' AND city='New York' OR day6 = '$mydate' AND city='New York' OR day7 = '$mydate' AND city='New York' OR day8 = '$mydate' AND city='New York' OR day9 = '$mydate' AND city='New York' OR day10 = '$mydate' AND city='New York'";  
}       
}
if($mydate) {
echo "New York - $mydate";
}
else {
echo "New York - $today";
}
$num_results_per_page = 8;
$num_page_links_per_page = 5;
$pg_param1 = ""; // Ex: &q=value
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
pagination($sql, $num_results_per_page, $num_page_links_per_page, $pg_param);
if($pg_error == '')
{
if(mysql_num_rows($pg_result) > 0)
{
while($data = mysql_fetch_assoc($pg_result))
{ 
echo "
-------
--------
-----
";
?>

此查询将显示今日数据或根据用户点击的日期显示数据。

请帮助我以下内容: 1)下一个和上一个按钮,如果点击显示前一天的数据和下一天的数据。 2)显示接下来的7天数据。

1 个答案:

答案 0 :(得分:1)

您的查询非常低效。这将更好地工作,并且更容易准备:

$date = (isset($_POST['datepicker'])) ? "'" . mysql_real_escape_string($_POST['datepicker']) "'" : 'now()';

SELECT * FROM postings
WHERE (city = 'New York') and
   $date IN (day1, day2, day3, day4, day5, day6, day7, day8, day9, da10);

至于接下来的7天,请执行以下操作:

WHERE ...
    DATE_ADD($date, INTERVAL 7 DAY) IN (....)