事件日历 - PHP

时间:2017-10-15 19:15:21

标签: php

有人可以帮我解决这两个问题,因为我很难实现这个问题吗?

我正在使用教程的帮助构建一个包含php,javascript,html和css的事件日历。

日历将当天的日期显示为红色。预订完成后,预订当天即为海军。

问题1

当插入表单附加到日历页面时,这是成功的。但是由于代码的一些奇怪的工作,表格最终变得与压延机的设计方式相同。因此,例如,如果月份行显示为黄色且日期行显示为灰色,则由于某种原因,第一行的表单变为黄色,第二行变为灰色。从本质上讲,表格风格与压延风格有着千丝万缕的联系。我怎样才能阻止这种情况发生?

问题2

第二个查询是,为了避免上述情况,我尝试将用户重定向到单独页面上的事件表单。当事件成功添加到数据库中时,当我返回到日历页面时,预订日期不像以前那样变为海军。我需要像以前一样再次转变海军的日期,并显示预订详情,因为问题1成功了。

正如你所知,我真的被困在这里,需要一些帮助(我真的很想实现第二个问题/场景),但第一个对我来说也没问题。请在下面找到每个问题的代码。非常感谢您提供的任何帮助。

问题1

calender.php

 <?php
//These are required to connect to the database
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', '');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'calender');

//This variable is used for displaying error
$error="Cannot connect";

//Connect to the database
$dbconnection=mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or     die($error);
mysqli_select_db($dbconnection,"calender") or die($error);
?>

<html> 
<head> 
<script> 
//This function represents the previous button on the calender
function goPreviousMonth(month, year){ 
if (month == 1) { 
--year; 
month = 13; 
} 
--month
var monthstring=""+month+"";
var monthlength=monthstring.length;
if(monthlength<=1){
monthstring="0"+monthstring;
}
//This creates the URL to state the month and year.
document.location.href = "<?php     $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year; 
} 

//This function represents the next button on the calender
function goNextMonth(month, year){ 
if (month == 12){ 
++year; 
month = 0; 
} 
++month
var monthstring=""+month+"";
var monthlength=monthstring.length;
if(monthlength<=1){
    monthstring="0"+monthstring;
}
//This creates the URL to state the month and year.
document.location.href = "<?php     $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year; 
} 
</script> 
</head> 

<link rel="stylesheet" type="text/css" href="calenderfakestyle.css">


<body> 

<?php 
//Check the URL to see if the user has passed a variable

//This is used to check if day has a passing variable 
if (isset ($_GET['day'])){ 
//If true, then get the day from the URL
$day = $_GET['day']; 
}else{ 
$day = date ("d");
} 

//The is used to check if month has a passing variable
if (isset ($_GET['month'])){ 
//If true, then get the month from the URL
 $month = $_GET['month']; 
 }else{ 
 $month = date ("n"); 
 } 

