Wednesday, June 29, 2016

HTML basic 6

Animation for Games!
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8" />
 <script src="bucky.js"></script>
</head>
<body>
 <section id="main">
 <canvas id="canvas" width="600" height="400">
 hopefully you dont see this. if you do, get google chrome
 </canvas>
 </section>
</body>
</html>

Bucky.JS
function doFirst(){
 var x = document.getElementById('canvas');
 canvas=x.getContext('2d');
 window.addEventListener("mousemove", bucky, false);
}
function bucky(e){
 // canvas.clearRect(0, 0, 600, 400);
 var xPos=e.clientX;
 var yPos=e.clientY;
 canvas.fillRect(xPos-10, yPos-10, 20, 20);
}
window.addEventListener("load", doFirst, false);

Drag and Drop
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8" />
 <link rel="stylesheet" href="bucky.css">
 <script src="bucky2.js"></script>
</head>
<body>
 <section id="leftbox">
 I dare you to drag an image in me!
 </section>
 <section id="rightbox">
  <img id="facepic" width="120" height="150" src="images/myface.png">
 </section>
</body>
</html>

bucky.css:
#leftbox{
 float:left;
 width:250px;
 height: 250px;
 margin: 5px;
 border: 3px solid blue;
}
#rightbox{
 float:left;
 width:250px;
 height: 250px;
 margin: 5px;
 border: 3px solid green;
}

bucky2.js:
function doFirst(){
 mypic = document.getElementById('facepic');
 mypic.addEventListener("dragstart", startDrag, false);
 leftbox=document.getElementById('leftbox');
 leftbox.addEventListener("dragenter", function(e){ e.preventDefault();}, false);
 leftbox.addEventListener("dragover", function(e){ e.preventDefault();}, false);
 leftbox.addEventListener("drop", dropped, false);
}
function startDrag(e){
 var code='<img id="facepic" width="120" height="150" src="images/myface.png">';
 e.dataTransfer.setData('Text', code);
}
function dropped(e){
 e.preventDefault();
 leftbox.innerHTML = e.dataTransfer.getData('Text');
}
window.addEventListener("load", doFirst, false);
\

HTML basic 5

Playing Video:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body style="background-color:#8EE5EE;">
<div id="box">
<video src="videos/34.mp4" width="640" height="360" poster="images/myface.png"
autoplay loop controls > </video>
</div>
</body>
</html>

Animation and Transition
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="new.css">
</head>
<body>
<div id="box">
<p id="text">omgwtfbbq</p>
</div>
</body>
</html>

New.css:
body{
text-align: center;
}
#box{
display: block;
width: 500px;
margin: 150px auto;
padding: 15px;
text-align: center;
border: 7px solid blue;
background: yellow;
-webkit-transition: -webkit-transform 1s;
}
#box:hover{
-webkit-transform:rotate(45deg);

}

Awesome Rollover Buttons:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="new.css">
</head>
<body>
<div id="box">
<p id="text">omgwtfbbq</p>
</div>
</body>
</html>
New.css:
#box {
        -webkit-border-radius: 10px;
-webkit-transition-property: background-color, width, opacity, height, -webkit-transform;
-webkit-transition-duration: 1s, 1s, 1s, 1s, 1s;
-webkit-transition-timing-function: linear, ease-in, ease-in, ease-in, ease-in;


}

#box:hover {
opacity: 1.0;
background: orange;
width: 200px;
height: 120px;
-webkit-transform: rotate(360deg);
}

HTML and CSS Basic 4

Flexible:
Index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Hello World</title>
<link rel="stylesheet" href="main2.css">
</head>
<body>
<div id="big_wrapper">
<header id="top_header">
<h1>Welcome to thenewboston!</h1>
</header>

<nav id="top_menu">
<ul>
<li>Home</li>
<li>Tutorial</li>
<li>Podcast</li>
</ul>
</nav>

<div id="new_div">

<section id="main_section">
<article>
<header>
<hgroup>
<h1>Title of article</h1>
<h2>Subtitle for article</h2>
    </hgroup>
</header>
         <p>This is the best article eva!</p>
