Category Archives: PHP

Screen Shot 2014-01-05 at 11.56.51 PM

WordPress – jQuery

WordPress will load jQuery automatically without you having to add the files yourself. I’ve seen a different methods, some of which do not always work. Here’s the method I use that seems to work reliably.

The following would be added to your functions.php file. If you don’t have a functions.php yet, create one, in your theme folder.

 

 

Note! There can be no characters, this include spaces, or extra lines, in front of the tag. This is good practice. Extra characters outside of the php tag will cause the server to create HTTP header and send it to the browser before WordPress is ready.

If you already have a funtions.php file in your theme, you can add the snippet above, without the <?php.

WordPress loads jQuery in “No Conflict” mode. This means that jQuery will be accessed through the variable jQuery rather than the $. To work with this easily, set up your document ready actions like this:

Notice here, that jQuery (note the uppercase Q!) replaces the $ outisde the ready function. While inside the function I’ve used the $. This is possible by including the $ as a parameter to the function inside jQuery(function($){}). Placing this variable here allows jQuery to assign itself to this name, so inside of the function you can use that variable in place of jQuery.

PHP – If statements

Most programming languages provide a conditional statement of some kind. PHP provides an if then else, the syntax of which is very similar to Javascript and other languages. In PHP the if statement has a few extra features.

Let’s start with the basic if statement.

Here if the condition is  evaluated to true the code block is executed. You can add an alternative code block that will be executed if the condition is evaluated to false.

Here the the second code block is executed when the condition is false.

Here’s a couple real world examples.

In this example if the variable $_GET['category'] has been set the value is printed to the page.

We can add an else to this:

Here if the $_GET['category'] variable is set, then it is printed to the page, otherwise (else) the message “Category not set” is printed instead.

Since PHP is an html embedded language you will often find that you want to print HTML and other text to the page as part of the if and else. Using echo is okay in some cases. Fo example:

In other cases the situation gets more complex. For example imagine that you are check if someone is logged in, and if not you want to display a form:

Printing the form using echo is awkward. I didn’t write the entire form above, use your imagination, it can get a little ugly; worst of all everything is inside the quotes so you don’t get any color coding or other help from you editor.

In these cases there is a better way. PHP allows you to close PHP and reopen the PHP tag anywhere. Here’s an example:

Here we exit PHP after the else statement, so the form can be written as standard HTML. Not that that the if statement has the it’s complete syntax within the <?php ?> tags. The lone } looks a little odd inside of <?php ?> on the last, but this completes the syntax so everything is okay.

Alternately you could write the same statement above with this alternate syntax.

PHP Basics: Functions

Functions in PHP

PHP allows you to write functions similarly to the way functions are written in other languages. Functions in PHP provide the same advantages that they provide in other languages also. Functions provide a way to encapsulate a block of code so it can be reused as often as needed. This allows you to avoid rewriting the same blocks of code over again. Which makes your job easier by writing less code. This also keeps your from having to write similar or the same block of code more than once.

Continue reading

PHP MySQL and Special Characters

You may find that you have a problem with special characters not displaying or showing up as a small black diamond with a ? in the center. Here’s a quick fix seems to work well.

These would be characters like: <, > and &. It also includes characters like: é,?, ø and the curly quotation marks.

At the point you are printing these characters into a page you’ll want to run them through the php htmlentities function. Here’s an example:

Where $str would be any string you wanted to convert to compatible characters.

For more info: http://www.php.net/manual/en/function.htmlentities.php

Doodle

Here’s an app I thought up the Firday. I managed to “finish” it up after dinner Friday and and with a few spare hours Saturday. I put finished in quotes, because it’s not really finished. There’s a still a lot to do. I did manage to mock up the basic ideas and get them all working.

http://newmedia.academyart.edu/~mhudson/doodle/

So what’s the idea anyway? Anyone can make a user account, login and create a small drawing, and then list all of the drawings that have been created. My idea was to create a sort of Twitter for the visual set. Instead of typing a short message of the moment. You can draw a small picture of the moment, and post it for everyone to view.

The basic functionality works now, but to make this really useful will require more features. At this point you are viewing all of the drawings in the database. The real idea is to show only drawings from people that you feel like seeing.

Kimili Flash Plugin

Testing the Kimili Flash plugin for WordPress. It seems to be working pretty well so far.

[kml_flashembed movie="http://webdevils.com/examples/Make3d/3D_03.swf" height="300" width="300" /]

Interesting to note that the plugin is supposed to work with Flash anywhere it appears. So you can use KimiliFlash to put Flash into your header or other areas of your site.

Another interesting item is that the Kimili plugin uses SWFObject: Javascript Flash Player detection and embed script which is XHTML valid. The default Flash HTML code written by Flash and Dreamweaver will not validate.

There is an article about this on A List Apart.