有部分文字内容需要显示在页面上,但是显示的时间比较复杂,上两天停几天如此,整个排期跨度比较大,而且不同日期会显示不同内容。
希望php+mysql实现把内容和显示的日期段录到数据库里,调用的时候再判断服务器日期是否显示。哪位朋友有这样的小程序不。目前只是像下面这样做,加很多条的话就比较麻烦,不过对数据库又不熟,希望好心人帮忙。<?php
function dayMsg($dayArr, $msg='')
{
$day_now = strtotime(date('Y-m-d'));
$queryList   = array();
if (is_array($dayArr))
{
  for($i=0,$n=count($dayArr); $i < $n; $i++)
  {
   $day_start  = $dayArr[$i]['day_start'];
   $day_end = $dayArr[$i]['day_end'];
   $queryList[] = "$day_now >= $day_start && $day_now <= $day_end";
  }
  $query  = 'if ( ' . implode(' || ', $queryList) . ' )';
  $query .= '{';
  $query .= 'echo "document.write($msg)";';
  $query .= '}';
  eval($query);  
}
}
$y = date('Y');
$day[0]['day_start'] = strtotime("$y-8-14");
$day[0]['day_end']   = strtotime("$y-8-15");
$day[1]['day_start'] = strtotime("$y-8-20");
$day[1]['day_end']   = strtotime("$y-8-22");
$day[2]['day_start'] = strtotime("$y-8-27");
$day[2]['day_end']   = strtotime("$y-8-29");
$day[3]['day_start'] = strtotime("$y-9-3");
$day[3]['day_end']   = strtotime("$y-9-5");
$day[4]['day_start'] = strtotime("$y-9-9");
$day[4]['day_end']   = strtotime("$y-9-12");
$day[5]['day_start'] = strtotime("$y-9-15");
$day[5]['day_end']   = strtotime("$y-9-17");
$day[6]['day_start'] = strtotime("$y-9-22");
$day[6]['day_end']   = strtotime("$y-9-24");
$day[7]['day_start'] = strtotime("$y-9-29");
$day[7]['day_end']   = strtotime("$y-9-30");dayMsg($day, '"内容内容"');
?>

解决方案 »

  1.   

    数据库可以这样设计
    显示内容id 开始生效时间 结束时间
    cid       begin_time  end_time显示的时候,直接select符合条件的语句.
      

  2.   

    就是要实现我上面代码的功能,在指定时间段内显示内容,因为有很多条内容,所以想改成录在数据库里的
    主要是时间段那里不知道怎么弄,把所有时间段录到一个字段里不行吗?哪位朋友能发个例子吗?[email protected]
      

  3.   

    可以建立一个表来存储数据,比如:
     create  table  time_content(time_content_id int(11) not null  auto_increment, start_time date,end_time date, content text,primary key(time_content_id));
    其中 start_time是开始时间,end_time 是结束时间,content存储你的显示内容。
    插入语句可以这么写:
    insert  into  time_content(start_time,end_time,content)  values('2008-10-23','2008-10-24','hello,world');
    当要显示的时候,可以直接从数据库中查询相应的记录和字段进行显示就可以了。
    有其他不明白的再说。
      

  4.   

    如果一天内有几条数据的话
    表中应该加一项autoid,比较好一点
    想时间插入数据表的话
    key值0--7可以设个循环变量$i
    insert  into  time_content(start_time,end_time,content)  values("$day[$i]['day_start']","$day[$i]['day_end']",'hello,world'); 
      

  5.   

    谢谢楼上朋友
    两个输入框把所有开始时间和结束时间分别录进去吗?显示的时候怎么查询是否显示呢
    好复杂,不太会弄哦,能帮忙写个程序例子吗:[email protected]
      

  6.   


    支持~真不明白楼主要怎样 说得很模糊。
    还有 什么时间段??你现在到底是要显示还是要插入数据?
    不解~如果要插入数据 提交表单就可以了。如果要显示,用php去以上数据表结构拿数据即可~
      

  7.   

    现在是要插入数据,不知道多个时间段如何录进去,表单那里要如何处理
    insert into time_content(start_time,end_time,content)  values('2008-10-23','2008-10-24','hello,world'); 
    如果hello,world这个内容还有其他显示的时间,例如继续2008-10-28到2008-10-30...2008-11-03到2008-11-05....
      

  8.   

    这个要在后台直接录入数据,写一个sql文件,导入mysql数据库中就可以了。
    比如:
    insert into time_content(start_time,end_time,content)  values('2008-10-23','2008-10-24','hello,world');   
    insert into time_content(start_time,end_time,content)  values('2008-10-28','2008-10-30','hello,world');   
    insert into time_content(start_time,end_time,content)  values('2008-11-03','2008-11-05','hello,world');   
    .............................
    .............................
    然后将该文件保存为a.sql
    在终端中选择数据库后输入:
    source  a.sql
    就把所有的数据一次导入数据库中了。