在for循环中提取PHP数组值并传递给JQuery

时间:2014-11-12 17:11:42

标签: php jquery css arrays

*编辑格式

我在网上找到了日历的PHP代码,并且我正在尝试创建一个on click事件,该事件将用户选择的日期设置为变量(稍后将在数据库查询中使用)。

截至目前我只想隔离特定日期并将其作为值返回,但到目前为止只能让它返回整个数组(1 2 3 4,5等)。有没有办法传递所选日期的特定$ list_day值?

有问题的特定循环:

for($list_day = 1; $list_day <= $days_in_month; $list_day++):
        $calendar.= '<td class="calendar-day">';
            /* add in the day number */
            $calendar.= '<div class="day-number">'.$list_day.'</div>';

完整的程序代码:

<!DOCTYPE html>
    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $("div").click(function(){
                    alert($('.calendar-day').text());
                        });
                    }); 
        </script>
    <style>
        table.calendar                      { border-left:1px solid #999; }
        tr.calendar-row                     {  }
        td.calendar-day                     { min-height:80px; font-size:11px; position:relative; } * html div.calendar-day { height:80px; }
        td.calendar-day-np                  { background:#eee; min-height:80px; } * html div.calendar-day-np { height:80px; }
        td.calendar-day-head                { background:#ccc; font-weight:bold; text-align:center; width:35px; padding:5px; border-bottom:1px solid #999; border-top:1px solid #999; border-right:1px solid #999; }
        div.day-number:hover                { background:#eceff5; }
        div.day-number                      { background:#ffffff; padding:5px; color:#999; font-size:20px; font-weight:bold; float:right; margin:-5px -5px -5px -5px; width:40px; text-align:center; }
        /* shared */
        td.calendar-day, td.calendar-day-np { width:35px; padding:5px; border-bottom:1px solid #999; border-right:1px solid #999; }
    </style>    
</head>
</html>

<?php
/* draws a calendar */
function draw_calendar($month,$year){

    /* draw table */
    $calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';

    /* table headings */
    $headings = array('Su','M','T','W','Th','F','Sa');
    $calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';

    /* days and weeks vars now ... */
           $running_day = date('w',mktime(0,0,0,$month,1,$year));
           $days_in_month = date('t',mktime(0,0,0,$month,1,$year));
           $days_in_this_week = 1;
           $day_counter = 0;
           $dates_array = array();

    /* row for week one */
    $calendar.= '<tr class="calendar-row">';

    /* print "blank" days until the first of the current week */
    for($x = 0; $x < $running_day; $x++):
        $calendar.= '<td class="calendar-day-np"></td>';
        $days_in_this_week++;
    endfor;

    /* keep going with days.... */
    for($list_day = 1; $list_day <= $days_in_month; $list_day++):
        $calendar.= '<td class="calendar-day">';
            /* add in the day number */
            $calendar.= '<div class="day-number">'.$list_day.'</div>';

            /** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !!  IF MATCHES FOUND, PRINT THEM !! **/
            $calendar.= str_repeat('<p> </p>',2);

    $calendar.= '</td>';
        if($running_day == 6):
            $calendar.= '</tr>';
            if(($day_counter+1) != $days_in_month):
                $calendar.= '<tr class="calendar-row">';
            endif;
            $running_day = -1;
            $days_in_this_week = 0;
        endif;
        $days_in_this_week++; $running_day++; $day_counter++;

    endfor;

    /* finish the rest of the days in the week */
    if($days_in_this_week < 8):
        for($x = 1; $x <= (8 - $days_in_this_week); $x++):
            $calendar.= '<div><td class="calendar-day-np"> </td></div>';
        endfor;
    endif;

    /* final row */
    $calendar.= '</tr>';

    /* end the table */
    $calendar.= '</table>';

    /* all done, return result */
    return $calendar;
}

/* sample usages */
echo '<h2>November 2014</h2>';
echo draw_calendar(11,2014);

echo date('t',mktime(0,0,0,$month,1,$year));

?>

1 个答案:

答案 0 :(得分:1)

您的问题在于您当天使用的jquery选择器。

而不是这个,它返回具有该类名的所有div元素:

$('.calendar-day').text()

使用此(字面意思):

$(this).text()

这将只返回您单击的特定div的文本。