Blog

From the minds of Division Overlay. Learn new techniques with our well formated tutorials, and get project updates all right here.

Archive for the ‘Uncategorized’ Category

Creating a basic View Counter using PHP and MySQL

Friday, January 1st, 2010

PlaceHolder
Creating a basic View Counter using PHP and MySQL
on January 1, 2010

Create a simple view counter using PHP and MySQL.

This tutorial relies on server side programming languages. To begin, be sure you have XAMPP or a similar localhost software running for testing purposes.
Get Xampp

In this tutorial, you will learn how to make a view counter using a few lines of PHP and MySQL. We will also be playing around in phpMyAdmin to create a basic table. This is not a real use case for this technique, you will most likely be adding this to a table on the database that contains profile information or a similar table.

Step 1

We begin the tutorial with a basic HTML page, with a PHP brackets in topmost part of the document. This is where we will place the database connection information. Your localhost may be set up differently than this, please familiarize yourself with your username and password of your server.

Step 2

In good practice, declare variables with data that will be used more than once. This includes “host” which in our case is localhost. This will differ if you are using a live server, or are using this on an application in a website. “username” is the username for the server. Default username for localhost is ‘root’. “password” is naturally the password to the server, again default for local host is ‘none (represented with empty quotes)’. “dbname” should contain the name of the database you are trying to reach. In this case we have created a database named ‘views’. We will go further into this in a moment. ”tblname” represents the table inside of the database that contains the information we desire, in our case we named it ‘counter’.

Step 3

