Code With Design

Web development, code, design, css, all around programming blog

SQL – An introduction to Keys

SQL – An introduction to Keys

The Primary Key

Cells inside of your database are all located through an X-axis and a Y-axis. The X-axis is a row and the Y-axis is the column. Because the information inside of a database is stored downward as you go through the table each added record needs a unique identifier to be found. This is where the primary key comes in.

For every table in your database a unique ID is required to find the records and because of this every table has exactly one Primary Key. Without a primary key there would be no way to reference the information stored inside of that row and data would become inaccessible very quickly. This is also the reason why the primary key cannot have the value of “null.”

In a table there can never two of the same key. Two keys would conflict with each other and return bad records, which is why it is impossible to have this. Generally when working on a database you never want to modify the key after it has been created. This would change how the data is accessed and would be a bad idea when it comes to referencing the information should anything go wrong. There are two different kinds of keys. The first kind is a simple key and which is made of one column. The second kind of key is a composite key, which is made of 2 or more columns.

In most cases the Primary key is an integer. This allows simple ordering and counting of records on system however there are cases where a hex number can be used or even a pattern of numbers and letters.

For example, say that we were creating a database for items in a store. Each item has a unique SKU but this is something can change over time so we would still need our primary key to be a number that is completely unique. The naming also will not work for a unique ID due to name changes or the same product name with a change in SKU, which happens over time with different versions and packaging.

Foreign Keys

Foreign keys are used to reference objects or rows from other tables. Theses keys generally reference parent tables from child tables and are used to cross data between tables. This means that the table with the foreign key is always going to be the child in the link.

Unlike the primary key, the foreign key can have a null value, which leads to no link being made between the two tables for that object. These keys also do not need to be unique; in fact they often never are unique do to the nature of how they are used.

An example of the foreign key, continuing from the example used in primary keys, will require us to add another column to the initial table so we will now have ID, SKU, and Vendor. The tables will look like this.

As you can see, in this example we have the foreign key doubling as a primary key in the lower table, which is used in as a vendor in the top table. This is a quick and simple way to store the values easily in the database and comes in handy when working with relationships.

posted by Caleb in SQL and have No Responses

Creating multiple sidebar menus in wordpress

Creating multiple sidebar menus in wordpress

In this tutorial I won’t just be making a sidebar menu, but three additional menus that will be placed side by side in the footer. Unfortunately the code for this is not as easy as it may seem. The possibility of creating the sidebars through only your footer.php and sidebar.php is not going to happen. Fortunately the code is very simple and involves a lot of copy and pasting with a couple modifications.

For this tutorial I will be using wordpress 2.9.2 (currently the latest release)

When creating multiple “sidebars” You only need to do two things. The first thing is to edit the functions.php page, and the second is to edit the HTML where the sidebar/dynamic menu will be placed.

The first thing that we will be doing is opening our functions.php page. The code for one sidebar should look like this:

1
2
3
4
5
6
7
8
9
10
if ( function_exists('register_sidebar_widget') ){
    register_sidebar(array(
        'name'=> __('Sidebar', 'cwj'),
        'id' => 'normal_sidebar',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));
}

The next thing we will need to do is modify this code so we can have four dynamic menus throughout our Wordpress theme. We will need to copy and paste the array and change out the name and the ID to something new and exciting. Because the menus will be placed in my footer I will be calling them “Footer1″ etc for each menu. The ID can be anything as long as it is unique to that specific menu. If you have two menus with the same ID you will only be able to add widgets to the last referenced menu.

The code should now look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
if ( function_exists('register_sidebar_widget') ){
    register_sidebar(array(
        'name'=> __('Sidebar', 'CJDesign-dark1'),
        'id' => 'normal_sidebar',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));
    register_sidebar(array(
        'name'=> __('Footer1', 'CJDesign-dark1'),
        'id' => 'col_sidebar1',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));
    register_sidebar(array(
        'name'=> __('Footer2', 'CJDesign-dark1'),
        'id' => 'col_sidebar2',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));
    register_sidebar(array(
        'name'=> __('Footer3', 'CJDesign-dark1'),
        'id' => 'col_sidebar3',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget' => '</li>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>',
    ));
}