<footer>
<p> written by Zoloo</p>
</footer>
</article>
<article>
<header>
<hgroup>
<h1>Title of article 2</h1>
<h2>Subtitle for article 2</h2>
</hgroup>
</header>
<p>This is the best article eva!</p>
<footer>
<p> written by Zoloo</p>
</footer>
</article>

</section>
<aside id="side_news">
<h4>News</h4>
Bucky has a new dog!
</aside>
</div>
<footer id="the_footer">
Copyright thenewboston 2011
</footer>
</div>
</body>
</html>

Main2.css
*{
margin:0px;
padding: 0px;
}
h1{ font: bold 20px Tahoma; }
h2{ font: bold 14px Tahoma; }
header, section, footer, aside, nav, article, hgroup{
display:block;
}
body{
width: 100%;
display: -webkit-box;
-webkit-box-pack: center;
}
#big_wrapper{
max-width:1000px;
margin: 20px 0px;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-box-flex: 1;
}
#top_header{
background: yellow;
border: 3px solid black;
padding: 20px;
}
#top_menu{
border: red;
background:blue;
color: white;
}
#top_menu li {
display: inline-block;
list-style: none;
padding: 5px;
font: bold 14px Tahoma;
}
#new_div{
display: -webkit-box;
-webkit-box-orient: horizontal;
}
#main_section{
border: 1px solid blue;
-webkit-box-flex: 1;
margin: 20px;
padding:20px;
}
#side_news{
border: 1px solid red;
width: 220px;
margin: 20px 0px;
padding: 30px;
background: #66CCCC;
}
#the_footer{
text-align: center;
padding:20px;
border-top: 2px solid green;
}

Sunday, June 12, 2016

HTML and CSS Basic 3

INDEX.HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Hello World</title>
<link rel="stylesheet" href="main.css">
</head>
<body>
<div id="big_wrapper">
<header id="top_header">
<h1>Welcome to thenewboston!</h1>
</header>

<nav id="top_menu">
<ul>
<li>Home</li>
<li>Tutorial</li>
<li>Podcast</li>
</ul>
</nav>
<section id="main_section">
<article>
<header>
<hgroup>
<h1>Title of article</h1>
<h2>Subtitle for article</h2>
</hgroup>
</header>
<p>This is the best article eva!</p>
<footer>
<p> written by Zoloo</p>
</footer>
</article>
<article>
<header>
<hgroup>
<h1>Title of article 2</h1>
<h2>Subtitle for article 2</h2>
</hgroup>
</header>
<p>This is the best article eva!</p>
<footer>
<p> written by Zoloo</p>
</footer>
</article>

</section>
<aside id="side_news">
<h4>News</h4>
Bucky has a new dog!
</aside>
<footer id="the_footer">
Copyright thenewboston 2011
</footer>
</div>
</body>
</html>

MAIN.CSS
*{
margin:0px;
padding:0px;
}
h1{
font: bold 20px Tahoma;
}
h2{
font: bold 14px Tahoma;
}
header, section, footer, aside, nav, article, hgroup{
display:block;
}
body{
text-align:center;
}
#big_wrapper{
border: 1px solid black;
width:1000px;
margin:20px auto;
text-align:left;
}
#top_header{
background:yellow;
border: 1px solid blue;
padding: 20px;
}
#top_menu{
background:blue;
color: white;
}
#top_menu li{
display:inline-block;
list-style:none;
padding:5px;
font: bold 14px Tahoma;
}
#main_section{
float:left;
width: 660px;
margin:30px;
}
#side_news{
float:left;
width:220px;
margin:20px 0px;
padding: 30px;
background: #66CCCC;
}
#the_footer{
clear:both;
text-align:center;
padding: 20px;
border-top:2px solid green;
}
article{
background:#FFFBCC;
border:1px solid red;
padding:20px;
margin-bottom:10px;
}
article footer{
text-align:right;
}

Wednesday, June 8, 2016

HTML basic 2

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<title>Page Title</title>
</head>
<body>
<form>
Username: <input type="text" name="username" size="20" value="Enter name here" />
<br>
Male: <input type="radio" name="sex" value="male" />
Female: <input type="radio" name="sex" value="female" />
<p>Select the foods that you would like to order!</p>
Bacon <input type="checkbox" name="sex" value="bacon" />
Ham <input type="checkbox" name="sex" value="ham" />
Tuna <input type="checkbox" name="sex" value="tuna" />
Soda Pop <input type="checkbox" name="sex" value="sodapop" />
<br>
<br>
What car do you want?
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
<br>
This is textarea:<br>
<textarea name="message" rows="5" cols="30">
The cat was playing in the garden.
</textarea>
<br>
Password: <input type="password" name="pword" />
<p>Now submit a file! </p>
<input type="file" name="buckyfile" />
</form>
</body>
</html>

