Tuesday, August 4, 2015

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>";
}
?>

No comments:

Post a Comment