Home > Monthly Archives: February 2012
Page 1

PHP Ad Tracker Part IV: Displaying and Linking Ads

In our last session, we looked at the process of entering information on ad banners and ad clients, as well as generating ad activity reports. This week, we examine how to retrieve a random banner ad. We will also learn how to delete, activate and deactivate ads and clients.

The getRandomAd() function retrieves a random ad by using the PHP rand() function. The function returns the ad’s primary key ID, client ID, title, URL and redirect path.

The function also updates the activity table for this banner ad. If no entry in the activity table exists for this ad (i.e. this is the first time this ad has appeared), the function inserts a new record for this ad into the activity table.

function getRandomAd() { $sql = "SELECT ad_id, ad_client_id, ad_title, ad_url, ad_path FROM devdrive_ads WHERE status=1 AND deleted=0 ORDER BY rand() LIMIT 0, 1"; if (DB::isError($rsTmp = $this->_oConn->query($sql))) { catchExc($rsTmp->getMessage()); return false; } // assign result to array $aRow = $rsTmp->fetchRow(DB_FETCHMODE_ASSOC); // assign return array values $return["Ad Id"] = $aRow["ad_id"]; $return["Client Id"] = $aRow["ad_client_id"]; $return["Title"] = $aRow["ad_title"]; $return["URL"] = $aRow["ad_url"]; $return["Path"] = $aRow["ad_path"]; // update advertisement activity $sql = "UPDATE devdrive_ads_activity SET ad_view_cnt=ad_view_cnt+1, ad_activity_month=".date("m").", ad_activity_year=".date("Y")." WHERE ad_id=".$aRow["ad_id"]." AND ad_activity_month=".date("m")."...
more →
Mattias Geniar says: Please consider avoiding the use of 'ORDER BY rand()' in queries. It is far more effective to generate a random integer in PHP...

What Can You Do With Paper.js?

There are many JavaScript frameworks that leverage HTML5.

Paper.js is one of these frameworks that uses Document Object Model (DOM) to structure objects in an easy-to-understand manner.  It offers creative and neat ways of doing lots of stuff on a Web browser that supports the <canvas> tag. It also offers a new and interesting approach to drawing vector graphics.

The basic setup is shown below:

<script src="js/paper.js" type="text/javascript"></script> <script src="js/main.js" type="text/paperscript"></script> <canvas id="draw" width="100%" height="100%" resize="true"></canvas>

As you can see, the paper.js is included after which you add your code file under “type=”text/….” After passing the canvas element id that needs to be drawn and ensuring that your code file includes all the classes and functionality to be used with paper.js, then the rest is a matter of being creative while you leave the rest to paper.js.

Working with Predefined Shapes

Paper.js allows you to use predefined shapes of varying dimensions and create path items and segments. For example, the code below produces circle-shaped paths from the “Circle” constructor:

var myCircle = new Path.Circle(new Point(300, 70), 50); myCircle.fillColor = 'black';

This piece of code creates a black circle with a radius of 50pts and at the x position of 300 and a y position of y.

To create a rectangle, you pass the “Rectangle” constructor the same way as a circle as shown below:

var rectangle = new Rectangle(new Point(50, 50), new Point(150, 100)); var path = new Path.Rectangle(rectangle); path.fillColor = '#e9e9ff'; path.selected = true;

Creating Interaction

Paper.js also supports drag n drop functionality as well as keyboard strokes. An empty path is created on execution where segments can also be added as shown below:

// Create a new path once, when the script is executed: var myPath = new Path(); myPath.strokeColor = 'black'; // This function is called whenever the user // clicks the mouse in the view: function onMouseDown(event) { // If the path is empty, we need to add two segments // to the path. The first one will stay put, // and the second...
more →
David Gitonga says: Hi Rokcy. Here is the demo link. http://paperjs.org/examples/ The link contains different animations created using Paperjs.

Developing a Responsive Website: Secondary Page Part 2

We’re going to wrap up our tutorial on how to develop a responsive website this week by making our secondary page, well, responsive.

We created our large layout for the page in our last tutorial, but now we want to make it fluid so that it will display nicely across various platforms, ranging from tablets and mobile devices to PC’s. 

Let’s take a quick peak at what we’re working towards.

Notice how once we hit the skinnier, mobile version of the site we go to a more vertical layout.  This makes it easier to read the text as it allows someone browsing the page from their touch-screen mobile device to navigate the page with one hand, as they can hold the device in their palm while vertically scrolling with their thumb.

The first order of business is to set some parameters for our main “services” id section under the landscape tablet media query.  We want to use our medium sized image to cut down on load time.  So, under the Tablet Layout: 768px  @media only screen and (max-width: 1024px) and (orientation:landscape), I have added the following bit of CSS.

#services { background: url(images/servicesMed.jpg) no-repeat fixed; background-position:center; background-size: cover; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; }

Once we have shrunk our background image down a bit we’re going to want to do the same with our services bubbles, and all of the content within the bubbles.  You’ll notice that I also changed the margins up a bit to accommodate the smaller sized circles.

.servCats { width:100px; height:100px; margin-left:12%; } .servCats p { width:100px; text-align:center; margin-top:30px; } .servCats a { font-size:1.2em; }

That does it for the landscape tablet view, so now let’s scroll down a little further in our main.css file and tweak the portrait view.  This will be the exact same code as above.

#services { background: url(images/servicesMed.jpg) no-repeat fixed; background-position:center; background-size: cover; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; } .servCats { width:100px; height:100px; margin-left:12%; } .servCats p { width:100px; text-align:center; margin-top:30px; } .servCats a { font-size:1.2em; }

Last up on the docket is our mobile media query.  What we’re going to do here is take our two “servColumn” elements and display them one on top of the other, instead of side-by-side like they are in the larger formats of our design.

Displaying these elements vertically like this will make the content much easier to read on mobile device.  To maintain the same appearance as we did in our larger design we would have had to shrink the text so small that it was unreadable, or if we maintained a reasonable size font we would have averaged a word or two per line.

First things first, go ahead and define the small image for the mobile query.

#services { background: url(images/servicesSmall.jpg)...
more →
Bikebone says: Thank you very much for the well-written tutorial. How about a link/links to the other articles like here:...

How to Create and Use WordPress Custom Menus

WordPress 3.0 introduced users to a powerful feature: custom menus.

Now, without the need for plugins, we can exclude, include, and rearrange our page links at will, displaying a different menu for different pages, and with some code, even different users entirely.

How to Create a Custom WordPress Menu

Creating a custom menu is simple. Log into your WordPress Administrative Dashboard and select “Menus” under the appearance tab. From here, select the “Menu Name” input and give your new custom menu a title. Anything works, but let’s use “Main Menu” for the purposes of this tutorial. Once you’ve typed that in, hit the blue “Create Menu” button.

Alright, now we’ve got a new custom menu setup, but we need to add some links. There are three types of things we can add to our menu: Custom Links, Pages, and Categories. Custom Links let you link to external pages (great for incorporating network websites, related projects, and affiliate links). Pages lets you add WordPress pages to the menu. Categories lets you add links to a specific category archive.

To add a custom link, type in the destination URL then assign the menu item a label (the text that will appear in your navigation menu for that URL). Finally, click “Add to menu.”

To add a page or category, toggle the checkbox next to the item you want to add, then hit the “add to menu” button.

To modify a navigation item’s label, simply click on the item to expand its options, then change the “navigation label” input to whatever text you want to appear on your website. You can also assign each menu link a title attribute (text that appears when you hover over a link) for added SEO and usability benefit.

Tip: automatically include any new parent pages you make by checking the “automatically add top level pages” box next to the menu title.

How to Create Drop-Down Menus with WordPress

If your theme supports drop-down menus, you can easily arrange parents and children by simply clicking and dragging menu items into an indented position. Similar to moving widgets into a widget area or moving around boxes in the admin dashboard, gray rectangles will appear beneath each menu item, creating a position for you to drop the menu item into. From there, your theme will generate the necessary code for drop-down positioning between parent and child.

How to Use Custom WordPress Menus

Now that you’ve created your menu, you have to assign it to a location on your website. This is where the “theme locations” tab comes into play. Depending on the theme you’re using, there could be just one (twenty-eleven has just a main menu area) or multiple (headway themes gives you the ability to add as many navigation menus as you want on your website).

Choose the area you want to assign your menu to, and click the dropdown field. It will display all of the custom menus you’ve created. Select the menu you wish to assign (in our case, we only have Main Menu created, so we’ll use this one) and then hit the “save” button.

Theme locations that have no menu assigned will, by default, show all public pages that have been published.

How to Make Your Theme Custom Menu Compatible

When developing your own themes, developing your own theme locations could be important to the creation of your theme. The basics of integrating menu support involve defining menus (the name of our theme locations) and then calling them in our templates.

First, let’s define some menus. Add the following code to your theme’s functions.php file:

function register_my_menus() { register_nav_menus( array( 'top-menu' => __( 'Top Menu' ), 'footer-menu' => __( 'Footer Menu' ) ) ); } add_action( 'init', 'register_my_menus' );

This function assumes that we’re going to be building two menus, one for the main navigation and one for the footer. Now if we were to call the menu in our header.php or footer.php files, they would look like this:

<?php wp_nav_menu( array( 'theme_location' => 'top-menu' )); ?>

or

<?php wp_nav_menu( array( 'theme_location' => 'footer-menu' )); ?>

Creating your own menu locations for themes can be used in a variety of ways, especially if WordPress conditional tags are employed. For example, let’s say we wanted a user to see a menu only if they’re logged in. We could place the following code in the header.php file:

<?php if ( is_user_logged_in() ) { wp_nav_menu(array('theme_location'=>'user-menu')); } else { wp_nav_menu(array('theme_location'=>'main-menu')); } ?>

Conditional tags offer us the ability to show different menus on all kinds of system pages like archives, categories, specific pages, etc. You can find a list of conditional tags on the WordPress Codex.

How are you using custom menus to enhance your WordPress theme? What questions do you have about developing with custom menus? Share your thoughts and experiences in the comments below.

...
more →
Steve says: I would like the top level item (the one which appears in the menu bar) to not be a link or a page of any kind, but just a title...

Skewing Web Page Elements Using The CSS3 Skew Transform

With CSS3, you can transform the appearance of Web page elements. The skew transform allows you to skew a particular element or group of elements, by supplying a number of degrees to skew along the X and Y axes.

Your code needs to be tailored to the specific browsers supporting CSS3 and you do, of course, need to remember that not all browsers support these properties yet.

The CSS3 skew transform can execute as the user interacts with your pages, and you can optionally include a transition within the effect.

Build an HTML Page

You can use an existing page if you have one you’re working with, but if not use the following HTML5 code outline:

<!DOCTYPE html> <html> <head> <style type="text/css"> </style> </head> <body> <img src="picture.jpg" alt="picture" class="skewer" /> </body> </html>

We are going to apply the skew effect to an image element, so alter the markup to reflect the name and location of your own image file, which can be any image you have but will be easier to work with if it is relatively small. Include your image name in the “img” tag “src” attribute.

Style the Element

To see the skew transform working, you first need to give your image element some initial style properties. Add the following declarations inside the CSS area within your page head:

img.skewer { left:100px; top:150px; position:absolute; }

This gives us a default position for the image element within the page.

Make the Element Interactive

Rather than simply applying the skew effect straight away, lets make it happen on user interaction. Add the following event attributes to your image element:

onmouseover="this.className='skewer skewed'" onmouseout="this.className='skewer'"

Your image element should now look as follows, with your own image indicated in the “src” attribute:

<img src="picture.jpg" alt="picture" class="skewer" onmouseover="this.className='skewer skewed'" onmouseout="this.className='skewer'" />

When the user moves their mouse onto the image, the page gives it an extra class name, then removes this name when the user moves their mouse away from the image. This additional class name will allow us to specify the skew transform in a separate CSS section.

Skew the Element

Add a new section in your CSS area as follows:

img.skewed { /*General*/ transform: skew(30deg, 10deg); /*Firefox*/ -moz-transform: skew(30deg, 10deg); /*Microsoft Internet Explorer*/ -ms-transform: skew(30deg, 10deg); /*Chrome, Safari*/ -webkit-transform: skew(30deg, 10deg); /*Opera*/ -o-transform: skew(30deg, 10deg);...
more →
says: Inline JS? That's a bad practice. How about putting the skewed css in the :hover state? skewer:hover { transform: skew(30deg,...

Developing a Responsive Website: Secondary Page

We’re going to begin to wrap up our tutorial on creating a responsive website this week by creating a secondary services page to feature the necessary, and important, content of our site.  Keeping a sites layout visually appealing is a crucial aspect of keeping a visitor on your site.

Having a simple box filled with verbiage running from left to right is not very inviting, nor aesthetically pleasing.  When displayed like that, the sheer amount of text could be enough to cause your visitor to quickly back up and off your site.  That’s why we’re going to break our text up and display it a little more creatively on our larger format.

Let’s first take a look at what we’re going to create and download the work up to this point, if need be.

When developing a site I typically use my homepage as a template by utilizing the “Save As” feature and renaming it what ever my new page will be.  Feel free to start from scratch or follow along on my code deleting rampage.

Open up your index.php file and save it as services.php.  Once it’s saved as services.php we’re going to need to delete some of the content.  We’ll start in the head of our document by deleting most of the Java scripts and links, with the exception of the two lines following the link to our CSS file.  These two lines call the jQuery and link the the JavaScript necessary for the Twitter feed in our footer to work.  The rest of the Java links can go, they were only necessary for features on our homepage and are no longer relevant.

My <head> section now looks as follows.

<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width; initial-scale=1; maximum-scale=1"> <title>Responsive Web Design</title> <link href="main.css" rel="stylesheet" type="text/css" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script> <script src="js/twitter.js"></script> </head>

We’re going to rename our “mainBG” div to “services”.  Go ahead and make that switch and change class “homeContent” to “servContent”.  You’ll also swap the note that closes our services div from “mainBG” to “services” to prevent any future confusion.

Next we’re going to want to ditch our “blurb” class, and the final thing we’re going to want to nix is the entire “mainBG2” section, which contains all of the information for the portfolio slider that’s on our homepage.

At this point, weather you gave your delete key a work out or exercised your words per minute, your page should look as follows.

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width; initial-scale=1; maximum-scale=1"> <title>Responsive Web Design</title> <link href="main.css" rel="stylesheet" type="text/css" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"...
more →
says: Layout is the key element in your website to keep visitors attracting & coming again & again to view your website. Good...

Creating a Responsive Website: The Footer Part 2

We’re going to pick up where we left off in our last tutorial on creating our responsive web design footer.

In the last tutorial you may recall that we laid out the large version of our footer and inserted a Twitter feed as well.  In this tutorial we’re going to finish off our footer by making it responsive and plugging in the jQuery necessary to make our Twitter feed work.

If your dog ate your work up until this point, you can download the source code here. Before we get started, let’s take another quick look at what we’ll be finishing up today.

We’ll begin with our Twitter feed and break it down in to three parts.  The first thing we’re going to need to do is retrieve our tweets and set it up so that we can easily change the account name, how many tweets we want displayed, and what element we want it to look for to display our tweets.  To do this we’re going to utilize AJAX to call the Twitter API in JSON format and then process it.  My code looks as follows.

JQTWEET = { user: 'TheScottStanton', numTweets: 1, appendTo: '.twitter', loadTweets: function() { $.ajax({ url: 'http://api.twitter.com/1/statuses/user_timeline.json/', type: 'GET', dataType: 'jsonp', data: { screen_name: JQTWEET.user, include_rts: true, count: JQTWEET.numTweets, include_entities: true }, success: function(data, textStatus, xhr) { var html = '<div class="tweet">TWEET_TEXT<div class="time">AGO</div>'; for (var i = 0; i < data.length; i++) { $(JQTWEET.appendTo).append( html.replace('TWEET_TEXT', JQTWEET.ify.clean(data[i].text)) .replace(/USER/g, data[i].user.screen_name) .replace('AGO', JQTWEET.timeAgo(data[i].created_at)) .replace(/ID/g, data[i].id_str) ); } } }); },

Next we’re going to want to create a dynamic calculator that can tell us how long ago we posted the tweet.  We’ll start by getting the time it was posted, which will require having to make an exception for IE as it can’t handle the screwy Ruby dates.  After that we’ll create a series of if statements to give our time stamp a relative time like “seconds ago”, or “about 1 hour ago.”

timeAgo: function(dateString) { var rightNow = new Date(); var...
more →
Logo Design Serives says: It very harmful for website developers ...... nice work