日曆價格表?

來源:互聯網
上載者:User
日曆 資料 box

問題:價格人數資訊不能對應數組裡面有的日期才顯示(比如數組裡面有2013-10-12和2013-10-21才有價格和人數,但程式會依次從開始日期顯示,而不是在產生的日曆裡面對應的日期裡才顯示),有高手請指點一下,越改邏輯越混亂了。
現在效果
代碼如下:

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 <<
product_id}">
{$box}

EOF;
}
/**
* 擷取日曆塊
*
*/
function getBox(){
$result = '
    ';
    $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 .= '
  • ' . $tmp_d . '' . $tmp_y . '' . $nums . '' . $price . '
  • ';
    } else {
    $result .= '
  • ' . $tmp_d . '' . $tmp_y . '
  • ';
    }

    if(($i < $this->count - 1) && (date('w', $startDate) == 6)){
    $result .= '
    ';
    }
    $startDate += 86400;
    }
    $result .= "
";

$isShow = '';
if($this->original_date != $this->date){
$isShow = ' style="display:none"';
}
echo "
";
return <<

出行日價格表



{$this->getHead()}



  • 星期日

  • 星期一

  • 星期二

  • 星期三

  • 星期四

  • 星期五

  • 星期六


{$result}

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 <<


{$this->nowMonth}月


{$this->nextMonth}月


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();
?>


回複討論(解決方案)

請貼全你的代碼!(包括樣式表)
你這樣的片段無法測試

請貼全你的代碼!(包括樣式表)
你這樣的片段無法測試
這是其它檔案,太長不好貼,問題是在上面貼出的裡,其它檔案是弄的ajax產品json格式,問題是這兒:
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;
}
}
這兒設定數組裡面有一個$nowtime格式是Y-m-d,設想是這個數組裡面的日期才輸出這裡面
function getBox(){
$result = '

    ';
    $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 .= '
  • ' . $tmp_d . '' . $tmp_y . '' . $nums . '' . $price . '
  • ';
    } else {
    $result .= '
  • ' . $tmp_d . '' . $tmp_y . '
  • ';
    }

    if(($i < $this->count - 1) && (date('w', $startDate) == 6)){
    $result .= '
    ';
    }
    $startDate += 86400;
    }
    $result .= "
";
如果方便可否加一下QQ

既然你只能給程式碼片段,那麼你就應該對給出的代碼做出注釋
不然神知道比都在做什麼

既然你只能給程式碼片段,那麼你就應該對給出的代碼做出注釋
不然神知道比都在做什麼
在function getBox()裡面判定當$tmp_y的值是否在
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;
}
}
數組的$nowtime裡面,如果存在執行這段:

  • ' . $tmp_d . '' . $tmp_y . '' . $nums . '' . $price . '
  • ';

    不存在執行下面這段:
  • ' . $tmp_d . '' . $tmp_y . '
  • ';

    你把需要填寫的內容數組用日期做鍵不就很簡單了嗎?

    你把需要填寫的內容數組用日期做鍵不就很簡單了嗎?
    能具體一點嗎?意思我明白了,但如何?

    for($i=1; $i<31; $i++) {  echo $i;  if(isset($data[$i])) {    //這裡輸出 $data[$i] 的內容  }}
    這個意思能明白嗎?

    for($i=1; $i<31; $i++) {  echo $i;  if(isset($data[$i])) {    //這裡輸出 $data[$i] 的內容  }}
    這個意思能明白嗎?
    thank you!大致方向了

    兩個很有才,悟性太高了
    我看代碼一頭暈

    然後老徐幾行代碼LZ就“方向”了,佩服

    兩個很有才,悟性太高了
    我看代碼一頭暈

    然後老徐幾行代碼LZ就“方向”了,佩服
    謝謝項帖

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.