Skip to main content

Posts

Showing posts from 2015

Setting up a database connection (Java+mysql)

Initially, it is essential to set up the database connection. STEP 1: Create a database in phpmyadmin and create the required table. STEP 2: Set up a new connection in Net Beans. 1. Right Click on Databases (Services tab) -> Select New Connection 2. Select MySQL  -> Select Next 3. Enter the name of your database -> TEST   CONNECTION 4. If you see the below added to your services, then the connection has been set up successfully. STEP 3: Add a library to the net beans project as below. STEP 4:     Creating the Database Connect Class 1. Create a new package under the project and create a new class called ' DBconnect ' under that package. 2. DBconnect class should look like below.  In the line,         con = (Connection) DriverManager.getConnection(" jdbc:mysql://localhost:3306/employee ","root",""); The highlighted path can be optained from the services pane where we es

Implementing Data Structures using Java - Stacks

The Stack Data Structure follows the principle of Last In First Out which means the last element inserted is the one to be taken out first.  Real world example would be a stack of CDs. To take out the bottom one you will have to remove the CDs on top of it. And in this Data Structure you have to remove each element one by one. This example is implemented using an array, hence contains elements of the same data type only. A Stack comes in handy when you need to reverse a character array and this will show you how. STEP 1: The Stack class should be implemented as below. STEP 2: The GUI should be designed as preferred. Then, the Stack is created dynamically in case it needs to be accessed in different events and for the button click the below code should be added. STEP 3: An object of the JFrame Class (GUI) should be created in the main program and the visibility should be set to true. Now, you can run the program and use it to reverse any string.

Admin panel of a Q & A Forum

In a Q & A Forum, when a user posts a question, it should be sent to the administrator for approval in case it contains inappropriate content. After approval it should be removed from this pending approval page and other users should be able to see the question afterwards. To enable this, we should maintain an approval column in our database table of records and for each record approval should be set to false by default. In the Pending approvals page only the records with approval=false should be displayed. Below is  the MySQL  statement for retrieval, $sql="SELECT * FROM topics WHERE approval=false"; To know which post was approved we should embed the post_id to the URL. And the relevant post should be updated as approval=true. Below is the complete code. <?php $sql="SELECT * FROM topics WHERE approval=false"; $query=mysqli_query($conn,$sql); echo '<form name="approve" method="p

Rating Counter using PHP

First, we should maintain the rate count in the database separately  for each record (whichever we are going to rate). For this we can use a separate column for this, which will be referred to as  rating  in this post. Basically, what is to be done is to increment the rating, which should be 0 by default, by one each time the +1 button is clicked and to decrement when the -1 button is clicked. if(isset($_GET['postIdPlus'])) //if plus button is clicked { $sql1="UPDATE posts SET rating=rating+1"; $r esult1=mysqli_query($conn,$sql1); } if(isset($_GET['postIdMinus']))   //if minus button is clicked { $sql1="UPDATE posts SET rating=rating-1"; $result1=mysqli_query($conn,$sql1); } When there are many posts, we should distinguish which post was rated. For this purpose, we shall embed the post_id (retrieved from the database along with the other details) to the URL.  echo " <a hre

To restrict access to certain users

In a website, we need to restrict views to users. As an instance, Admin panel should be accessible for only the administrators. <?php session_start();  if(!empty($_SESSION['login_user'])) // if a user is logged in { $user=$_SESSION['login_user']; include './dbconnect.php'; $sql3="select user_level from users where user_name='$user'";  // checking  the user level of the user logged in $result3 = mysqli_query($conn,$sql3) or die (mysqli_error($conn)); if(mysqli_num_rows($result3) > 0) { while($row3=mysqli_fetch_array($result3)) { $userlevel=$row3['user_level']; } // if the user is not an admin, to redirect to the home page if($userlevel!=1) { echo '<script type="text/javascript"> location="index.php"; alert("You do not have access"); </script>'; } } } // if a user is not logged in else{ echo '<sc

Creating a Log In/Sign Up page from Scratch using php

HTML Sign-up form <div id="signup-form">                             <form name="f1" method="post" action="<?php $_SERVER['PHP_SELF']?>" onSubmit="return validate_signup()" >                                 <table>                                     <br><br>                                     <tr><td><input type="text" name="name" id="name" placeholder="Name"></td></tr><br>                                     <tr><td><input type="email" id="mail" name="mail" placeholder="E-mail"><br></td></tr>                                     <tr><td><input type="text" id="username" name="username" placeholder="Username"><br></td></tr>              

Calculator using PHP

This Calculator model will take inputs from the Number 1 and Number 2 fields and when the user clicks on the relevant operator the result will be displayed in the Results field. For log10(), to radian, to degree, sin, cos, tan operations only require one input. Hence, the user is instructed to input the values to the 1st field only. First, before proceeding with the calculation, we need to obtain the values from the text boxes. For that we should include all the form elements inside a form. The result is directed to the same page. Therefore we will use the form action as $_SERVER['PHP_SELF'] and the method as post. Next, we can obtain the values in the text boxes.       $_POST[' form_element_name '] will give you the value of the respective element. We can write the php code as follows (in the <head>) to obtain the value from Number 1 and Number 2 fields.       <?php              $num1=$_POST['num1']; //num1 is the name of th

GPA Calculator using C++

SOURCE CODE: private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) { String^ pgrd1=(textBox1->Text); String^ pgrd2=(textBox2->Text); String^ pgrd3=(textBox3->Text); String^ pgrd4=(textBox4->Text); String^ pgrd5=(textBox5->Text); float GPA1,GPA2,GPA3,GPA4,GPA5; float GPA=0; if (pgrd1==""||pgrd2==""||pgrd3==""||pgrd4==""||pgrd5=="") { MessageBox::Show("Fields cannot be empty!"); } else { //***************************************1st subject***************************************************// if (pgrd1=="A+"||pgrd1=="A") { GPA1=4.0; } else if (pgrd1=="A-") { GPA1=3.7; } else if (pgrd1=="B+") { GPA1=3.3; } else if (pgrd1=="B") { GPA1=3.0; } else if (pgrd1==&quo