Thursday, May 4, 2017

Switch Statement in PHP

In this article I would like to share with you guys how to use switch statement in PHP. The code is very short and easy to understand. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.



Program Listing

<?php


$year_level = '4';


switch($year_level) {
case "1" :
   echo "Your are belong to Freshmen.";
break;
case "2" :
   echo "Your are belong to Sophomore.";
break;
case "3" :
   echo "Your are belong to Juniors.";
break;
case "4" :
   echo "Your are belong to Seniors.";
break;
default:
        echo "Invalid year level. Try again.";
}

?>

Multi Level User Login And Registration System in PHP and MySQL

Here is a sample program that I wrote using PHP and MySQL that allows the user to login in different user levels or rules in the system. The code is very compatible with PHP 7 database connections and queries.  I hope you will find my work useful. Thank you very much.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.













Sample Program Output



Program Listing

/* Written By: Mr. Jake Rodriguez Pomperada, MAED-IT             */
/* Tools : PHP / MySQL                                                                    */
/* May 4, 2017   Thursday                                                                */
/* jakerpomperada@yahoo.com and jakerpomperada@gmail.com */


connect.php

<?php
$username="root";  
$password="";  
$hostname = "localhost";  
//connection string with database  
$dbhandle = mysqli_connect($hostname, $username, $password)  
or die("Unable to connect to MySQL");  
echo "";  
// connect with database  
$selected = mysqli_select_db($dbhandle, "levels")  
or die("Could not select examples");  

?>


index.php

<?php include("header.php"); ?>
  <body>
    <div class="container">    
        <div id="loginbox" style="margin-top:50px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">                    
            <div class="panel panel-info" >
                    <div class="panel-heading">
                        <div class="panel-title">Login Security Page</div>
                      
                    </div>     

                    <div style="padding-top:30px" class="panel-body" >

                        <div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>
                            
                        <form action="login_action.php" method="post">
                                    
                            <div style="margin-bottom: 25px" class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                                        <input id="login-username" type="text" class="form-control" name="username" value="" placeholder="username">                                        
                                    </div>
                                
                            <div style="margin-bottom: 25px" class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                                        <input id="login-password" type="password" class="form-control" name="password" placeholder="password">
                                    </div>

                                <div style="margin-top:10px" class="form-group">
                                    <!-- Button -->

                                    <div class="col-sm-12 controls">
                                
<input type="button" class="btn btn-success pull-right" style='margin-left:25px' 
 value="Register"  title="Click here to register to the system." onclick="location.href = 'register.php';">            
 <button type="submit" class="btn btn-success pull-right" title="Click here to Login in the system." >
 <span class="glyphicon glyphicon-check"></span> Login</button>
    
                                     
  </div>
</div>
</form>
                         </div>
</div>
</div> 
</div> <!-- /container -->

    <script src="js/ie10-viewport-bug-workaround.js"></script>
  </body>
</html>


login_action.php

<?php
include("connect.php"); 
$tbl_name="user_levels"; 

$username=$_POST['username']; 
$password=$_POST['password']; 

$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($dbhandle,$username);
$password = mysqli_real_escape_string($dbhandle,$password);

$result = mysqli_query($dbhandle, "SELECT * FROM $tbl_name WHERE username='$username' AND password='$password'");

if(mysqli_num_rows($result) != 1){
echo "<script>alert(' Wrong Username or Password Access Denied !!! Try Again');
window.location='index.php';
</script>";
}else{
$row = mysqli_fetch_assoc($result);
if($row['userlevel'] == 1){
header('location: admin.php');
}else if($row['userlevel'] == 2 ){
header("Location: faculty.php");
}else if($row['userlevel'] == 3 ){
header("Location: student.php");
}
else if($row['userlevel'] == 4 ){
    header("Location: staff.php");
}
else{
echo "<script>alert('Wrong Username or Password Access Denied !!! Try Again');
window.location='index.php';
</script>";
}
}

?>


insert.php


<?php

include("connect.php"); 
$tbl_name="user_levels"; 

$username=$_POST['username']; 
$password=$_POST['password']; 
$user_level = $_POST['user_level']; 

$result = mysqli_query($dbhandle, "INSERT INTO $tbl_name (username,password,userlevel) VALUES ('$username','$password','$user_level')");