//The is used to check if month has a passing variable
if (isset ($_GET['year'])){ 
//If true, then get the year from the URL
 $year = $_GET['year']; 
 }else{ 
 $year = date ("Y"); 
 } 

 //The calender variables

 //This stores day, month and year variables within a timestamp
 $currentTimeStamp = strtotime("$year-$month-$day"); 

 //This gets the current month name 
 $monthName = date("F", $currentTimeStamp); 

 //This determines how many days there are in the current month 
 $numDays = date("t", $currentTimeStamp); 

 //This variable is used to count cells in the loop later
 $counter = 0; 
 ?> 

 <?php
 //This code must be below the date variable
 if(isset($_GET['add'])){
     $title=$_POST['txttitle'];
     $detail=$_POST['txtdetail'];

     $eventdate=$month."/".$day."/".$year;

     $sqlinsert="insert into booking (title,detail,event_date,date_added)     values ('".$title."','".$detail."','".$eventdate."',now())";
     $resultinsert=mysqli_query($dbconnection,$sqlinsert);
     if($resultinsert){
         echo "Event was successfully added";
     }else{
         echo "Event was not added";
     }
 }


 ?>
 <table border='1'> 
    <tr> <td> <input class="previousbutton" type='button' value='<'     name='previousbutton' onClick="goPreviousMonth (<?php echo     $month.",".$year?>)"> </td> 
         <td colspan='5'> <span class="title"> <?php echo $monthName."     ".$year; ?> </span> </td> 
         <td> <input class="nextbutton"  type='button' value='>'     name='nextbutton' onClick="goNextMonth (<?php echo $month.",".$year?>)">     </td> 
    </tr> 

 <tr> 
    <td>Sun</td> 
    <td>Mon</td> 
    <td>Tue</td> 
    <td>Wed</td> 
    <td>Thu</td> 
    <td>Fri</td> 
    <td>Sat</td> 
 </tr> <?php echo "<tr>"; 

 //This is used to loop from 1 to the number of days in the month
 for ($i = 1; $i < $numDays+1; $i++, $counter++){

 //This is a timestamp for each day in the loop
 $timeStamp = strtotime ("$year-$month-$i"); 

//This checks if if it is the first day
if($i == 1){ 
    //This determines which day for the first date of the month
    $firstDay = date ("w", $timeStamp);
    //This loop is used to make a blank cell if it is not the first day
    for ($j = 0; $j < $firstDay; $j++, $counter++){ 
    //Blank space 
    echo "<td>&nbsp;</td>"; 
     } 
   } 
    //This checks to see if the day is on the last column. If so, a new     row will be made.
    if($counter % 7 == 0 ){     
    echo "<tr></tr>"; 
 } 
$monthstring=$month;
$monthlength=strlen($monthstring);
$daystring=$i;
$daylength=strlen($daystring);
if($monthlength<=1){
$monthstring="0".$monthstring;  
}
if($daylength<=1){
    $daystring="0".$daystring;
}
$todaysDate=date("m/d/Y");
$dateToCompare=$monthstring. '/' . $daystring . '/' . $year;
echo "<td align='center' ";
if($todaysDate==$dateToCompare){
echo "class='today'";   
}else{
    $sqlCount="select * from booking where     event_date='".$dateToCompare."'";
    $noOfEvent= mysqli_num_rows(mysqli_query($dbconnection,$sqlCount));
    if($noOfEvent>=1){
        echo "class='event'";
    }
}
echo "><a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&    day=".$daystring."&year=".$year."&v=true'>".$i."</a></td>";
 } 
 echo "</tr>"; 
 ?> 
 </table> 

 <?php
 if(isset($_GET['v'])){
     echo "<a href='".$_SERVER['PHP_SELF']."?month=".$month."&    day=".$day."&year=".$year."&v=true&f=true'>Add Event</a>";
     if(isset($_GET['f'])){
         include("eventform.php");
     }
     $sqlEvent="select * from booking where     event_date='".$month."/".$day."/".$year."'";
     $resultEvents=mysqli_query($dbconnection,$sqlEvent);
     echo "<hr>";
     while($events=mysqli_fetch_array($resultEvents)){
     echo "Title : ".$events['title']."<br>"; 
     echo "Detail : ".$events['detail']."<br>";
     }
 }
 ?>

 </body> 
 </html>

eventform.php

<form name='eventform' method='POST' action="<?php $_SERVER['PHP_SELF'];     ?>?month=<?php echo $month;?>&day=<?php echo $day;?>&year=<?php echo     $year;?>&v=true&add=true">

<table width='400px'>
    <tr>
        <td width='150px'>Title</td>
        <td width='250px'><input type='text' name='txttitle'> </td>
    </tr>

    <tr>
        <td width='150px'>Detail</td>
        <td width='250px'> <textarea name='txtdetail'> </textarea> </td>
    </tr>

<tr>
    <td td colspan='2'align='center'> <input type='submit' name='btnadd'     value='Add Event'> </td>
</tr>
</table>

</form>

问题2