The next stage in this tutorial is to quickly and easily insert the line of code to display your newly created dynamic menus. Just place this into the HTML where you would like it to be.

1
2
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer3') ) : ?>
<?php endif; ?>

You simply need to replace where I have Footer3 with the name of the sidebar that you previously created with the function.

The sidebar now looks as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<div class="footer-col">
	<ul>
		<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer1') ) : ?>
  		<?php endif; ?>
    </ul>
</div>
<div class="footer-col">
	<ul>
		<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer2') ) : ?>
        <?php endif; ?>
    </ul>
</div>
<div class="footer-col">
	<ul>
		<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Footer3') ) : ?>
        <?php endif; ?>
    </ul>
</div>

And the widgets can be modified through the Wordpress widget page.

<img>

And the final product.

<img>

posted by Caleb in Web Development, php and have No Responses

Recycle pages by splitting your website into pieces with PHP

Recycle pages by splitting your website into pieces with PHP

Recycle pages by splitting your website into pieces with PHP from Calebj on Vimeo.

posted by Caleb in php, video and have No Responses

Remove images and increase load times with CSS3

Remove images and increase load times with CSS3

Recently when working on a clients site I found that there were a lot of images being used when a quick little CSS fix could have done the trick. The basic outline of the site involved a few transparencies and a lot of drop shadows. The drop shadows were placed on the container/wrap and the text had drop shadows that had been all done in Photoshop. (Some of the drop shadows were not consistent.)

After looking at the site I told the client that within three minutes I could remove all of the images being for text, drop shadows and opacity allowing his site to load faster.

The answer was what I had expected and the following code was quickly being thrown into the site.

The Code

For the drop shadows on divs:

1
2
3
4
5
6
7
8
.container{
     width:1080px;
     margin:0 auto;
     -moz-border-radius:5px;
     -webkit-border-radius:5px;
     -moz-box-shadow: 0 2px 5px #666;
     -webkit-box-shadow: 0 2px 5px #666;
}

For the drop shadows on text:

1
2
3
4
5
h3{
	font-family: Times;
	letter-spacing: 1px;
	text-shadow: 2px 2px 2px #000;
}

For the opacity:

1
2
3
4
5
.sidebar ul{
	list-style-type:none;
	opacity: 0.9;
	width: 180px;
}
posted by Caleb in CSS, Web Development and have No Responses

Writing and reading objects and arrays to a binary file with java

Writing and reading objects and arrays to a binary file with java

This article requires that you have read the following java tutorials or you feel comfortable writing and reading variables to a binary file.

Advantage of writing files in binary with java

Reading information from a binary file with java

Writing information to a binary file with java

The first thing that is needed in this tutorial is an object that we are going to save into binary. This object is going to be a simple object with one instance variable, two constructors, one of which is default and a get name method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.io.Serializable;
public class Person implements Serializable{
    private String name;
 
    public Person(){
        name = "no name";
    }
    public Person(String x){
        name = x;
    }
    public String getName(){
        return name;
    }
}

You may notice that at the top of our new object we have a line that says “import java.io.Serializable” This line imports a class that we can use. It is required when we want to turn an object into binary and save it to a file. There is also a line that implements Serializable. This line is needed so the class can use the methods within the class Serializable.

The next piece of code will be added outside of the main method. It is added because we will need to use the file input and output streams along with the object input and output streams. We will also be using an IOException. If you would like to use other exception handling classes you can certainly feel free too. I would advise using the file not found and end of file exception.

1
2
3
4
5
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;

This next bit of code is going to save what we want for a filename inside of a string variable. After that we will be creating two person objects and two arrays, one being a string type and the other an integer type. The next line of code will create a null output stream then create the output stream while testing it for any errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//the filename to create.
        String fileName = "file.dat";
 
        //Person Objects
        Person p1 = new Person("Bob");
        Person p2 = new Person("Paul");
 
        //Array to write
        String[] helloThere = new String[]{"dog","cat","hamburger"};
        int[] thereHello = new int[]{1,2,3,4,5,6,7,8,9};
 
        //creating the space in memory for the file
        ObjectOutputStream  outputStream = null;
 
        try{
            outputStream = new ObjectOutputStream(new FileOutputStream(fileName));
        }catch(IOException e){
            System.out.println("Could not open the file." + e);
            System.exit(0);
        }

Writing the objects to a binary file is done in a similar way to writing everything else. Since an array is actually an object it uses the same method as the object person. To write these two kinds of objects we will need to use the try catch in order to handle any errors then inside of that write objects using the outputstream.

1
2
3
4
5
6
7
8
9
10
11
12
try{
            outputStream.writeObject(p1);
            outputStream.writeObject(p2);
            outputStream.writeObject(helloThere);
            outputStream.writeObject(thereHello);
 
            outputStream.close();
 
        }catch(IOException e){
            System.out.println("Writing error: " + e);
            System.exit(0);
        }

Now that we are done writing the objects to the binary file we will need to read the information back into new objects.

We are going to create a new input stream in the same way that we made the output stream. It will first be of type null and then we will truly create it inside of the try catch blocks.

1
2
3
4
5
6
7
ObjectInputStream inputStream = null;
        try{
            inputStream = new ObjectInputStream(new FileInputStream(fileName));
        }catch(IOException e){
            System.out.println("There was a problem opening the file: " + e);
            System.exit(0);
        }

Since we are going to pull the information out of the file and reload them as objects we will need to create new objects in memory to show that we have successfully read the file.

1
2
3
4
5
//Create new blank objects to store the data in.
        Person newp1 = null;
        Person newp2 = null;
        String[] test = null;
        int[] test2 = null;

In order to read the information we will rely on our trusted try and catch blocks to let us test that everything is going as planned. Inside of the try catch blocks there is going to be the code required to load the objects read from memory into the new objects that we created in the code above.

You will also notice that when we saved the object information to a new object we specified what kind of object it was. This casts the information and sets it up as said object so it can once again use the methods that come along with that object type.

1
2
3
4
5
6
7
8
9
10
try{
            newp1 = (Person)inputStream.readObject();
            newp2 = (Person)inputStream.readObject();
            test = (String[])inputStream.readObject();
            test2 = (int[])inputStream.readObject();
            inputStream.close();
        }catch(Exception e){
            System.out.println("There was an issue reading from the file: " + e);
            System.exit(0);
        }

The last thing required is for us to do is test a person and an array. We will do this as follows.

1
2
System.out.println("newp1\'s name is " + newp1.getName());
        System.out.println(test2[0]);

If everything goes according to plan then we will have no errors. If you do have an error then please refer to the last bit of code to see if you did anything wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* Code with Design
 *
 * Created By: Caleb Jonasson
 *
 * Desc: This class file is used to create a binary file,
 * write values to the file and check for errors along the way.
 */
 
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
 
public class writebin {
 
    public static void main(String[] args) {
        //the filename to create.
        String fileName = "file.dat";
 
        //Person Objects
        Person p1 = new Person("Bob");
        Person p2 = new Person("Paul");
 
        //Array to write
        String[] helloThere = new String[]{"dog","cat","hamburger"};
        int[] thereHello = new int[]{1,2,3,4,5,6,7,8,9};
 
        //creating the space in memory for the file
        ObjectOutputStream  outputStream = null;
 
        try{
            outputStream = new ObjectOutputStream(new FileOutputStream(fileName));
        }catch(IOException e){
            System.out.println("Could not open the file." + e);
            System.exit(0);
        }
 
 
 
        try{
            outputStream.writeObject(p1);
            outputStream.writeObject(p2);
            outputStream.writeObject(helloThere);
            outputStream.writeObject(thereHello);
 
            outputStream.close();
 
        }catch(IOException e){
            System.out.println("Writing error: " + e);
            System.exit(0);
        }
        System.out.println("Records have been written to the file.");
 
 
        /*Reading the objects from memory.*/
 
        ObjectInputStream inputStream = null;
        try{
            inputStream = new ObjectInputStream(new FileInputStream(fileName));
        }catch(IOException e){
            System.out.println("There was a problem opening the file: " + e);
            System.exit(0);
        }
 
        //Create new blank objects to store the data in.
        Person newp1 = null;
        Person newp2 = null;
        String[] test = null;
        int[] test2 = null;
 
        try{
            newp1 = (Person)inputStream.readObject();
            newp2 = (Person)inputStream.readObject();
            test = (String[])inputStream.readObject();
            test2 = (int[])inputStream.readObject();
            inputStream.close();
        }catch(Exception e){
            System.out.println("There was an issue reading from the file: " + e);
            System.exit(0);
        }
        System.out.println("Reading of the file: " + fileName + " has been completed.");
 
        //Now we test to make sure that it worked correctly.
        System.out.println("newp1\'s name is " + newp1.getName());
        System.out.println(test2[0]);
 
    }
 
}
posted by Caleb in Java and have No Responses

Objective-C creating objects, methods and variables

Objective-C creating objects, methods and variables

In this tutorial we will be creating a console application to try every part of our code out. Please make sure that the console is not set to its default language, which is C and is instead set to foundation.

The first thing we need to do is create a no project with Xcode. I will be naming mine “test” but you really don’t need to follow along with that much detail since it is not needed at this stage with the level of coding we will be doing.

The next thing we will need to do is open up our “test.m” file through Xcode. This file contains the default main method but we are going to be creating objects and variables.

1
2
3
4
5
6
7
8
9
10
#import <Foundation/Foundation.h>
 
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
    // insert code here...
    NSLog(@"Hello, World!");
    [pool drain];
    return 0;
}

Now you are going to need to create an interface and an implementation of the class made in the interface. Luckily for us Xcode has an auto complete feature built in that look something like this.

Until we start modifying the information in the interface and implementation you can just leave it with its default values that are given when it auto completes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#import <Foundation/Foundation.h>
 
@interface <#class#> : <#superclass#>
{
	<#ivars#>
}
 
<#methods#>
 
@end
 
@implementation <#class#>
 
<#methods#>
 
@end
 
 
 
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
    // insert code here...
    NSLog(@"Hello, World!");
    [pool drain];
    return 0;
}

The next thing we will be doing is creating the actual object from our interface. This requires us to create an object name, which in this case will be a “Person”, and it also requires a super class, which we will send to “NSObject.”

1
2
@interface Person : NSObject
{

Note: It is standard and common practice to associate objects as follows: “PersonObjectSomething.” You will notice that when naming all of the words are capitalized. This is common practice when naming classes. When naming methods it is standard to leave that first letter as a lower case like so “thisTestMethod.”

The next things that are required are instance variables. These are variables/information that is associated with the object. For example if we created an object that was a cup then we would have instance variables for weight, liquid capacity, circumference of base, etc.

For our person object we will be creating the instance variables for age and height. Normally I would like to do a name but I will be leaving the string library for another tutorial.

When declaring instance variables, or any variables, you will need to create the data type first then give the variable a name. A simple example of this would be an integer data type named x as you commonly see in math. You can also assign the variable a value when it is first declared.

1
2
int x;
int y = 32;

Once we have declared our instance variables for person you should have something that looks like this.

1
2
3
4
5
6
7
8
#import <Foundation/Foundation.h>
 
@interface Person : NSObject
{
	int height; // in CM
	int age;
}
@end

Now that we have created our Person interface’ variables we will need to create methods. If you have never worked with methods you may know them by another name, which is a function. If you still haven’t worked with them then you should know that a method is essentially a small sub program running in a larger one. Methods naturally return a value as well however, you can change the method type to void and not have to worry about returning a value.

There are two kinds of basic methods used in programming languages. The first one is called an accessor method and the second one is called a mutator method. The accessor method simply accesses a variable from the object it is derived from and returns said variable. And the mutator changes the value of the variable in some way. For this program we will be using these two types along with another void type that will print information on the screen.

In order to create a method, we must first link to it in the interface section of the code so our object can use it with the variables it contains. For this program we will need to set the height and age, access the height and age, and print the information on the screen. Add this section of code to your interface.

1
2
3
4
5
6
7
-(void)setHeight: (int) x;
-(void)setAge: (int) x;
 
-(int)getHeight;
-(int)getAge;
 
-(void)printInfo;

You should now have an interface that looks like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#import <Foundation/Foundation.h>
 
@interface Person : NSObject
{
	int height; // in CM
	int age;
}
-(void)setHeight: (int) x;
-(void)setAge: (int) x;
 
-(int)getHeight;
-(int)getAge;
 
-(void)printInfo;
@end

The next thing we will need to do is create the body of our object. We can do this by filling out the implementation section of our code. As you can see in your Xcode’ code view we have a place for the class to be written and a place for the methods. We are going to change the class to “Person” and start writing in our accessor and mutator methods. When done we will have the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@implementation Person
 
-(void)setHeight: (int) x{
	height = x;
}
-(void)setAge: (int) x{
	age = x;
}
 
-(int)getHeight{
	return height;
}
-(int)getAge{
	return age;
}
-(void)printInfo{
	NSLog(@"height: %i age: %i",height, age);
}
@end

Now that we have created our methods and instance variables we will go ahead and create this object and put it into memory. The following code is what we currently have for a main method.

1
2
3
4
5
6
7
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
    NSLog(@"Hello, World!");
    [pool drain];
    return 0;
}

When creating objects there are two main terms you will need to familiarize yourself with. The first term is “init” Which of course stands for initialize. This is when you tell the machine that you are ready to use the object. The next term you should be familiar with is “alloc,” it is short for allocate. This is the process of freeing up memory in your ram for the storage of the object.

The code to initialize and allocate an object is written as follows.

1
2
3
Person *p1;
p1 = [Person alloc];
p1 = [p1 init];

In this process we first named the object p1 and assigned it enough space to hold the object “Person” in memory. The next line of code simply initializes the object so it is ready for our use.

When you are done using your object and you would like to delete it you must include an object release.

1
[p1 release];

In between creating the object in memory and removing it from memory we are going to add some code to set the variables, access the variables and display what the object holds on screen.

In order to display an object’s methods you will need to follow the form of writing the code as follows.

1
[p1 setAge:18];

You can see that the object is placed within square brackets and the first statement is the object name itself. The second statement accepts the method that you would like to call on then a colon and some parameters if you made a method that accepts arguments.

When using a method that returns a value it is possible to load the data type returned into another variable as long as it is of the same data type. We use an example of this with the following lines. You can also use a variable as an argument when setting an objects variable.

1
2
int g = [p1 getHeight];
[p1 setAge: g];

The entire program looks as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#import <Foundation/Foundation.h>
 
@interface Person : NSObject
{
	int height; // in CM
	int age;
}
-(void)setHeight: (int) x;
-(void)setAge: (int) x;
 
-(int)getHeight;
-(int)getAge;
 
-(void)printInfo;
@end
 
@implementation Person
 
-(void)setHeight: (int) x{
	height = x;
}
-(void)setAge: (int) x{
	age = x;
}
 
-(int)getHeight{
	return height;
}
-(int)getAge{
	return age;
}
-(void)printInfo{
	NSLog(@"height: %i age: %i",height, age);
}
@end
 
 
 
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	Person *p1;
 
	p1 = [Person alloc];
	p1 = [p1 init];
 
	[p1 setAge:18];
	[p1 setHeight:188];
	[p1 printInfo];
	int g = [p1 getHeight];
	[p1 setAge: g];
	[p1 printInfo];
 
	[p1 release];
    // insert code here...
    NSLog(@"Hello, World!");
    [pool drain];
    return 0;
}

If you have any questions or concerns please feel free to comment below.

posted by Caleb in objective-c and have (1) Response

Looking for Ideas

Looking for Ideas

Code With Design is looking for tutorial ideas. If you have anything you would like to know more about please go ahead and send me an email at: caleb [at] codewithdesign [dot] com. On a further note; if you are interested in writing coding tutorials for codewithdesign.com you can also send me an email.

posted by Caleb in Random Posting and have No Responses