if($result===TRUE)
{
echo "<script>alert('User Account has been saved in the database.');
window.location='index.php';
</script>";
}
else
{
  echo"The query did not run";
mysqli_close($result);


?>

register.php

<html>
 <?php include("header.php"); ?>
  <body>
    <div class="container">    
        <div id="loginbox" style="margin-top:50px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">                    
            <div class="panel panel-info" >
                    <div class="panel-heading">
                        <div class="panel-title">Registration Page</div>
                      
                    </div>     

                    <div style="padding-top:30px" class="panel-body" >

                        <div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>
                            
                        <form action="insert.php" method="post">
                                    
                            <div style="margin-bottom: 25px" class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                                        <input id="login-username" type="text" class="form-control" name="username" value="" placeholder="username"  autofocus required>                                        
                                    </div>
                                
                            <div style="margin-bottom: 25px" class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                                        <input id="login-password" type="password" class="form-control" name="password" placeholder="password" required>
                                    </div>
 <div style="margin-bottom: 25px" class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                                        <input id="login-user_level" type="text" class="form-control" name="user_level" placeholder="user level" required>
                                    </div>

                                <div style="margin-top:10px" class="form-group">
                                    <!-- Button -->

                                    <div class="col-sm-12 controls">
                                
<input type="button" class="btn btn-success pull-right" style='margin-left:25px' value="Cancel" 
title="Click to return to main page." onclick="location.href = 'index.php';">            
 <button type="submit" class="btn btn-success pull-right" title="Click here to save the records in the database." >
 <span class="glyphicon glyphicon-check"></span> Ok</button>
    
                                     
  </div>
</div>
</form>
                         </div>
</div>
</div> 
</div> <!-- /container -->


 </html>

header.php

<?php include("connect.php"); ?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">
    <title>User - Levels</title>

    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="signin.css" rel="stylesheet">
    <script src="js/ie-emulation-modes-warning.js"></script>

  </head>

  admin.php

  <html>
  <head>
    <title> Administrator Page </title>
 <head>
  <style>
    body {
 font-family:arial;
 background-color : lightblue;
 color:red;
};
</style>
<body><br>
 <h1 align="center">
  Welcome To Administrator Page 
  </h1>
 </body>
 </html>

 faculty.php

 <html>
  <head>
    <title> Administrator Page </title>
 <head>
  <style>
    body {
 font-family:arial;
 background-color : lightblue;
 color:red;
};
</style>
<body><br>
 <h1 align="center">
  Welcome To Administrator Page 
  </h1>
 </body>
 </html>

 student.php

 <html>
  <head>
    <title> Student Page </title>
 <head>
  <style>
    body {
 font-family:arial;
 background-color : yellow;
 color:blue;
};
</style>
<body><br>
 <h1 align="center">
  Welcome To Student Page 
  </h1>
 </body>
 </html>

 staff.php

 <html>
  <head>
    <title> Staff Page </title>
 <head>
  <style>
    body {
 font-family:arial;
 background-color : pink;
 color:yellow;
};
</style>
<body><br>
 <h1 align="center">
  Welcome To Staff Page 
  </h1>
 </body>
 </html>


user_levels.sql

-- phpMyAdmin SQL Dump
-- version 4.6.5.2
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: May 03, 2017 at 01:52 PM
-- Server version: 10.1.21-MariaDB
-- PHP Version: 7.0.15

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `levels`
--

-- --------------------------------------------------------

--
-- Table structure for table `user_levels`
--

CREATE TABLE `user_levels` (
  `id` int(11) NOT NULL,
  `username` varchar(20) NOT NULL,
  `password` varchar(20) NOT NULL,
  `userlevel` varchar(20) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `user_levels`
--

INSERT INTO `user_levels` (`id`, `username`, `password`, `userlevel`) VALUES
(1, 'user1', 'user1', '1'),
(2, 'user2', 'user2', '2'),
(3, 'user3', 'user3', '3'),
(4, 'user4', 'user4', '4'),
(5, 'admin', 'admin', '1'),
(6, 'a', 'a', '1'),
(8, 'adam', 'adam', '2'),
(9, 'jake', 'jake', '3'),
(10, 'jacob', 'jacob', '2'),
(11, 'mark', 'mark', '4'),
(12, 'jose', 'jose', '2'),
(13, 'mario', 'mario', '2');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `user_levels`
--
ALTER TABLE `user_levels`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `user_levels`
--
ALTER TABLE `user_levels`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=14;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;


















Monday, May 1, 2017

Word Count in Visual Basic NET

In this article I would like to share with you a sample program that will ask the user to give a sentence or string and then our program will count the number of words in the given word or sentence using Microsoft Visual Basic NET.  The source code is very short and very easy to understand.  Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.






Sample Program Listing


Program Listing

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim strInput As String
        strInput = TextBox1.Text
        Dim strSplit() As String
        strSplit = strInput.Split(CChar(" "))
        Label2.Text = "Number of words: " & strSplit.Length
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        TextBox1.Text = ""
        Label2.Text = ""
        TextBox1.Focus()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        End
    End Sub
End Class