Wednesday, March 5, 2014
PHP Webcam Image Capture
The following script is to capture the image from webcam and save that image.
index.php
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<div id="camera">
<video autoplay id="vid" style="display:;" width="240">
</video>
<canvas id="canvas" height="380" style="border:1px solid #d3d3d3;"></canvas><br>
<button onclick="snapshot()">Take Picture</button>
<input type="button" value="Save" id="savepic" />
</div>
<div id="mypic" width="240"></div>
</body>
<script type="text/javascript">
var pid="<?php echo uniqid();?>";
var video = document.querySelector("#vid");
var canvas = document.querySelector('#canvas');
var ctx = canvas.getContext('2d');
var localMediaStream = null;
var onCameraFail = function (e) {
console.log('Camera did not work.', e);
};
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
window.URL = window.URL || window.webkitURL;
navigator.getUserMedia({video:true}, function (stream) {
video.src = window.URL.createObjectURL(stream);
localMediaStream = stream;
}, onCameraFail);
function snapshot() {
var video =document.getElementById("vid");
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext('2d');
var localMediaStream = null;
ctx.drawImage(video, -60, -40);
}
document.getElementById("savepic").onclick=function(){
var canvas1 = document.getElementById("canvas");
if (canvas1.getContext) {
var ctx = canvas1.getContext("2d");
var myImage = canvas1.toDataURL("image/png").replace("image/png", "image/octet-stream");
}
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest;
}else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("mypic").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","upload_photo.php",true);
xmlhttp.setRequestHeader('Content-Type', 'canvas/upload');
xmlhttp.send("canvasData="+myImage+"&pid="+pid);
document.getElementById('camera').style.display="none";
alert("Photo saved");
}
</script>
</html>
Tuesday, March 4, 2014
PHP Live Search
This is the example how to create live search where you get the search result while you type.
index.php
<html>
<head>
<script type="text/javascript">
function Search(src){
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest;
}else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("result").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","search.php?src="+src,true);
xmlhttp.send();
}
</script>
</head
<body>
<input type="text" id="search" onkeyup="Search(this.value)" />
<div id="result"></div>
</body>
</html>
search.php
<?php
$src = (isset($_GET['srch'])) ? $_GET['srch'] : "";
$query=mysql_query("SELECT field FROM table WHERE field LIKE '%".$src."%'");
while($result=mysql_fetch_assoc($query)){
echo $result['field'];
}
?>
index.php
<html>
<head>
<script type="text/javascript">
function Search(src){
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest;
}else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("result").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","search.php?src="+src,true);
xmlhttp.send();
}
</script>
</head
<body>
<input type="text" id="search" onkeyup="Search(this.value)" />
<div id="result"></div>
</body>
</html>
search.php
<?php
$src = (isset($_GET['srch'])) ? $_GET['srch'] : "";
$query=mysql_query("SELECT field FROM table WHERE field LIKE '%".$src."%'");
while($result=mysql_fetch_assoc($query)){
echo $result['field'];
}
?>
Monday, March 3, 2014
PHP Simple Bar Chart
A simple bar chart using php
index.php
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
//----------class----------
class graph{
public function setHeight($h = 300){
return $h;
}
public function setWidth($w = 500){
return $w;
}
public function setBar($h = 150, $w = 30){
return $h.','.$w;
}
}
//-------------------------
$graph = new graph;
$width = $graph->setWidth();
$height = $graph->setHeight();
echo '<div id="graph" style="width:'.$width.'px;height:'.$height.'px">';
$data['A'] = 150;
$data['B'] = 20;
$data['C'] = 50;
$data['D'] = 25;
$data['E'] = 98;
$data['F'] = 167;
$i=0;
foreach($data as $label => $value){
$i++;
$barsize = explode(',',$graph->setBar($value,40));
$mtop = ($height - $barsize[0]) - 1;
$mleft = 6+($barsize[1]+8)*$i;
echo '<div id="bar" style="height:'.$barsize[0].'px;width:'.$barsize[1].'px;margin:'.$mtop.'px 0 0 '.$mleft.'px;" onMouseOver=getBarLabel('.$i.',1)
onMouseOut=getBarLabel('.$i.',0)>'.$value.'</div>
<div id="label'.$i.'" style="margin:'.($mtop+10).'px 0 0 '.($mleft-10).'px;background:#FFF;display:none;width:100px;padding:0 0 0 5px;z-index:1">'.$label.'</div>';
}
echo '</div>';
?>
</body>
<script type="text/javascript">
function getBarLabel(i,e){
var label = document.getElementById("label"+i);
if(label){
if(e==1)
label.style.display="";
else
label.style.display="none";
}
}
</script>
</html>
style.css
#graph {
border-bottom-width: 1px;
border-left-width: 1px;
border-bottom-style: solid;
border-left-style: solid;
border-bottom-color: #060;
border-left-color: #090;
vertical-align: bottom;
margin-top: 50px;
margin-left: 50px;
}
#graph div {
border: 1px solid #039;
position: absolute;
background-color: #36F;
}
PHP Pagination
Paging button designed to be included into web pages.
First, create a file "index.php", copy the code below.
First query to get total rows of data
$query=mysql_query("SELECT * FROM table");
$rows = mysql_num_rows($query);
Set the number of rows to be displayed in every pages
$max = 10;
$totalpage = ceil($rows/$max);
$query2=mysql_query("SELECT * FROM table ".$limit."");
while($result = mysql_fetch_assoc($query2)){
echo $result['column'];
}
So, in the index.php,
index.php
First, create a file "index.php", copy the code below.
First query to get total rows of data
$query=mysql_query("SELECT * FROM table");
$rows = mysql_num_rows($query);
Set the number of rows to be displayed in every pages
$max = 10;
$totalpage = ceil($rows/$max);
$currentPage = (isset($_GET['page'])) ? $_GET['page'] : 1;
$limit = "LIMIT ".($currentPage-1)*$max.",".$max;
Second query to display data$limit = "LIMIT ".($currentPage-1)*$max.",".$max;
$query2=mysql_query("SELECT * FROM table ".$limit."");
while($result = mysql_fetch_assoc($query2)){
echo $result['column'];
}
So, in the index.php,
index.php
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
$query=mysql_query("SELECT * FROM table");
$rows = mysql_num_rows($query);
$max = 10;
$totalpage = ceil($rows/$max);
$currentPage = (isset($_GET['page'])) ? $_GET['page'] : 1;
$limit = "LIMIT ".($currentPage-1)*$max.",".$max;
$query2=mysql_query("SELECT * FROM table ".$limit."");
while($result = mysql_fetch_assoc($query2)){
echo $result['column'];
}
$query=mysql_query("SELECT * FROM table");
$rows = mysql_num_rows($query);
$max = 10;
$totalpage = ceil($rows/$max);
$currentPage = (isset($_GET['page'])) ? $_GET['page'] : 1;
$limit = "LIMIT ".($currentPage-1)*$max.",".$max;
$query2=mysql_query("SELECT * FROM table ".$limit."");
while($result = mysql_fetch_assoc($query2)){
echo $result['column'];
}
include 'pagenum.php';
$page = "index.php";
echo getPageNum($totalpage,$currentPage,$page);
?>
</body>
PHP Pagination (cont)
Create a file "pagenum.php" and copy the code below
pagenum.php
<?php
function getPageNum($totalPage = 50, $currentPage = 1,$page = "index.php"){
$totalNum = 5; // minimum is 2
$stx = 1;
$enx = ($totalNum < 2) ? 3 : ($totalNum + 1);
for($t=0;$t<$totalPage;$t++){
$n = ($totalNum*$t)+1;
$start = ($n == 1) ? $n : $n-$t;
$end = $start+$totalNum;
if($start <= $totalPage){
if($currentPage > $start && $currentPage < $end){
$stx = $start;
$enx = $end;
}
}
}
$prev = $currentPage-1;
$pclass = "button";
$plink = '<a href="'.$page.'&page=1"><<</a>';
$plinkx = '<a href="'.$page.'&page='.$prev.'"><</a>';
if($currentPage <= 1){
$pclass = "ndis";
$plink = "<span><<</span>";
$plinkx = "<span><</span>";
}
$result = '<div id="'.$pclass.'">'.$plink.'</div>';
$result .= '<div id="'.$pclass.'">'.$plinkx.'</div>';
for($i=$stx;$i<=$enx;$i++){
$num = ($i <= $totalPage) ? $i : "";
if($num != ""){
$class = ($num == $currentPage) ? "pnums" : "pnum";
$result .= '<div id="'.$class.'">
<a href="'.$page.'&page='.$num.'">'.$num.'</a>
</div>';
}
}
if($enx < $totalPage){
$result .= '<div id="dots">.......</div>';
$result .= '<div id="pnum"><a href="'.$page.'&page='.$totalPage.'">'.$totalPage.'</a></div>';
}
$next = $currentPage+1;
$nclass = "button";
$nlink = '<a href="'.$page.'&page='.$next.'">></a>';
$nlinkx = '<a href="'.$page.'&page='.$totalPage.'">>></a>';
if($currentPage >= $totalPage){
$nclass = "ndis";
$nlink = "<span>></span>";
$nlinkx = "<span>>></span>";
}
$result .= '<div id="'.$nclass.'">'.$nlink.'</div>';
$result .= '<div id="'.$nclass.'">'.$nlinkx.'</div>';
return $result;
} ?>
Click link below to continue.
style.css
pagenum.php
<?php
function getPageNum($totalPage = 50, $currentPage = 1,$page = "index.php"){
$totalNum = 5; // minimum is 2
$stx = 1;
$enx = ($totalNum < 2) ? 3 : ($totalNum + 1);
for($t=0;$t<$totalPage;$t++){
$n = ($totalNum*$t)+1;
$start = ($n == 1) ? $n : $n-$t;
$end = $start+$totalNum;
if($start <= $totalPage){
if($currentPage > $start && $currentPage < $end){
$stx = $start;
$enx = $end;
}
}
}
$prev = $currentPage-1;
$pclass = "button";
$plink = '<a href="'.$page.'&page=1"><<</a>';
$plinkx = '<a href="'.$page.'&page='.$prev.'"><</a>';
if($currentPage <= 1){
$pclass = "ndis";
$plink = "<span><<</span>";
$plinkx = "<span><</span>";
}
$result = '<div id="'.$pclass.'">'.$plink.'</div>';
$result .= '<div id="'.$pclass.'">'.$plinkx.'</div>';
for($i=$stx;$i<=$enx;$i++){
$num = ($i <= $totalPage) ? $i : "";
if($num != ""){
$class = ($num == $currentPage) ? "pnums" : "pnum";
$result .= '<div id="'.$class.'">
<a href="'.$page.'&page='.$num.'">'.$num.'</a>
</div>';
}
}
if($enx < $totalPage){
$result .= '<div id="dots">.......</div>';
$result .= '<div id="pnum"><a href="'.$page.'&page='.$totalPage.'">'.$totalPage.'</a></div>';
}
$next = $currentPage+1;
$nclass = "button";
$nlink = '<a href="'.$page.'&page='.$next.'">></a>';
$nlinkx = '<a href="'.$page.'&page='.$totalPage.'">>></a>';
if($currentPage >= $totalPage){
$nclass = "ndis";
$nlink = "<span>></span>";
$nlinkx = "<span>>></span>";
}
$result .= '<div id="'.$nclass.'">'.$nlink.'</div>';
$result .= '<div id="'.$nclass.'">'.$nlinkx.'</div>';
return $result;
} ?>
Click link below to continue.
style.css
PHP Pagination (cont)
Create a file "style.css" to apply to index.php and copy the code below.
style.css
#pnum {
width: 30px;
border: 1px solid #000;
float: left;
margin-left: 2px;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #900;
background-color: #CCC;
text-align: center;
padding-top: 7px;
padding-bottom: 7px;
}
#pnum a{
text-decoration: none;
}
#pnums {
width: 30px;
border: 1px solid #000;
float: left;
margin-left: 2px;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #900;
background-color: #484848;
text-align: center;
padding-top: 7px;
padding-bottom: 7px;
}
#pnums a{
text-decoration: none;
font-weight: bold;
color: #FFF;
}
#pnum .snum{
text-decoration: none;
font-weight: bold;
}
#dots {
font-family: Arial, Helvetica, sans-serif;
font-size: 20px;
color: #900;
float: left;
padding-top: 10px;
margin-right: 8px;
margin-left: 8px;
}
#button {
width: 40px;
border: 1px solid #000;
float: left;
padding-top: 7px;
padding-bottom: 7px;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
text-align: center;
background-color: #484848;
margin-left: 2px;
}
#button a {
text-decoration: none;
font-weight: bold;
color: #FFF;
}
#ndis {
width: 40px;
border: 1px solid #666;
float: left;
padding-top: 7px;
padding-bottom: 7px;
font-size: 12px;
text-align: center;
background-color: #CCC;
margin-left: 2px;
color: #999;
}
#ndis span {
font-weight: bold;
color: #999;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px; }
style.css
#pnum {
width: 30px;
border: 1px solid #000;
float: left;
margin-left: 2px;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #900;
background-color: #CCC;
text-align: center;
padding-top: 7px;
padding-bottom: 7px;
}
#pnum a{
text-decoration: none;
}
#pnums {
width: 30px;
border: 1px solid #000;
float: left;
margin-left: 2px;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
color: #900;
background-color: #484848;
text-align: center;
padding-top: 7px;
padding-bottom: 7px;
}
#pnums a{
text-decoration: none;
font-weight: bold;
color: #FFF;
}
#pnum .snum{
text-decoration: none;
font-weight: bold;
}
#dots {
font-family: Arial, Helvetica, sans-serif;
font-size: 20px;
color: #900;
float: left;
padding-top: 10px;
margin-right: 8px;
margin-left: 8px;
}
#button {
width: 40px;
border: 1px solid #000;
float: left;
padding-top: 7px;
padding-bottom: 7px;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
text-align: center;
background-color: #484848;
margin-left: 2px;
}
#button a {
text-decoration: none;
font-weight: bold;
color: #FFF;
}
#ndis {
width: 40px;
border: 1px solid #666;
float: left;
padding-top: 7px;
padding-bottom: 7px;
font-size: 12px;
text-align: center;
background-color: #CCC;
margin-left: 2px;
color: #999;
}
#ndis span {
font-weight: bold;
color: #999;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px; }
PHP Event Calendar
A calendar designed to create events.
Create a database "calendar".
Create table "event":
CREATE TABLE `event` (
`id` int(2) NOT NULL AUTO_INCREMENT,
`event` varchar(50) DEFAULT NULL,
`dt_start` datetime DEFAULT NULL,
`dt_end` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
Click links below to continue
index.php
data.php
css.php
Create a database "calendar".
Create table "event":
CREATE TABLE `event` (
`id` int(2) NOT NULL AUTO_INCREMENT,
`event` varchar(50) DEFAULT NULL,
`dt_start` datetime DEFAULT NULL,
`dt_end` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
Click links below to continue
index.php
data.php
css.php
PHP Event Calendar (cont)
index.php
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>My Calendar</title>
<?php
</head>
<body>
<?php
include("css.php");
$k = (isset($_GET['x'])) ? explode('-',$_GET['x']) : "";
$month = (isset($_GET['x'])) ? $k[0] : date('m') + 1;
$year = (isset($_GET['x'])) ? $k[1] : date('Y');
if($month == 14){ $year++ ; $month = 2;};
if($month == 1){ $year-- ; $month = 13;};
$mar_l = $width + $space;
$daysname = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
echo '<div class="ntfmain">';
echo '<div class="prev" id="prev" onmouseover=crtHover(this,"prevhover"); onmouseout=crtHover(this,"prev"); onclick=goDate("'.($month - 1).'-'.$year.'");>'
.'<'
.'</div>'
.'<div class="ntfdate">';
$ntfdate = "01"."-".($month - 1)."-".$year." "."00:00:00";
echo date('F Y',strtotime($ntfdate))
.'</div>';
echo '<div class="next" onmouseover=crtHover(this,"nexthover"); onmouseout=crtHover(this,"next"); onclick=goDate("'.($month + 1).'-'.$year.'");>'
.'>'
.'</div>'
.'</div>';
echo '<div class="mainbox">';
for($d = 0; $d < count($daysname); $d++){
$m_l = $mar_l * $d;
echo '<div class="daysname" style="margin-left:'.$m_l.'px;">'.$daysname[$d].'</div>';
}
//----------------------------------------------DIV STYLE------------------------------------------------
include("data.php");
if(isset($_POST['save'])){
$val = array();
$value = (isset($_POST['field'])) ? $_POST['field'] : "";
foreach($value as $key => $v){
$val[] = "'".ucwords($v)."'";
$valx = implode(",",$val);
}
saveData($valx);
}
$event = getData(); // get data
$count_boxes = 0;
$days_so_far = 0;
$strmon = array('01'=>'Januari','02'=>'Febuari','03'=>'Mac','04'=>'April','05'=>'Mei','06'=>'Jun','07'=>'Julai','08'=>'Ogos','09'=>'September','10'=>'Oktober','11'=>'November','12'=>'Disember');
echo '<div class="box" id="theBox">';
$days_in_month = date("t", mktime(0,0,0,$month,0,$year));
$first_day_of_month = date ("w", mktime(0,0,0,$month-1,1,$year));
$first_day_of_month = $first_day_of_month + 1;
$x = 0;
$num = array();
for ($i = $first_day_of_month-1; $i < 7; $i++) {
$days_so_far = $days_so_far + 1;
$count_boxes = $count_boxes + 1;
$m_l = $mar_l * $i;
$x = $x + 1;
$x = (strlen($x) == 1) ? '0'.($x) : $x;
$mon = (strlen($month - 1) == 1) ? '0'.($month - 1) : ($month - 1);
$xtodate = $x.$mon.$year;
$class = ($xtodate == date('dmY')) ? "todaybox" : "daysbox";
echo '<div class="'.$class.'" style="margin-left:'.$m_l.'px;">'
.'<span style="color:#fff">'.$x.'</span>';
foreach($event as $edate => $ev){
$z = 14;
for($e = 0; $e < count($ev); $e++){
$id = ($edate == $xtodate) ? $x.$e : 0;
$num[] = $id;
$zx = $z * ($e);
$prtevent = ($edate == $xtodate) ? $ev[$e] : "";
$popDivVal = ($edate == $xtodate) ? str_replace(' ',',',$prtevent).".".$x.",".$strmon[$mon].",".$year : "";
echo '<div class="event" id="prtevent" onmouseover=popup("'.($m_l - 8).'","'.((70) + $zx).'","'.$edate.$e.'","1"); onmouseout=popup("0","0","'.$edate.$e.'","0")>'
.$prtevent
.'</div>';
}
}
echo '<div class="newevent" onmousedown=newEvent(this,"1","'.$xtodate.'"); onmouseup=newEvent(this,"0","");>+</div>';
echo '</div>';
}
$top = $height + $space;
$boxes = $days_in_month - ($days_so_far);
$rows = ceil($boxes/7);
$extra_boxes = 7 - $count_boxes;
for($r = 0; $r < $rows; $r++){
$m_top = $top * ($r + 1);
$a = ($r == 0) ? ($days_so_far + 1) * ($r + 1) : $a + 7;
$b = (($a + 7) <= $days_in_month) ? $a + 7 : ($days_in_month + 1);
$y = -1;
for ($j = $a; $j < $b; $j++) {
$y = $y + 1;
$m_lb = $mar_l * $y;
$x = $x + 1;
$x = (strlen($x) == 1) ? '0'.($x) : $x;
$mon = (strlen($month - 1) == 1) ? '0'.($month - 1) : ($month - 1);
$xtodate = $x.$mon.$year;
$class = ($xtodate == date('dmY')) ? "todaybox" : "daysbox";
echo '<div class="'.$class.'" style="margin-left:'.$m_lb.'px;margin-top:'.$m_top.'px">'
.'<span style="color:#fff">'.$x
.'</span>';
foreach($event as $edate => $ev){
$z = 14;
for($e = 0; $e < count($ev); $e++){
$id = $x.$e;
$num[] = $id;
$zx = $z * ($e);
$prtevent = ($edate == $xtodate) ? $ev[$e] : "";
echo '<div class="event" id="prtevent" style="margin-top:'.$zx.'px"" onmouseover=popup("'.($m_lb - 8).'","'.(($m_top + 70) + $zx).'","'.$edate.$e.'","1"); onmouseout=popup("0","0","'.$edate.$e.'","0")>'
.$prtevent
.'</div>';
}
}
$eventdate = substr($xtodate,4,4)."-".substr($xtodate,2,2)."-".substr($xtodate,0,2);
echo '<div class="newevent" onmousedown=newEvent(this,"1","'.$eventdate.'"); onmouseup=newEvent(this,"0","");>+</div>';
echo '</div>';
}
}
for ($i = 1; $i <= $extra_boxes; $i++) {
//echo "<td width=\"100\" height=\"100\" class=\"afterdayboxes\"> </td>";
}
echo '</div>';
foreach($event as $edatep => $evp){
for($t = 0; $t < count($evp); $t++){
$prteventp = $evp[$t];
echo '<div class="popdiv" id="popdiv'.$edatep.$t.'" style="display:none">';
echo '<div>'.$prteventp.'</div>';
echo '<div>'.substr($edatep,0,2)." ".$strmon[substr($edatep,2,2)]." ".substr($edatep,4,4).'</div>';
echo '</div>';
}
}
echo '<form action="" method="post" name="Event"><div class="addevent" id="addevent" style="display:none">'
.'<div class="close" id="close" onmouseover=addEvent("3","addevent",""); onmouseout=addEvent("4","addevent",""); onmousedown=addEvent("2","addevent",""); onmouseup=addEvent("0","addevent","");>X</div>';
$label = array("Event","Date Start","Date End");
$ftop = 26;
for($f = 0; $f < count($label); $f++){
$fxtop = $ftop * ($f + 3);
echo '<div style="position:absolute;margin-left:20px;margin-top:'.$fxtop.'px">'.$label[$f].'</div>';
$size = "20";
if($f == 0){$size = "50";}
echo '<div style="position:absolute;margin-left:120px;margin-top:'.$fxtop.'px"><input id="eventf'.$f.'" type="text" size="'.$size.'" name="field[]" style="text-transform:capitalize" /></div>';
}
echo '<div style="position:absolute;margin-left:200px;margin-top:'.($fxtop + 40).'px"><input type="button" value="Save" onclick=saveEvent("'.$f.'"); /></div>';
/*echo '<div><input type="text" name="startdate" id="eventdate" /> Until <input type="text" name="enddate" /></div>'
.'<div><input type="text" name="event" /></div>'
.'<div>'
.'<input type="button" value="Save" onclick="saveEvent()" />'
echo '<input type="hidden" name="save" />'
.'</div>';*/
echo '<input type="hidden" name="save" />';
echo '</div>';
echo '</div></form>';
//----------------------------------------------DIV STYLE------------------------------------------------
?>
<script type="text/javascript">
function crtHover(x, y){
x.className = y
}
function goDate(x){
window.location.href = "?x="+x;
}
function popup(left, top, val, ev){
var popdiv = document.getElementById('popdiv'+val);
popdiv.style.marginLeft = left+"px";
popdiv.style.marginTop = top+"px";
popdiv.style.display = (ev == '1') ? "" : "none";
}
function newEvent(x, y, z){
if(y == 1){
x.className = "neweventhover";
for(var i = 1; i <= 2; i++){
var eventdate = document.getElementById('eventf'+i);
eventdate.value = z;
}
}else{
var addEvent = document.getElementById('addevent');
x.className = "newevent";
addEvent.style.display = "";
}
}
function addEvent(x, y, z){
var addEvent = document.getElementById(y);
var cls = document.getElementById('close');
if(x == 1){
addEvent.style.display = "";
}else if(x == 2){
cls.className = "closedown";
}else if(x == 3){
cls.className = "closeover";
}else if(x == 4){
cls.className = "close";
}else{
cls.className = "close";
addEvent.style.display = "none";
}
}
function saveEvent(x){
var y = 0;
for(var i = 0; i < x; i++){
var eventf = document.getElementById('eventf'+i);
if(eventf.value == ""){
y++;
eventf.value = "Wajib";
}
}
if(y == 0){
document.Event.submit();
var addEvent = document.getElementById('addevent');
addEvent.style.display = "none";
}
}
</script>
</body>
</html>
Click links below to continue
data.php
css.php
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>My Calendar</title>
<?php
</head>
<body>
<?php
include("css.php");
$k = (isset($_GET['x'])) ? explode('-',$_GET['x']) : "";
$month = (isset($_GET['x'])) ? $k[0] : date('m') + 1;
$year = (isset($_GET['x'])) ? $k[1] : date('Y');
if($month == 14){ $year++ ; $month = 2;};
if($month == 1){ $year-- ; $month = 13;};
$mar_l = $width + $space;
$daysname = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
echo '<div class="ntfmain">';
echo '<div class="prev" id="prev" onmouseover=crtHover(this,"prevhover"); onmouseout=crtHover(this,"prev"); onclick=goDate("'.($month - 1).'-'.$year.'");>'
.'<'
.'</div>'
.'<div class="ntfdate">';
$ntfdate = "01"."-".($month - 1)."-".$year." "."00:00:00";
echo date('F Y',strtotime($ntfdate))
.'</div>';
echo '<div class="next" onmouseover=crtHover(this,"nexthover"); onmouseout=crtHover(this,"next"); onclick=goDate("'.($month + 1).'-'.$year.'");>'
.'>'
.'</div>'
.'</div>';
echo '<div class="mainbox">';
for($d = 0; $d < count($daysname); $d++){
$m_l = $mar_l * $d;
echo '<div class="daysname" style="margin-left:'.$m_l.'px;">'.$daysname[$d].'</div>';
}
//----------------------------------------------DIV STYLE------------------------------------------------
include("data.php");
if(isset($_POST['save'])){
$val = array();
$value = (isset($_POST['field'])) ? $_POST['field'] : "";
foreach($value as $key => $v){
$val[] = "'".ucwords($v)."'";
$valx = implode(",",$val);
}
saveData($valx);
}
$event = getData(); // get data
$count_boxes = 0;
$days_so_far = 0;
$strmon = array('01'=>'Januari','02'=>'Febuari','03'=>'Mac','04'=>'April','05'=>'Mei','06'=>'Jun','07'=>'Julai','08'=>'Ogos','09'=>'September','10'=>'Oktober','11'=>'November','12'=>'Disember');
echo '<div class="box" id="theBox">';
$days_in_month = date("t", mktime(0,0,0,$month,0,$year));
$first_day_of_month = date ("w", mktime(0,0,0,$month-1,1,$year));
$first_day_of_month = $first_day_of_month + 1;
$x = 0;
$num = array();
for ($i = $first_day_of_month-1; $i < 7; $i++) {
$days_so_far = $days_so_far + 1;
$count_boxes = $count_boxes + 1;
$m_l = $mar_l * $i;
$x = $x + 1;
$x = (strlen($x) == 1) ? '0'.($x) : $x;
$mon = (strlen($month - 1) == 1) ? '0'.($month - 1) : ($month - 1);
$xtodate = $x.$mon.$year;
$class = ($xtodate == date('dmY')) ? "todaybox" : "daysbox";
echo '<div class="'.$class.'" style="margin-left:'.$m_l.'px;">'
.'<span style="color:#fff">'.$x.'</span>';
foreach($event as $edate => $ev){
$z = 14;
for($e = 0; $e < count($ev); $e++){
$id = ($edate == $xtodate) ? $x.$e : 0;
$num[] = $id;
$zx = $z * ($e);
$prtevent = ($edate == $xtodate) ? $ev[$e] : "";
$popDivVal = ($edate == $xtodate) ? str_replace(' ',',',$prtevent).".".$x.",".$strmon[$mon].",".$year : "";
echo '<div class="event" id="prtevent" onmouseover=popup("'.($m_l - 8).'","'.((70) + $zx).'","'.$edate.$e.'","1"); onmouseout=popup("0","0","'.$edate.$e.'","0")>'
.$prtevent
.'</div>';
}
}
echo '<div class="newevent" onmousedown=newEvent(this,"1","'.$xtodate.'"); onmouseup=newEvent(this,"0","");>+</div>';
echo '</div>';
}
$top = $height + $space;
$boxes = $days_in_month - ($days_so_far);
$rows = ceil($boxes/7);
$extra_boxes = 7 - $count_boxes;
for($r = 0; $r < $rows; $r++){
$m_top = $top * ($r + 1);
$a = ($r == 0) ? ($days_so_far + 1) * ($r + 1) : $a + 7;
$b = (($a + 7) <= $days_in_month) ? $a + 7 : ($days_in_month + 1);
$y = -1;
for ($j = $a; $j < $b; $j++) {
$y = $y + 1;
$m_lb = $mar_l * $y;
$x = $x + 1;
$x = (strlen($x) == 1) ? '0'.($x) : $x;
$mon = (strlen($month - 1) == 1) ? '0'.($month - 1) : ($month - 1);
$xtodate = $x.$mon.$year;
$class = ($xtodate == date('dmY')) ? "todaybox" : "daysbox";
echo '<div class="'.$class.'" style="margin-left:'.$m_lb.'px;margin-top:'.$m_top.'px">'
.'<span style="color:#fff">'.$x
.'</span>';
foreach($event as $edate => $ev){
$z = 14;
for($e = 0; $e < count($ev); $e++){
$id = $x.$e;
$num[] = $id;
$zx = $z * ($e);
$prtevent = ($edate == $xtodate) ? $ev[$e] : "";
echo '<div class="event" id="prtevent" style="margin-top:'.$zx.'px"" onmouseover=popup("'.($m_lb - 8).'","'.(($m_top + 70) + $zx).'","'.$edate.$e.'","1"); onmouseout=popup("0","0","'.$edate.$e.'","0")>'
.$prtevent
.'</div>';
}
}
$eventdate = substr($xtodate,4,4)."-".substr($xtodate,2,2)."-".substr($xtodate,0,2);
echo '<div class="newevent" onmousedown=newEvent(this,"1","'.$eventdate.'"); onmouseup=newEvent(this,"0","");>+</div>';
echo '</div>';
}
}
for ($i = 1; $i <= $extra_boxes; $i++) {
//echo "<td width=\"100\" height=\"100\" class=\"afterdayboxes\"> </td>";
}
echo '</div>';
foreach($event as $edatep => $evp){
for($t = 0; $t < count($evp); $t++){
$prteventp = $evp[$t];
echo '<div class="popdiv" id="popdiv'.$edatep.$t.'" style="display:none">';
echo '<div>'.$prteventp.'</div>';
echo '<div>'.substr($edatep,0,2)." ".$strmon[substr($edatep,2,2)]." ".substr($edatep,4,4).'</div>';
echo '</div>';
}
}
echo '<form action="" method="post" name="Event"><div class="addevent" id="addevent" style="display:none">'
.'<div class="close" id="close" onmouseover=addEvent("3","addevent",""); onmouseout=addEvent("4","addevent",""); onmousedown=addEvent("2","addevent",""); onmouseup=addEvent("0","addevent","");>X</div>';
$label = array("Event","Date Start","Date End");
$ftop = 26;
for($f = 0; $f < count($label); $f++){
$fxtop = $ftop * ($f + 3);
echo '<div style="position:absolute;margin-left:20px;margin-top:'.$fxtop.'px">'.$label[$f].'</div>';
$size = "20";
if($f == 0){$size = "50";}
echo '<div style="position:absolute;margin-left:120px;margin-top:'.$fxtop.'px"><input id="eventf'.$f.'" type="text" size="'.$size.'" name="field[]" style="text-transform:capitalize" /></div>';
}
echo '<div style="position:absolute;margin-left:200px;margin-top:'.($fxtop + 40).'px"><input type="button" value="Save" onclick=saveEvent("'.$f.'"); /></div>';
/*echo '<div><input type="text" name="startdate" id="eventdate" /> Until <input type="text" name="enddate" /></div>'
.'<div><input type="text" name="event" /></div>'
.'<div>'
.'<input type="button" value="Save" onclick="saveEvent()" />'
echo '<input type="hidden" name="save" />'
.'</div>';*/
echo '<input type="hidden" name="save" />';
echo '</div>';
echo '</div></form>';
//----------------------------------------------DIV STYLE------------------------------------------------
?>
<script type="text/javascript">
function crtHover(x, y){
x.className = y
}
function goDate(x){
window.location.href = "?x="+x;
}
function popup(left, top, val, ev){
var popdiv = document.getElementById('popdiv'+val);
popdiv.style.marginLeft = left+"px";
popdiv.style.marginTop = top+"px";
popdiv.style.display = (ev == '1') ? "" : "none";
}
function newEvent(x, y, z){
if(y == 1){
x.className = "neweventhover";
for(var i = 1; i <= 2; i++){
var eventdate = document.getElementById('eventf'+i);
eventdate.value = z;
}
}else{
var addEvent = document.getElementById('addevent');
x.className = "newevent";
addEvent.style.display = "";
}
}
function addEvent(x, y, z){
var addEvent = document.getElementById(y);
var cls = document.getElementById('close');
if(x == 1){
addEvent.style.display = "";
}else if(x == 2){
cls.className = "closedown";
}else if(x == 3){
cls.className = "closeover";
}else if(x == 4){
cls.className = "close";
}else{
cls.className = "close";
addEvent.style.display = "none";
}
}
function saveEvent(x){
var y = 0;
for(var i = 0; i < x; i++){
var eventf = document.getElementById('eventf'+i);
if(eventf.value == ""){
y++;
eventf.value = "Wajib";
}
}
if(y == 0){
document.Event.submit();
var addEvent = document.getElementById('addevent');
addEvent.style.display = "none";
}
}
</script>
</body>
</html>
Click links below to continue
data.php
css.php
PHP Event Calendar (cont)
Php script to save created event into database
data.php
<?php
$host = "localhost";
$username = "root";
$password = "";
mysql_connect($host,$username,$password);
mysql_select_db("calendar");
function getData(){
$event = array();
//------------------------------------edit here only-----------------------------------------------------
$sql = "select * from event";
//-------------------------------------------------------------------------------------------------------
$query = mysql_query($sql);
while($result = mysql_fetch_assoc($query)){
$dt_start = mktime(0,0,0,date('m',strtotime($result['dt_start'])),date('d',strtotime($result['dt_start'])),date('Y',strtotime($result['dt_start'])));
$dt_end = mktime(0,0,0,date('m',strtotime($result['dt_end'])),date('d',strtotime($result['dt_end'])),date('Y',strtotime($result['dt_end'])));
$difference = $dt_end - $dt_start;
$daysago = floor($difference /60/60/24);
$i = 0;
while ($i <= $daysago) {
if ($i != 0) { $dt_start = $dt_start + 86400; }
$today = date('Y-m-d',$dt_start);
$day = date('dmY',strtotime($today));
$event[$day][] = $result['event'];
$i++;
}
}
return $event;
}
function saveData($x){
echo $sql = "insert into event "
."(event, dt_start, dt_end) "
."values "
."(".$x.")";
return mysql_query($sql);
}
?>
Click links below to continue
css.php
data.php
<?php
$host = "localhost";
$username = "root";
$password = "";
mysql_connect($host,$username,$password);
mysql_select_db("calendar");
function getData(){
$event = array();
//------------------------------------edit here only-----------------------------------------------------
$sql = "select * from event";
//-------------------------------------------------------------------------------------------------------
$query = mysql_query($sql);
while($result = mysql_fetch_assoc($query)){
$dt_start = mktime(0,0,0,date('m',strtotime($result['dt_start'])),date('d',strtotime($result['dt_start'])),date('Y',strtotime($result['dt_start'])));
$dt_end = mktime(0,0,0,date('m',strtotime($result['dt_end'])),date('d',strtotime($result['dt_end'])),date('Y',strtotime($result['dt_end'])));
$difference = $dt_end - $dt_start;
$daysago = floor($difference /60/60/24);
$i = 0;
while ($i <= $daysago) {
if ($i != 0) { $dt_start = $dt_start + 86400; }
$today = date('Y-m-d',$dt_start);
$day = date('dmY',strtotime($today));
$event[$day][] = $result['event'];
$i++;
}
}
return $event;
}
function saveData($x){
echo $sql = "insert into event "
."(event, dt_start, dt_end) "
."values "
."(".$x.")";
return mysql_query($sql);
}
?>
Click links below to continue
css.php
PHP Event Calendar (cont)
I created css style and save it as .php
css.php
<?php
$width = "120";
$height = "100";
$ntfprev = "280";
$ntfm = $ntfprev + 100;
$ntfnext = $ntfm + 180;
$space =3;
?>
<style type="text/css">
.daysname {
position: absolute;
width: <?php echo $width;?>px;
text-align: center;
height: 26px;
padding-top: 4px;
background-color: #7D2800;
color: #CCC;
box-shadow: 10px 10px 5px #888888;
border-radius:5px;
font-weight: bold;
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
}
.popdiv {
position: absolute;
background-color: #000;
height: 60px;
width: 160px;
color: #FFF;
border-radius:5px;
opacity:0.8;
text-indent: 10;
}
.event {
cursor: pointer;
color: #3E1400;
text-indent: 10px;
position: absolute;
}
.newevent {
margin-left: <?php echo $width - 20;?>px;
margin-top: <?php echo $height - 35;?>px;
position: absolute;
background-color: #551C00;
height: 12px;
width: 12px;
text-align: center;
font-weight: bold;
border: 1px outset #333;
cursor: pointer;
color: #CCC;
border-radius:2px;
}
.neweventhover {
margin-left: <?php echo $width - 20;?>px;
margin-top: <?php echo $height - 35;?>px;
position: absolute;
background-color: #CCC;
height: 12px;
width: 12px;
text-align: center;
font-weight: bold;
border: 1px inset #333;
cursor: pointer;
border-radius:2px;
}
.box {
position: absolute;
margin-top: 40px;
}
.daysbox {
position: absolute;
width: <?php echo $width;?>px;
height: <?php echo $height;?>px;
background-color: #B75B00;
box-shadow: 10px 10px 5px #888888;
border-radius:8px;
opacity:0.8;
}
.ntfdate {
margin-left: <?php echo $ntfm;?>px;
height: 40px;
position: absolute;
}
.ntfmain {
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
color: #999;
height: 50px;
}
.next {
position: absolute;
margin-left: <?php echo $ntfnext;?>px;
text-align: center;
width: 30px;
border: 1px solid #930;
box-shadow: 10px 10px 5px #888888;
border-radius:5px;
opacity:0.3;
background-color: #B75B00;
color: #FFF;
}
.nexthover {
position: absolute;
margin-left: <?php echo $ntfnext;?>px;
text-align: center;
width: 30px;
border: 1px solid #930;
background-color: #7D2800;
cursor: pointer;
color: #FFF;
box-shadow: 10px 10px 5px #888888;
border-radius:5px;
opacity:0.8;
}
.prev {
position: absolute;
margin-left: <?php echo $ntfprev;?>px;
border: 1px solid #930;
width: 30px;
text-align: center;
box-shadow: 10px 10px 5px #888888;
border-radius:5px;
opacity:0.3;
background-color: #B75B00;
color: #FFF;
}
.prevhover {
position: absolute;
margin-left: <?php echo $ntfprev;?>px;
border: 1px solid #930;
width: 30px;
text-align: center;
background-color: #7D2800;
cursor: pointer;
color: #FFF;
box-shadow: 10px 10px 5px #888888;
border-radius:5px;
opacity:0.8;
}
.todaybox {
position: absolute;
border: 1px solid #000;
width: <?php echo $width;?>px;
height: <?php echo $height;?>px;
background-color: #555555;
color: #FFF;
box-shadow: 10px 10px 5px #888888;
border-radius:5px;
opacity:0.8;
}
.mainbox {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
margin-left: 20px;
}
.addevent {
background-color: #F60;
height: 300px;
width: 500px;
position: absolute;
opacity: 0.9;
border-radius:10px;
margin-top: 80px;
margin-left: 120px;
}
.close {
height: 20px;
width: 20px;
background-color: #900;
color: #FFF;
text-align: center;
margin-top: 6px;
float: right;
position: absolute;
margin-left: 473px;
cursor: pointer;
font-weight: bold;
border: 1px solid #000;
}
.closeover {
height: 20px;
width: 20px;
background-color: #900;
color: #FFF;
text-align: center;
margin-top: 6px;
float: right;
position: absolute;
margin-left: 473px;
cursor: pointer;
font-weight: bold;
border: 1px outset #000;
}
.closedown {
height: 20px;
width: 20px;
background-color: #900;
color: #FFF;
text-align: center;
margin-top: 6px;
float: right;
position: absolute;
margin-left: 473px;
cursor: pointer;
font-weight: bold;
border: 1px inset #000;
}
</style>
css.php
<?php
$width = "120";
$height = "100";
$ntfprev = "280";
$ntfm = $ntfprev + 100;
$ntfnext = $ntfm + 180;
$space =3;
?>
<style type="text/css">
.daysname {
position: absolute;
width: <?php echo $width;?>px;
text-align: center;
height: 26px;
padding-top: 4px;
background-color: #7D2800;
color: #CCC;
box-shadow: 10px 10px 5px #888888;
border-radius:5px;
font-weight: bold;
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
}
.popdiv {
position: absolute;
background-color: #000;
height: 60px;
width: 160px;
color: #FFF;
border-radius:5px;
opacity:0.8;
text-indent: 10;
}
.event {
cursor: pointer;
color: #3E1400;
text-indent: 10px;
position: absolute;
}
.newevent {
margin-left: <?php echo $width - 20;?>px;
margin-top: <?php echo $height - 35;?>px;
position: absolute;
background-color: #551C00;
height: 12px;
width: 12px;
text-align: center;
font-weight: bold;
border: 1px outset #333;
cursor: pointer;
color: #CCC;
border-radius:2px;
}
.neweventhover {
margin-left: <?php echo $width - 20;?>px;
margin-top: <?php echo $height - 35;?>px;
position: absolute;
background-color: #CCC;
height: 12px;
width: 12px;
text-align: center;
font-weight: bold;
border: 1px inset #333;
cursor: pointer;
border-radius:2px;
}
.box {
position: absolute;
margin-top: 40px;
}
.daysbox {
position: absolute;
width: <?php echo $width;?>px;
height: <?php echo $height;?>px;
background-color: #B75B00;
box-shadow: 10px 10px 5px #888888;
border-radius:8px;
opacity:0.8;
}
.ntfdate {
margin-left: <?php echo $ntfm;?>px;
height: 40px;
position: absolute;
}
.ntfmain {
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
color: #999;
height: 50px;
}
.next {
position: absolute;
margin-left: <?php echo $ntfnext;?>px;
text-align: center;
width: 30px;
border: 1px solid #930;
box-shadow: 10px 10px 5px #888888;
border-radius:5px;
opacity:0.3;
background-color: #B75B00;
color: #FFF;
}
.nexthover {
position: absolute;
margin-left: <?php echo $ntfnext;?>px;
text-align: center;
width: 30px;
border: 1px solid #930;
background-color: #7D2800;
cursor: pointer;
color: #FFF;
box-shadow: 10px 10px 5px #888888;
border-radius:5px;
opacity:0.8;
}
.prev {
position: absolute;
margin-left: <?php echo $ntfprev;?>px;
border: 1px solid #930;
width: 30px;
text-align: center;
box-shadow: 10px 10px 5px #888888;
border-radius:5px;
opacity:0.3;
background-color: #B75B00;
color: #FFF;
}
.prevhover {
position: absolute;
margin-left: <?php echo $ntfprev;?>px;
border: 1px solid #930;
width: 30px;
text-align: center;
background-color: #7D2800;
cursor: pointer;
color: #FFF;
box-shadow: 10px 10px 5px #888888;
border-radius:5px;
opacity:0.8;
}
.todaybox {
position: absolute;
border: 1px solid #000;
width: <?php echo $width;?>px;
height: <?php echo $height;?>px;
background-color: #555555;
color: #FFF;
box-shadow: 10px 10px 5px #888888;
border-radius:5px;
opacity:0.8;
}
.mainbox {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
margin-left: 20px;
}
.addevent {
background-color: #F60;
height: 300px;
width: 500px;
position: absolute;
opacity: 0.9;
border-radius:10px;
margin-top: 80px;
margin-left: 120px;
}
.close {
height: 20px;
width: 20px;
background-color: #900;
color: #FFF;
text-align: center;
margin-top: 6px;
float: right;
position: absolute;
margin-left: 473px;
cursor: pointer;
font-weight: bold;
border: 1px solid #000;
}
.closeover {
height: 20px;
width: 20px;
background-color: #900;
color: #FFF;
text-align: center;
margin-top: 6px;
float: right;
position: absolute;
margin-left: 473px;
cursor: pointer;
font-weight: bold;
border: 1px outset #000;
}
.closedown {
height: 20px;
width: 20px;
background-color: #900;
color: #FFF;
text-align: center;
margin-top: 6px;
float: right;
position: absolute;
margin-left: 473px;
cursor: pointer;
font-weight: bold;
border: 1px inset #000;
}
</style>
Subscribe to:
Posts (Atom)