Monday, June 6, 2016

HTML basic 1

<!DOCTYPE html>
<html>
<head>
<title> This is the title </title>
</head>

<body>
<strong> Im in the body </strong>
<hr/>
<p><em> This is paragraph </em></p>
<!-- this is comment -->
This will be a <a href="second.html">link</a>
<br><a href="mailto:zooljaboy@yahoo.com" title = "this is tip">Email Zoloo</a><br>
<img src="http://www.w3schools.com/images/w3schools_green.jpg" alt="W3Schools.com" width="160" height="128">
<br>

<!-- TABLE -->
<table border="1">
<tr>
<th>A team</th>
<th>B team</th>
<th>C team</th>
</tr>
<tr>
<td>Atlanta</td>
<td>Boston</td>
<td>Chicago</td>
</tr>
<tr>
<td>New York</td>
<td>Miami</td>
<td>Indiana</td>
</tr>
<tr>
<td>Washington</td>
<td>Portland</td>
<td>New Orlean</td>
</tr>
</table>

<br>
<table border="1" cellspacing="10">
<tr>
<th colspan="2"><h3>NBA all star</h3></th>
</tr>
<tr>
<th>East</th>
<th>West</th>
</tr>
<tr>
<td>Miami</td>
<td>Indiana</td>
</tr>
<tr>
<td>Portland</td>
<td>New Orlean</td>
</tr>
</table>

<!-- LIST -->
<br>
<ul>
<li>Point Guard</li>
<li>Shooting Guard</li>
<li>Small Forward</li>
<li>Power Forward</li>
<li>Center</li>
</ul>
<ol>
<li>Point Guard</li>
<li>Shooting Guard</li>
<li>Small Forward</li>
<li>Power Forward</li>
<li>Center</li>
</ol>
</body>
</html>

Sunday, May 29, 2016

PHP Tutorial 12

Logging the User In Out:
Index.php
<?php
require 'core.inc.php';
require 'connect.inc.php';

if (loggedin()) {
            echo 'You\'re logged in.<a href="logout.php">Log out</a>';
} else {
                        include 'loginform.inc.php';    
}

?>

Loginform.inc.php
<?php
if(isset($_POST['username']) && isset($_POST['password'])){
            $username=$_POST['username'];
            $password=$_POST['password'];
            $password2 = md5($password);
            if(!empty($username) && !empty($password)){
                        $query="SELECT id FROM users WHERE username='".$username."' AND password='".$password2."'";
                       
                        if($query_run = mysqli_query($mysql_conn,$query)){
                                    $query_num_rows=mysqli_num_rows($query_run);
                                    if($query_num_rows==0){
                                                echo 'Invalid username/password combination.';
                                    } else if($query_num_rows==1) {
                                                // echo 'OK';
                                      $user_id=mysqli_fetch_array($query_run);
                                       extract($user_id);
                                       $_SESSION['user_id']=$user_id;
                                       header('Location: Tutorial2.php');
                                    }
                        }
            } else {
                        echo 'You must supply a username and password';
            }
}
?>

<form action="<?php echo $current_file; ?>" method="POST">
Username: <input type="text" name="username"><br><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Log in">
</form>

Core.inc.php
<?php
ob_start();
session_start();
$current_file = $_SERVER['SCRIPT_NAME'];
@$http_referer = $_SERVER['HTTP_REFERER'];
function loggedin(){
                if(isset($_SESSION['user_id']) && !empty($_SESSION['user_id']))
                                return true;
                return false;
}
?>

Logout.php
<?php
require 'core.inc.php';
session_destroy();
header('Location: '.$http_referer);

?>

PHP Tutorial 11

Auto Suggest Application / AJAX /
index.html
<html>
<head>
<script type="text/javascript">
function findmatch(){
                if(window.XMLHttpRequest){
                                xmlhttp= new XMLHttpRequest();
                } else {
                                xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
                }
                xmlhttp.onreadystatechange=function(){
                                if(xmlhttp.readyState ==4 && xmlhttp.status==200){
                                                document.getElementById('results').innerHTML = xmlhttp.responseText;
                                }
                }
                xmlhttp.open('GET', 'search.inc.php?search_text='+document.search.search_text.value, true);
                xmlhttp.send();
}
</script>
</head>
<body>
<form id="search" name="search">
                Type a name:<br>
                <input type="text" name="search_text" onkeydown="findmatch();">
</form>
<div id="results"> </div>

</body>
</html>

Search.inc.php
<?php
if(isset($_GET['search_text'])){
                 $search_text=$_GET['search_text'];
}
if(!empty($search_text)){
                if(@mysql_connect('localhost', 'root', '')){
                                if(@mysql_select_db('a_database')){
                                                $query= "SELECT name FROM names WHERE name LIKE '".mysql_real_escape_string($search_text)."%'";
                                                $query_run=mysql_query($query);
                                                while($query_row=mysql_fetch_assoc($query_run)){
                                                                // echo $name=$query_row['name'].'<br>';
                                                                echo $name='<a href="anotherpage.php?search_text='.$query_row['name'].'">'.$query_row['name'].'</a><br>';

                                                }
                                }
                }
}
?>

POSTing Data / AJAX /
Index.php
<html>
<head>

<script type="text/javascript">
function insert(){
                // var text_value = document.getElementById('insert_text').value;
                // alert(text_value);
                if(window.XMLHttpRequest){
                                xmlhttp= new XMLHttpRequest();
                } else {
                                xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
                }
                xmlhttp.onreadystatechange=function(){
                                if(xmlhttp.readyState ==4 && xmlhttp.status==200){
                                                document.getElementById('message').innerHTML = xmlhttp.responseText;
                                }
                }
                parameters='text='+document.getElementById('insert_text').value;
                xmlhttp.open('POST', 'update.inc.php', true);
                xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
                xmlhttp.send(parameters);
}
</script>

</head>
<body>
Insert: <input type="text" id="insert_text">
<input type="button" value ="Submit" onclick="insert();">
<div id="message"> </div>
</body>
</html>

Update.inc.php
<?php
// echo $_POST['text'];
@mysql_connect('localhost', 'root', '');
@mysql_select_db('a_database');
if(isset($_POST['text'])){
                $text = $_POST['text'];
                if(!empty($text)){
                                $query="INSERT INTO data VALUES('', '".mysql_real_escape_string($text)."')";
                                if($query_run=mysql_query($query)){
                                                echo 'Data inserted!';
                                } else {
                                                echo 'Failed';
                                }
                } else {
                                echo 'Please type something.';
                }
}
?>

Try, throw, catch
<?php
$age=16;
try{
                if($age>18) echo 'Old enough';
                else throw new Exception('Not old enough');
} catch(Exception $ex){
                echo 'Error: '.$ex->getMessage();
}

?>

Wednesday, May 25, 2016

PHP Tutorial 10

SELECT by example:
<form action ="Tutorial.php" method="GET">
                Choose a food type:
                                <select name="uh">
                                <option value="u">Unhealthy</option>
                                <option value="h">Healthy</option>
                                </select><br><br>
                                <input type="submit" value="Submit">
</form>

<?php
$mysql_host='localhost';
$mysql_user='root';
$mysql_pass='';
$mysql_db='a_database';

if(isset($_GET['uh']) && !empty($_GET['uh'])){
                $uh=strtolower($_GET['uh']);
               
                if($uh=='u' || $uh=='h') {
$query="SELECT `food`, `calories` FROM `food` WHERE `healthy_unhealthy`='".$uh."' ORDER BY `id` DESC";
                                              
                if(@mysql_connect($mysql_host, $mysql_user, $mysql_pass)){
                      if(@mysql_select_db($mysql_db)){
                                                                              
                        if($query_run=mysql_query($query)){
                                                                                              
                                    while($query_row = mysql_fetch_assoc($query_run)){
                                            $food = $query_row['food'];
                                            $calories=$query_row['calories'];
                             echo $food.' has '.$calories.' calories.<br>';
                               }
                } else {
                     echo 'Query failed';
         }
      }
    }
  } else {
     echo 'Must be u, or h.';
 }
}

?>

LIKE with a Search Engine Example:
<form action ="Tutorial.php" method="POST">
 Name: <input type="text" name="search_name"><input type="submit" value="Search">
</form>

<?php
require 'connect.inc.php';

if(isset($_POST['search_name'])){  
 $search_name = $_POST['search_name'];

 if(!empty($search_name)){
   $query = "SELECT name FROM names WHERE name LIKE '%".mysqli_real_escape_string($mysql_conn,$search_name)."%'";
   $query_run = mysqli_query($mysql_conn,$query); 

   if(mysqli_num_rows($query_run) == NULL){ 
    echo 'No result found'; 
   }else{
    echo 'Result found: <br>';
   while($query_row = mysqli_fetch_assoc($query_run)){ 
    echo $query_row['name'].'<br>'; 
   }
  }
}
}
?>
connect.inc.php
<?php
 $host = 'localhost';
 $name = 'root';
 $password = '';
 $database = 'a_database';   //your database name
 if(@mysqli_connect($host, $name, $password)) {
  $mysql_conn = @mysqli_connect($host, $name, $password);
  if(@mysqli_select_db($mysql_conn, $database)) {
  } else {
   die('Could not find database.');
  }
 } else {
  die('Could not connect.');
 }

?>


Tuesday, May 24, 2016

PHP Tutorial 9

MD5 Encryption
<?php
/*
$string ='password';
$string_hash = md5($string);
echo $string_hash;
*/
if(isset($_POST['user_password']) && !empty($_POST['user_password'])){
            $user_password = md5($_POST['user_password']);
           
            $filename = 'hash.txt';
            $handle = fopen($filename, 'r');
            $file_password=fread($handle, filesize($filename));
           
            if($user_password==$file_password){
                        echo 'Password ok!';
            } else {
                        echo 'Incorrect password.';
            }
} else {
            echo 'Please enter a password';
}
?>
<form action ="some2.php" method="POST">
Password: <input type="text" name="user_password"><br><br>
<input type="submit" value="Submit">
</form>

Sending an Email
<?php
$to='alex@phpacademy.org';
$subject ='This is an email';
$body='This is a test email'."\n\n".'Hope you got it.';
$headers='From: Zoljargal <someone@phpacademy.org>';

if(mail($to, $subject, $body, $headers)){
            echo 'Email has been sent to '.$to;
} else {
            echo 'There was an error sending the email.';
}
?>

Creating a Simple Contact Form
<?php
if(isset($_POST['contact_name']) && isset($_POST['contact_email']) && isset($_POST['contact_text'])){
            $c_name = $_POST['contact_name'];
            $c_email = $_POST['contact_email'];
            $c_text = $_POST['contact_text'];
            if(!empty($c_name) && !empty($c_email) && !empty($c_text)){
                        if(strlen($c_name)>25 || strlen($c_email)>50 || strlen($c_text)>1000){
                                    echo 'Sorry, maxLength for each field has been exceeded.';
                        } else {
                                                $to='ub_edu@yahoo.com';
                                                $subject ='Contact form submitted.';
                                                $body=$c_name."\n\n".$c_text;
                                                $headers='From: '.$c_email;

                                                if(mail($to, $subject, $body, $headers)){
                                                            echo 'Email has been sent to '.$to;
                                                } else {
                                                            echo 'There was an error sending the email.';
                                                }
                                    }
            } else {
                        echo 'All fields are required.';
            }
}
?>
<form action = "some2.php" method="POST">
Name:<br><input type="text" name="contact_name" maxlength="25"><br><br>
Email address:<br><input type="text" name="contact_email" maxlength="50"><br><br>
Message:<br>
<textarea  name="contact_text" rows="6" cols="30" maxlength="1000"></textarea><br><br>
<input type="submit" value="Submit">
</form>

Reading a Simple XML File
<?php
$xml = simplexml_load_file('names.xml');
// echo $xml->producer[1]->name.' is '.$xml->producer[1]->age;
foreach($xml->producer as $producer){
echo $producer->name.'('.$producer->age.')<br>';
foreach($producer->show as $show){
echo $show->showname.' on '.$show->showdate.'<br>';
}
}

?>