calender.php

         <?php
        //These are required to connect to the database
        DEFINE ('DB_USER', 'root');
        DEFINE ('DB_PASSWORD', '');
        DEFINE ('DB_HOST', 'localhost');
        DEFINE ('DB_NAME', 'calender');

    //This variable is used for displaying error
    $error="Cannot connect";

//Connect to the database
$dbconnection=mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or     die($error);
mysqli_select_db($dbconnection,"calender") or die($error);
ob_start();
?>

<html> 
<head> 
<script> 
//This function represents the previous button on the calender
function goPreviousMonth(month, year){ 
if (month == 1) { 
--year; 
month = 13; 
} 
--month
var monthstring=""+month+"";
var monthlength=monthstring.length;
if(monthlength<=1){
    monthstring="0"+monthstring;
}
//This creates the URL to state the month and year.
document.location.href = "<?php     $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year; 
} 

//This function represents the next button on the calender
function goNextMonth(month, year){ 
if (month == 12){ 
++year; 
month = 0; 
} 
++month
var monthstring=""+month+"";
var monthlength=monthstring.length;
if(monthlength<=1){
    monthstring="0"+monthstring;
}
//This creates the URL to state the month and year.
document.location.href = "<?php     $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year; 
} 
</script> 
</head> 

<link rel="stylesheet" type="text/css" href="calenderfakestyle.css">


<body> 

<?php 
//Check the URL to see if the user has passed a variable

//This is used to check if day has a passing variable 
if (isset ($_GET['day'])){ 
//If true, then get the day from the URL
$day = $_GET['day']; 
}else{ 
$day = date ("d");
} 

//The is used to check if month has a passing variable
if (isset ($_GET['month'])){ 
//If true, then get the month from the URL
 $month = $_GET['month']; 
 }else{ 
 $month = date ("n"); 
 } 

//The is used to check if month has a passing variable
if (isset ($_GET['year'])){ 
//If true, then get the year from the URL
 $year = $_GET['year']; 
 }else{ 
 $year = date ("Y"); 
 } 

 //The calender variables

 //This stores day, month and year variables within a timestamp
 $currentTimeStamp = strtotime("$year-$month-$day"); 

 //This gets the current month name 
 $monthName = date("F", $currentTimeStamp); 

 //This determines how many days there are in the current month 
 $numDays = date("t", $currentTimeStamp); 

 //This variable is used to count cells in the loop later
 $counter = 0; 
 ?> 

 <?php
 //This code must be below the date variable
 if(isset($_GET['add'])){
     $title=$_POST['txttitle'];
     $detail=$_POST['txtdetail'];

     $eventdate=$month."/".$day."/".$year;

     $sqlinsert="insert into booking (title,detail,event_date,date_added)     values ('".$title."','".$detail."','".$eventdate."',now())";
     $resultinsert=mysqli_query($dbconnection,$sqlinsert);
     if($resultinsert){
         echo "Event was successfully added";
     }else{
         echo "Event was not added";
     }
 }


 ?>
 <table border='1'> 
    <tr> <td> <input class="previousbutton" type='button' value='<'     name='previousbutton' onClick="goPreviousMonth (<?php echo     $month.",".$year?>)"> </td> 
         <td colspan='5'> <span class="title"> <?php echo $monthName."     ".$year; ?> </span> </td> 
         <td> <input class="nextbutton"  type='button' value='>'     name='nextbutton' onClick="goNextMonth (<?php echo $month.",".$year?>)">     </td> 
    </tr> 

 <tr> 
    <td>Sun</td> 
    <td>Mon</td> 
    <td>Tue</td> 
    <td>Wed</td> 
    <td>Thu</td> 
    <td>Fri</td> 
    <td>Sat</td> 
 </tr> <?php echo "<tr>"; 

 //This is used to loop from 1 to the number of days in the month
 for ($i = 1; $i < $numDays+1; $i++, $counter++){

 //This is a timestamp for each day in the loop
 $timeStamp = strtotime ("$year-$month-$i"); 

//This checks if if it is the first day
if($i == 1){ 
    //This determines which day for the first date of the month
    $firstDay = date ("w", $timeStamp);
    //This loop is used to make a blank cell if it is not the first day
    for ($j = 0; $j < $firstDay; $j++, $counter++){ 
    //Blank space 
    echo "<td>&nbsp;</td>"; 
    } 
   } 
    //This checks to see if the day is on the last column. If so, a new     row will be made.
    if($counter % 7 == 0 ){     
    echo "<tr></tr>"; 
 } 
$monthstring=$month;
$monthlength=strlen($monthstring);
$daystring=$i;
$daylength=strlen($daystring);
if($monthlength<=1){
$monthstring="0".$monthstring;  
}
if($daylength<=1){
    $daystring="0".$daystring;
}
$todaysDate=date("m/d/Y");
$dateToCompare=$monthstring. '/' . $daystring . '/' . $year;
echo "<td align='center' ";
if($todaysDate==$dateToCompare){
echo "class='today'";   
}else{
    $sqlCount="select * from booking where     event_date='".$dateToCompare."'";
    $noOfEvent= mysqli_num_rows(mysqli_query($dbconnection,$sqlCount));
    if($noOfEvent>=1){
        echo "class='event'";
    }
}
echo "><a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&    day=".$daystring."&year=".$year."&v=true'>".$i."</a></td>";
 } 
 echo "</tr>"; 
 ?> 
 </table> 

 <?php
 if(isset($_GET['v'])){
        header("Location:eventform.php");
     if(isset($_GET['f'])){
         include("eventform.php");
     }
     $sqlEvent="select * from booking where     event_date='".$month."/".$day."/".$year."'";
     $resultEvents=mysqli_query($dbconnection,$sqlEvent);
     echo "<hr>";
     while($events=mysqli_fetch_array($resultEvents)){
     echo "Title : ".$events['title']."<br>"; 
     echo "Detail : ".$events['detail']."<br>";
     }
 }
 ?>




 </body> 
 </html>

Eventform.php

<?php


DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', '');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'calender');

//This variable is used for displaying error
$error="Cannot connect";

//Connect to the database
$dbconnection=mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or     die($error);
mysqli_select_db($dbconnection,"calender") or die($error);

//Check the URL to see if the user has passed a variable

//This is used to check if day has a passing variable 
if (isset ($_GET['day'])){ 
//If true, then get the day from the URL
$day = $_GET['day']; 
}else{ 
$day = date ("d");
} 

//The is used to check if month has a passing variable
if (isset ($_GET['month'])){ 
//If true, then get the month from the URL
 $month = $_GET['month']; 
 }else{ 
 $month = date ("n"); 
 } 

//The is used to check if month has a passing variable
if (isset ($_GET['year'])){ 
//If true, then get the year from the URL
 $year = $_GET['year']; 
 }else{ 
 $year = date ("Y"); 
 } 

 //The calender variables

 //This stores day, month and year variables within a timestamp
 $currentTimeStamp = strtotime("$year-$month-$day"); 

 //This gets the current month name 
 $monthName = date("F", $currentTimeStamp); 

 //This determines how many days there are in the current month 
 $numDays = date("t", $currentTimeStamp); 

 //This variable is used to count cells in the loop later
 $counter = 0; 


if(isset($_GET['add'])){
     $title=$_POST['txttitle'];
     $detail=$_POST['txtdetail'];

     $eventdate=$month."/".$day."/".$year;

     $sqlinsert="insert into booking (title,detail,event_date,date_added)     values ('".$title."','".$detail."','".$eventdate."',now())";
     $resultinsert=mysqli_query($dbconnection,$sqlinsert);
     if($resultinsert){
         echo "Event was successfully added";
     }else{
         echo "Event was not added";
      }
 }

?>













<form name='eventform' method='POST' action="<?php $_SERVER['PHP_SELF'];     ?>?month=<?php echo $month;?>&day=<?php echo $day;?>&year=<?php echo     $year;?>&v=true&add=true">

