Avatar billede heavybreezer Nybegynder
19. februar 2010 - 21:46 Der er 1 kommentar

Kalender viser måned, men skal vise uge

Hej Eksperter!

Jeg har hentet et kalenderscript, som virker rigtig fint!
Koden er nem at gå til osv. Mit problem er så at jeg ikke er erfaren nok i php til at kunne gennemskue, hvordan man laver en ændring så der kun vises den pågældende uge i stedet for hele måneden. Måske kan én af jer se, hvad og hvor der skal ændres.

Mange tak :-)

Koden er her:

<?php
//Database connection details
$host = "x";
$mysql_user = "x";
$mysql_password = "x";
$mysql_db = "x";

//make connection with mysql and select the database
$mysql_connect = mysql_connect($host, $mysql_user, $mysql_password);
$db_select = mysql_select_db($mysql_db);

//check if time is set in the URL
if(isset($_GET['time']))
    $time = $_GET['time'];
else
    $time = time();

$today = date("Y/n/j", time());

$current_month = date("n", $time);

$current_month2 = date("m", $time);

$current_year = date("Y", $time);

$current_month_text = date("F Y", $time);

$total_days_of_current_month = date("t", $time);

$events = array();

//query the database for events between the first date of the month and the last date of month
$result = mysql_query("SELECT DATE_FORMAT(eventDate,'%d') AS day,eventContent,eventTitle FROM eventcal WHERE eventDate BETWEEN  '$current_year/$current_month/01' AND '$current_year/$current_month/$total_days_of_current_month'");

while($row_event = mysql_fetch_object($result))
{
    //loading the $events array with evenTitle and eventContent wrapped with <span> and <li>. We will add them inside <ul> in later part
    $events[intval($row_event->day)] .= '<li><span class="title">- '.stripslashes($row_event->eventTitle).'</span></li>';
}

$first_day_of_month = mktime(0,0,0,$current_month,1,$current_year);

//geting Numeric representation for the first day of the month. 0 (for Sunday) through 6 (for Saturday).
$first_w_of_month = date("w", $first_day_of_month);
if($first_w_of_month==0){
$first_w_of_month=7;
}


//how many rows will be in the calendar to show the dates
$total_rows = ceil(($total_days_of_current_month + $first_w_of_month)/7);

//trick to show empty cell in the first row if the month doesn't start from Sunday
if($first_w_of_month==0){
$first_w_of_month=7;
$day = -($first_w_of_month-1);
}
$next_month = mktime(0,0,0,$current_month+1,1,$current_year);
$next_month_text = date("F \'y", $next_month);

$previous_month = mktime(0,0,0,$current_month-1,1,$current_year);
$previous_month_text = date("F \'y", $previous_month);

$next_year = mktime(0,0,0,$current_month,1,$current_year+1);
$next_year_text = date("F \'y", $next_year);

$previous_year = mktime(0,0,0,$current_month,1,$current_year-1);
$previous_year_text = date("F \'y", $previous_year);
?>

<html>
<head>
<title><?=$current_month_text?></title>
<link rel="stylesheet" href="css/master.css" type="text/css" media="screen" charset="utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script src="js/coda.js" type="text/javascript"> </script>
</head>
<body>
    <h2><?=$current_month_text?></h2>
    <table cellspacing="0">
        <thead>
        <tr>
           
            <th>Mon</th>
            <th>Tue</th>
            <th>Wed</th>
            <th>Thu</th>
            <th>Fri</th>
            <th>Sat</th>
            <th>Sun</th>
        </tr>
        </thead>
        <tr>
            <?php
            for($i=0; $i< $total_rows; $i++)
            {
                for($j=0; $j<7;$j++)
                {
                    $day++;

                    if($day>0 && $day<=$total_days_of_current_month)
                    {
                        //YYYY-MM-DD date format
                        $date_form = "$current_year/$current_month/$day";

                        echo '<td';

                        //check if the date is today
                        if($date_form == $today)
                        {
                            echo ' class="today"';
                        }

                        //check if any event stored for this date in $events array
                        if(array_key_exists($day,$events))
                        {
                            //adding the date_has_event class to the <td> and close it
                            echo ' class="date_has_event"><a href="date.php?showdate='.$current_year.'-'.$current_month2.'-'.$day.'"> '.$day.'</a>';

                            //adding the eventTitle and eventContent wrapped with <span> and <li> to <ul>
                            echo '<div class="events"><ul>'.$events[$day].'</ul></div>';
                        }
                        else
                        {
                            //if there is not event on that date then just close the <td> tag
                            echo '> '.$day;
                        }

                        echo "</td>";
                    }
                    else
                    {
                        //showing empty cells in the first and last row
                        echo '<td class="padding">&nbsp;</td>';
                    }
                }
                echo "</tr><tr>";
            }

            ?>
        </tr>
        <tfoot>
            <th>
                <a href="<?=$_SERVER['PHP_SELF']?>?time=<?=$previous_year?>" title="<?=$previous_year_text?>">&laquo;&laquo;</a>
            </th>
            <th>
                <a href="<?=$_SERVER['PHP_SELF']?>?time=<?=$previous_month?>" title="<?=$previous_month_text?>">&laquo;</a>
            </th>
            <th>&nbsp;</th>
            <th>&nbsp;</th>
            <th>&nbsp;</th>
            <th>
                <a href="<?=$_SERVER['PHP_SELF']?>?time=<?=$next_month?>" title="<?=$next_month_text?>">&raquo;</a>
            </th>
            <th>
                <a href="<?=$_SERVER['PHP_SELF']?>?time=<?=$next_year?>" title="<?=$next_year_text?>">&raquo;&raquo;</a>
            </th>
        </tfoot>
    </table>
</body>
</html>
Avatar billede repox Seniormester
20. februar 2010 - 17:49 #1
Nå, inden der går for lang tid med tråden her, så kan jeg lige så godt fortælle at det nok ikke er så interessant for nogen her at omskrive dit script til at kunne noget helt andet end hvad det egentlig er tænkt til.

Et hurtigt kig på din kode giver et indtryk af at det ikke bare lige er nok at skrive et par linier og placerere dem her og der - så du må nok enten selv forsøge eller prøve at finde nogle andre færdige scripts der passer bedre med dit behov.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester