Tuesday, August 4, 2015

PHP PROGRAM FOR PRINTING DIAMOND PATTERN OF ALPHABETS

This program prints diamond pattern of alphabets, validations using PHP. This program checks the input value is even number between 2 and 24 then prints the diamond pattern of alphabets.


<html>
<head>
<style type="text/css">
.but {
border-radius: 20px;
width: 250px;
height: 40px;
font-weight: bolder;
font-size: 20px;
}
</style>
<body>
<form id="formd" method="get" action="<?php $_PHP_SELF ?>">
<label>Diamond No.:</label>
<input type="text" name="dia" maxlength="2" placeholder = "0" />
<button type="submit" class="but" form="formd" value="submit">Make Diamonds</button>
</form>
</body>
</html>

<?php
$val = $_GET["dia"];
if (($val<25) && ($val>1)) {
  $t = $val%2;
if ($t==0) {
drawDiamond($val);
}
else {
echo "Only EVEN numbers between 2 and 24 are allowed";
}
}
else {
echo "Only EVEN numbers between 2 and 24 are allowed";
}

function drawDiamond($a) {
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$count= 0;
echo "<pre>";
for ($i= 1;$i<=($a/2);$i++) {
for ($s=($a/2); $s>=$i; $s--) {
print(" ");
}
for ($j= 1; $j<(2*$i); $j++) {
print("$str[$count]");
}
$count++;
print ("\n");
}
for ($p=1;$p<=$a+1;$p++) {
print("$str[$count]");
}
print("\n");
$h=1;
for ($d=1;$d<=($a/2);$d++) {
for ($e=1;$e<=$d;$e++) {
print(" ");
}
$count--;
for ($f=($a-$h);$f>=1;$f--) {
print("$str[$count]");
}
$h=$h+2;
print("\n");
}
echo "</pre>";
}
?>

JAVASCRIPT PROGRAM FOR PRINTING DIAMOND PATTERN OF ALPHABETS

HTML program for printing diamond pattern of alphabets validations using JavaScript, this program works on both ways by appending child and document.write method. For using this program with document.write method simply uncomment the commented part and comment the appendChild part and innerHTML part. This program checks the input is even number between 2 and 24 and then prints the diamond pattern of alphabets.


<html>
<head>
<style type="text/css">
.but {
border-radius: 20px;
width: 250px;
height: 40px;
font-weight: bolder;
font-size: 20px;
}
</style>
<script type="text/javascript" >
function makeDiamond() {
var n = document.getElementById("dia").value;
n = Number(n);
if ((n<25) && (n>1)) {
  var t = n%2;
if (t==0) {
drawDiamond(n);
}
else {
alert("Only EVEN numbers between 2 and 24 are allowed");
}
}
else {
alert("Only EVEN numbers between 2 and 24 are allowed");
}
}
function drawDiamond(a) {
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var count= 0;
var pree = document.createElement("pre");
var divv = document.createElement("div");
var sp= document.getElementById("ad");
pree.setAttribute("id", "pr");
sp.appendChild(divv);
sp.appendChild(pree);
// document.getElementById("pr").innerHTML += ;
// document.write("<pre>");
for (var i= 1;i<=(a/2);i++) {
for (var s=(a/2); s>=i; s--) {
// document.write(" ");
document.getElementById("pr").innerHTML += " ";
}
for (var j= 1; j<(2*i); j++) {
// document.write(str.charAt(count));
document.getElementById("pr").innerHTML += str.charAt(count);
}
count++;
// document.write("\n");
document.getElementById("pr").innerHTML += "\n";
}
for (var p=1;p<=a+1;p++) {
// document.write(str.charAt(count));
document.getElementById("pr").innerHTML += str.charAt(count);
}
// document.write("<br>");
document.getElementById("pr").innerHTML += "\n";
var h=Number(1);
for (var d=1;d<=(a/2);d++) {
for (var e=1;e<=d;e++) {
// document.write(" ");
document.getElementById("pr").innerHTML += " ";
}
count--;
for (var f=(a-h);f>=1;f--) {
// document.write(str.charAt(count));
document.getElementById("pr").innerHTML += str.charAt(count);
}
h=h+2;
// document.write("\n");
document.getElementById("pr").innerHTML += "\n";
}
// document.write("</pre>");
}

function clearScreen() {
var sp= document.getElementById("ad");
var prr = document.getElementById("pr");
sp.removeChild(sp.childNodes[0]);
}

function numb(e, t) {
            try {
                if (window.event) {
                    var charCode = window.event.keyCode;
                }
                else if (e) {
                    var charCode = e.which;
                }
                else { return true; }
                if ((charCode > 47 && charCode < 58))
                    return true;
                else
                    return false;
            }
            catch (err) {
                alert(err.Description);
            }
        }

</script>
</head>
<body>
<div>
<form id="formd" method="get" action="JavaScript:makeDiamond()">
<label for="dia">Diamond No.:</label>
<input type="text" id="dia" maxlength="2" onkeypress="return numb()" />
</form>
<button type="submit" class="but" form="formd" value="submit">Make Diamonds</button>
<button type="button" class="but" value="clear" onclick="return clearScreen()">Clear Screen</button>
</div>
<span id="ad"></span>
</body>
</html>

PHP PROGRAM FOR PRINTING DIAMOND PATTERNS OF ALPHABETS VALIDATION FROM MYSQL DATABASE

This program uses MySQL database daidb having table tabdia having column numdia. This program verifies the input given is an even number between 2 and 24 from database and prints diamond pattern of alphabets as per the given input.



<html>
<head>
<style type="text/css">
.but {
border-radius: 20px;
width: 250px;
height: 40px;
font-weight: bolder;
font-size: 20px;
}
</style>
<body>
<form id="formd" method="get" action="<?php $_PHP_SELF ?>">
<label>Diamond No.:</label>
<input type="text" name="dia" maxlength="2" placeholder = "0" />
<button type="submit" class="but" form="formd" value="submit">Make Diamonds</button>
</form>
</body>
</html>

<?php
$val = $_GET["dia"];
/*if (($val<25) && ($val>1)) {
  $t = $val%2;
if ($t==0) {
drawDiamond($val);
}
else {
echo "Only EVEN numbers between 2 and 24 are allowed";
}
}
else {
echo "Only EVEN numbers between 2 and 24 are allowed";
}*/

$servername = "localhost";
$username = "username";
$password = "password";
$dbn = "diadb";
$conn = new mysqli($servername, $username, $password, $dbn);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "select * from tabdia where numdia = $val";
$result = $conn->query($sql);
if($result->num_rows ==0){
echo "Only EVEN numbers between 2 and 24 are allowed";
} else {
drawDiamond($val);
}
$conn->close();

function drawDiamond($a) {
$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$count= 0;
echo "<pre>";
for ($i= 1;$i<=($a/2);$i++) {
for ($s=($a/2); $s>=$i; $s--) {
print(" ");
}
for ($j= 1; $j<(2*$i); $j++) {
print("$str[$count]");
}
$count++;
print ("\n");
}
for ($p=1;$p<=$a+1;$p++) {
print("$str[$count]");
}
print("\n");
$h=1;
for ($d=1;$d<=($a/2);$d++) {
for ($e=1;$e<=$d;$e++) {
print(" ");
}
$count--;
for ($f=($a-$h);$f>=1;$f--) {
print("$str[$count]");
}
$h=$h+2;
print("\n");
}
echo "</pre>";
}
?>