<table width='400px'>
    <tr>
        <td width='150px'>Title</td>
        <td width='250px'><input type='text' name='txttitle'> </td>
    </tr>

    <tr>
        <td width='150px'>Detail</td>
        <td width='250px'> <textarea name='txtdetail'> </textarea> </td>
    </tr>

<tr>
    <td td colspan='2'align='center'> <input type='submit' name='btnadd'     value='Add Event'> </td>
</tr>
</table>

</form>

两个问题的CSS样式表:

table {
position: absolute;
width: 700px;
left: 50%;
margin-left: -350px;
margin-top:-30px;
text-align: center; 
border-collapse: collapse;
font-size: 20px;
}

table tr td a {
text-decoration: none;
display: block;
width:100%;
padding: 20% 0;
}

td {
width: 100px;
height: 60px;
background-color: white;
}

a:link {
    color: black;
}

a:visited {
    color: black;
}

td:hover {
  background-color: purple;
}

.previousbutton{
width: 100px;
height: 60px;
border: none;
background-color: blue;
cursor: pointer;
font-size:20px;
}

.previousbutton:hover{
background-color: #blue;
}

.nextbutton{
width: 100px;
height: 60px;
border: none;
background-color: blue;
cursor: pointer;
font-size:20px;
}

.nextbutton:hover{
background-color: #7FFFD4;
}

.today {
    background-color: red;
}

.event {
    background-color: navy;
}

tr:nth-child(1) td:nth-child(2){
    background-color: yellow;
}

tr:nth-child(2) td:nth-child(1n){
    background-color: #D3D3D3;
}

.title {
    color:black;    
}

该数据库包含以下字段:

ID
title
detail
event_date
date_added

1 个答案:

答案 0 :(得分:0)

问题1:您的CSS过于通用,因此所有与表相关的元素都以相同的样式结束。

实施例。你定义

tr:nth-child(1) td:nth-child(2){
    background-color: yellow;
}

tr:nth-child(2) td:nth-child(1n){
    background-color: #D3D3D3;
}

适用于所有表格中的所有tr元素

如果您只想将它​​应用于日历表,您可以在日历(ex caltr)中为tr添加一个类名,并修改您的css以指定tr.caltr。然后表单中的tr将不适用这种风格。

你将这种机制用于.today。只有td类=&#34;今天&#34;最终会变成红色,所以对你的tr元素应用相同的东西。

问题2:一旦你解决问题1,是否还需要问题2?好像不是。

示例:

的index.html

<html>
    <head>
        <title>TEST</title>
        <link rel="stylesheet" type="text/css" href="test2.css">
    </head>
    <body>
        <h1>Table1</h1>
        <table>
            <thead>
                <tr class="table1"><th>Cell1</th><th>Cell2</th></tr>
            </thead>
            <tbody>
                <tr class="table1"><td>value1-1</td><td>Value1-2</td></tr>
                <tr class="table1"><td>value2-1</td><td>Value2-2</td></tr>
                <tr class="table1"><td>value3-1</td><td>Value3-2</td></tr>
            </tbody>
        </table>
        <h1>Table2</h1>
        <table>
            <thead>
                <tr><th>Cell1</th><th>Cell2</th></tr>
            </thead>
            <tbody>
                <tr><td>value1-1</td><td>Value1-2</td></tr>
                <tr><td>value2-1</td><td>Value2-2</td></tr>
                <tr><td>value3-1</td><td>Value3-2</td></tr>
            </tbody>
        </table>
    </body>
</html>

test2.css

/* TEST */
tr.table1:nth-child(1) td:nth-child(2){
    background-color: yellow;
}

tr.table1:nth-child(2) td:nth-child(1n){
    background-color: #D3D3D3;
}

使用此功能,您将获得:

enter image description here

看两个表中的tr是如何分开的?你使用class =&#34;&#34;属性来区分两者。您必须编辑HTML和CSS才能匹配。