문제

I'm creating a calendar that display the events with date that is stored on the database. The calendar displays the current month of the YEAR. I'm trying to query the events in the db, but the problem is that, the last value is the only one displaying. Also I'm not sure how to query events with the same dates. This is the syntax I made. Any idea?

$query = "SELECT cal_event, cal_date FROM calendar WHERE YEAR(cal_date) = $cYear AND MONTH(cal_date) = $cMonth";

                        $q = mysql_query($query);

                        while($rs = mysql_fetch_array($q))
                        {

                            $events = $rs['cal_event'];
                            $event_day = date("j", strtotime($rs['cal_date']));
                        }

                    for ($i=0; $i<($maxday+$startday); $i++) 
                    {

                        if(($i % 7) == 0 ) echo "<tr >";
                        if($i < $startday) echo "<td style='border:3px solid #fff;background-color:#fff'></td>";
                        else 
                            if($i <= $maxday+$startday)
                                    {
                                        echo "<td align='center' valign='middle' height='83px' style='border:3px solid #fff;background-color:#d9d9d9' ><span style='float:right;font-weight:bold'>" .($i - $startday + 1) ."</span><br><textarea name=''  maxlength='50' style='width:108px; height:50px; resize:none;'>";

                                        if($event_day == $i - $startday + 1) echo $events; 
                                        echo "</textarea></td>";   
                                    }
                        if(($i % 7) == 6 ) echo "</tr>";
                    }

UPDATE SOLUTION.

HERE IS THE SOLUTION ON MY PROBLEM

for ($i=0; $i<($maxday+$startday); $i++) 
                        {

                            $query = "SELECT * from calendar where cal_date = '".$cYear."-".$cMonth."-".($i - $startday + 1)."' ";
                            $q = mysql_query($query);
                            $rs=mysql_fetch_array($q);

                            if(($i % 7) == 0 ) echo "<tr >";
                            if($i < $startday) echo "<td style='border:3px solid #fff;background-color:#fff'></td>";
                            else 
                                if($i <= $maxday+$startday)
                                        {
                                            echo "<td align='center' valign='middle' height='83px' style='border:3px solid #fff;background-color:#d9d9d9' ><span style='float:right;font-weight:bold'>" .($i - $startday + 1) ."</span><br><textarea name=''  maxlength='50' style='width:108px; height:50px; resize:none;'>";
                                            echo $rs['cal_event']; 
                                            echo "</textarea></td>";  



                                        }
                            if(($i % 7) == 6 ) echo "</tr>";
                        }

올바른 솔루션이 없습니다

다른 팁

Apart from the requisite mention that you shouldn't use "mysql_" functions, you have (at least) one logic error in your code.

The while loop:

                    while($rs = mysql_fetch_array($q))
                    {

                        $events = $rs['cal_event'];
                        $event_day = date("j", strtotime($rs['cal_date']));
                    }

Is looping through all the results, but not doing anything with them. So, the variables $events and $event_day is getting set to the last values encountered. You should probably do something in this loop.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top