Its time to open your phpMyAmdin. This is where we can access, and manipulate database data. Depending on software and platform, your URL may be different. (http://localhost/phpmyadmin/).
Find the section that allows you to create a new database, shown below. We will be making a new database named “views” as we have coded into the variables in step 2.

Step 4

Now, we will need to add a table and a row of data. Start off by making a table and naming it “counter” as we have coded in the variables on step 2. Then we will need 1 field of data on this table. (As mentioned before, this is not a real life use case. In actual use, you would add a field to an existing table.)

Step 5

After creating a new table, you will be presented with the field form. The way you make this is really up to you, for this tutorial we will just do a couple of things. The filed is named “hits” and the length is set to “5” the highest number this counter can hit will be ’99999′.

Step 6

There are several ways you can do this through PHP, for this tutorial we will do it through phpMyAmdin. Insert a new row, and set the initial value to ’0′. This will ensure the counter will start at 0 and the first view will be counted as 1.

Step 7

We are now done with phpMyAdmin. Lets get back to the code. We have our database set up, and we have all our variables with the names set up. Now we need top connect to the server and database.
To do this, we create a new variable named “con” for ‘connection’. The name of this variable is not important, however you should have a clear idea of what variable is when looking at the name.

mysql_connect()

this function we will use to contact the server. Inside the parenthesis is where the variables containing the server details go.

“$host”, “$username”, “$password”

After the parenthesis, to make sure nothing executes unless the server is connected add the function ‘die()’ This will stop the script from running any further.
We also need to connect to the correct database, in our case the name is ‘views’. To do this is similar to connecting to the server.

mysql_select_db()

Inside the parenthesis is where the variable containing the name of the database goes “$db_name”. Just like above, use the ‘die()’ function to stop script execution if there is an error connecting.

Full code for connecting to server and database

< ?php
$host="localhost";
$username="root";
$password="";
$dbname="views";
$tblname="counter";

$con = mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select DB");
?>

Step 8

Now that we are all connected, we are ready to start the fun part. Obtaining data from the database, displaying it, and modifying it. To do this we first create a variable named ‘query’.
Inside the variable we access the database using SQL – mysql_query()
“SELECT hits FROM $tbl_name”
This will select the row ‘hits’ from the table ‘counter’.

Then we need to count how many rows are in the ‘hits’ block. Create a new variable named ‘numrows’.

$numrows = mysql_num_rows($query);

Step 9

Next we add an if-while loop.This will make sure the variable numrows has data in it, and to store that data in variables as needed.

if($numrows !=0) {
while ( $row = mysql_fetch_assoc($query)) {
$views = $row['hits'];
}
}

This grabs the data inside the query, and stores them all in an array variable named ‘row’. We want to be a little more specific, so we add a variable named views and have it be the same as the hits-row.

Step 10

At this point the information is in the script, you just need to display it. To do this, we are going to add a print() function. Adding the variable to the print data so it is on the screen.

print(“There has been $views on this page”);

Step 11

Everything works, and now we need to count up and add the new number to the database. This part is quite simple. Take the variable ‘views’ and add 1 to it on every page load.

$views = $views + 1;

We then need to put the new number in the database. To do this requires a little bit of SQL.
Use the same ‘query’ variable and structure as we have above.

$query = mysql_query()

The SQL for this action is a little different than selecting. To place a new value in the database requires the following query “UPDATE $tblname SET hits=’$views’”
This will update the ‘counter’ table and set the row ‘hits’ with the value of the variable ‘views’.

Full code in body section

< ?php
$query = mysql_query("SELECT hits FROM $tblname");
$numrows = mysql_num_rows($query);

if($numrows !=0) {

while ( $row = mysql_fetch_assoc($query)) {
$views = $row['hits'];
}
}
print("There has been $views on this page");

$views = $views + 1;
$query = mysql_query("UPDATE $tblname SET hits='$views'");
?>

Final

Your view counter is done, Watch as it counts up. Questions and comments will be answered below. Thanks for reading the tutorial.

Posted in Uncategorized | 4 Comments »

Happy Holidays!

Saturday, November 28th, 2009

PlaceHolder
Happy Hollidays!
on November 27, 2009

Happy Holidays from Division Overlay. 40% off of all design packages in the month of December to celebrate the holiday and our second successful year of operation. Also, take a sneak peek at the new Echo Suite service.

Seasons greetings from Division Overlay! It is a joyous occasion. We are approaching our second full year of operation. Not only that mile stone, it is the season of gift giving. If you look on our home page http://divisionoverlay.net/ you will see a very special offer for the month of December.

Holiday Savings

We are offering 40% off of all design packages in the month of December. Starting December 1st at Midnight and running through December 31st at 11:55pm. This sale includes but is not limited to Logo design, UI design, HTML mockups, Icon design, and much more. However the sale does not include, Consultations, any development work (PHP, JavaScript, XML), and Hosting.

Echo Suite Teaser

Also this month, we are launching Echo Suite into beta. You will be able to register and use the Suite starting this December (Date TBD). There is a teaser graphic also on our homepage (http://divisionoverlay.net). There will be a separate blog post about Echo Suite when it is released.

Posted in Uncategorized | 2 Comments »

Updated Engine and Theme

Thursday, November 19th, 2009

PlaceHolder
Updated Engine and Theme
on November 19th 2009

Welcome back to the blog! We have been working on a few new features. We have also upgraded the engine behind the blog (wordpress) and optimized it by moderating plugins. Read on to learn what is new.

Hello and welcome back to the blog. We are glad to announce the updated theme is up and functional. What is new? (Click Images to enlarge)


“New” post ribbon displayed on new posts. The layout for post previews are now handled with wordpress excerpts. We have added an icon representing the post topic and a brief summary of the article which will be displayed on all new posts.


On all new blog entries, the title, post image and the post summary will all be displayed at the top in a container for quick viewing.


Now, on tutorials, instead of giving boring text links to downloads and demos we now added buttons. Clear and centered in the middle of the blog post.


Author information and bio has also been implemented. Not a big feature, just giving the blog posts a little more personality.

Other News

On the development side, We have optimized the code slightly for the blog posts and home screen. We have also deactivated all the useless plugins that were installed, and updated the ones still in use.
The blog is also on the latest version of wordpress (2.8.6). That ensures that we have a more secure and bug-less operating system.

There are currently 3 new blog posts in draft, 2 of which are tutorials and the other being an inspiration article. We hope to get those up progressively before the end of the year.

We are also working on a few things for holiday release. Here is a little sneak peek of some of the things we are working on.

Echo Suite is a new web service created by Division Overlay. It will feature many different services combined into a central station where you will be able to use one ID to login to all the different apps.

Watch for this little guy on the home page (here) within the next 2 weeks. There is a special offer in the air…

We are also looking for people who want to by ad space (left sidebar on blog) if you are intrested, contact us (here).

So please leave a comment below and tell us what you think about the new features and coming attractions from Division Overlay.

Posted in Uncategorized | No Comments »