日历价格表?
问题:价格人数信息不能对应数组里面有的日期才显示(比如数组里面有2013-10-12和2013-10-21才有价格和人数,但程序会依次从开始日期显示,而不是在生成的日历里面对应的日期里才显示),有高手请指点一下,越改逻辑越混乱了。
现在效果如图:
代码如下:
<?php
class productDate{
var $product_id = '';
var $date = '';
var $original_date = '';
var $nextDate = '';
var $prices = '';
var $count = 42;
var $nowWeek;
var $nowMonth;
var $nextMonth;
var $startDate;
var $lastDate;
function create($product_id, $date, $prices){
$this->product_id = $product_id;
$this->original_date = $this->date = strtotime($date);
$this->prices = $prices;
$this->_init();
$datesTmp = array_keys($this->prices);
$this->lastDate = array_pop($datesTmp);
}
/**
* 下一个月
*
*/
function createNext(){
$this->nextDate = $date; //下一月, 待处理
$this->create($this->nextDate);
}
function getNextMonth($tmp_date, $step = 1){
//切割出年份
$tmp_year= date('Y', $tmp_date);
//切割出月份
$tmp_mon =date('m', $tmp_date);
$tmp_nextmonth=mktime(0,0,0,$tmp_mon + $step,1,$tmp_year);
//得到当前月的下2个月 ,最晚的日期的下一个月的开始
return $tmp_nextmonth;
}
function getPreMonth($tmp_date, $step = 1){
//切割出年份
$tmp_year= date('Y', $tmp_date);
//切割出月份
$tmp_mon =date('m', $tmp_date);
$tmp_premonth=mktime(0,0,0,$tmp_mon - $step,1,$tmp_year);
//得到当前月的上一个月
return $tmp_premonth;
}
/**
* 初始化参数
*
*/
function _init(){
$this->nowWeek = date('w', $this->date);
$this->nowMonth = date('m', $this->date);
$this->nextMonth = date('m', $this->date + 86400 * $this->count);
$this->startDate = $this->date - 86400 * $this->nowWeek;//获取开始日期
}
/**
* 获取html
*
*/
function getHtml(){
$box = $this->getBox();
while($this->getNextMonth($this->date, 2) < $this->lastDate){
$this->date = $this->getNextMonth($this->date);
$this->_init();
$box .= $this->getBox();
}
return <<<EOF
<div class="calendar_free" data-super-free="false" data-product-type="TICKET" data-sub-product-type="TICKET" data-pid="{$this->product_id}">
{$box}
</div>
EOF;
}
/**
* 获取日历块
*
*/
function getBox(){
$result = '<ul class="search_pp_calendar_d">';
$startDate = $this->startDate;
for($i = 0; $i < $this->count; $i++){
$tmp_y = date("Y-n-d", $startDate);
$tmp_m = date("m", $startDate);
$tmp_d = date("d", $startDate);
if(isset($this->prices[$startDate]) && is_array($this->prices[$startDate])){
$nowtime = date("Y-m-d",$this->prices[$startDate]['nowtime']);
$price = $this->prices[$startDate]['price'] ? '¥' . $this->prices[$startDate]['price'] : '暂无';
$nums = $this->prices[$startDate]['nums'] > 10 ? '充足' : '剩' . $this->prices[$startDate]['nums'];
} else {
$price = '';
$nums = '';
}
if($startDate >= $this->date){
$result .= '<li><div data-sellbale="true" class="search_pp_calendar_d_box month_2"><span class="search_pp_calendar_day">' . $tmp_d . '</span><span style="display:none;" class="search_pp_calendar_day_date">' . $tmp_y . '</span><span class="search_pp_calendar_balance">' . $nums . '</span><span class="search_pp_calendar_price">' . $price . '</span></div></li>';
} else {
$result .= '<li><div class="search_pp_calendar_d_box_no_hover month_1"><span class="search_pp_calendar_day">' . $tmp_d . '</span><span style="display:none;" class="search_pp_calendar_day_date">' . $tmp_y . '</span></div></li> ';
}
if(($i < $this->count - 1) && (date('w', $startDate) == 6)){
$result .= '</ul><ul class="search_pp_calendar_d">';
}
$startDate += 86400;
}
$result .= "</ul>";
$isShow = '';
if($this->original_date != $this->date){
$isShow = ' style="display:none"';
}
echo "<br />";
return <<<EOF
<div class="search_pp_calendar_box"{$isShow}>
<h2 class="search_pp_calendar_tit">出行日价格表</h2>
<div class="search_pp_calendar_m">
{$this->getHead()}
</div><!--mounth-->
<ul class="search_pp_calendar_t">
<li>星期日</li>
<li>星期一</li>
<li>星期二</li>
<li>星期三</li>
<li>星期四</li>
<li>星期五</li>
<li>星期六</li>
</ul>
{$result}
</div>
EOF;
}
/**
* 头部设置月份按钮
*
*/
function getHead(){
$result = '';
$preIcon = $this->getPreMonth($this->date) > $this->getPreMonth($this->original_date) ? "search_pp_cal_nevm_icon" : "search_pp_cal_nevm_no_icon";//开始
$nextIcon = $this->getNextMonth($this->date, 2) < $this->lastDate ? "search_pp_cal_nextm_icon" : "search_pp_cal_nextm_no_icon";//结尾
return <<<EOF
<div class="search_pp_cal_nevm">
<span class="{$preIcon}"></span>
<span class="search_pp_cal_nevm_text">{$this->nowMonth}月</span>
</div>
<div class="search_pp_cal_nextm">
<span class="search_pp_cal_nextm_text">{$this->nextMonth}月</span>
<span class="{$nextIcon}"></span>
</div>
EOF;
}
}
$date = date("Y-m-d");
$dateTmp = strtotime($date);
$price = array();//初始化价格数据
foreach($xianluttime as $k=>$v)
{
$price[$dateTmp] = array(
'nowtime' => $v[0],
'nums' => rand(2, 12),
'price' => $v[1]
);
$dateTmp = $dateTmp + 86400;
$i++;
if($i==$countday)
{
break;
}
}
$productId = $_REQUEST['productId'];
$obj = new productDate();
$obj->create($productId, $date, $price);
echo $obj->getHtml();
?> 日历 数据 box
[解决办法]
请贴全你的代码!(包括样式表)
你这样的片段无法测试