Showing posts with label Code. Show all posts
Showing posts with label Code. Show all posts

Wednesday, January 08, 2014

Scroller Animation Resources

Scroll activated website animations are so hot right now. Oh my gosh sooooo hot. Here are some resources.

Monday, September 23, 2013

Returning Multiple Values with a Function in PHP

As I'm coding I try to package my code in useful pieces that make it easy to understand what is going on in my programs. What that means in practice is using functions to do the packaging. Functions are just actions that can include multiple statements and do multiple things. If variables are the nouns of your program then a function is the verb. Functions are sometimes also referred to as methods.

Anyways, one action you might want a function to do is return a value. To do this you may have a function fetch results from a database or perform some equation base on a value being passed to the function. For example I could make a function that adds 10 to a value.
function addTen($number) {
$sum = $number + 10;
return $sum;
}
echo addTen(5);
In the above example I have a function called addTen that is passed a number and returns that number with 10 added to it. In order to have the code spit out the value you just echo out the function. Pretty simple

Returning Multiple Values

A function returning one variable is easy. Functions are meant to return a variable. Having a function return multiple variables is a little more complicated. The way it is achieved is with an array.

Arrays are variables with multiple values. An example of an array might be a variable called $color. There are many colors, so it would make sense to create our variable $color using an array. To make an array you declare the variable and specify the values like so: $color = array(red,blue,green,black); You access the values by specifying the key (Pointing at which value you want). To access red you would: echo $color[0]; Arrays start with 0 as the first value. Another type of array are associative arrays. These types of arrays have user defined keys instead of the default numbered keys. For example I could have and array of the traits of a person: $person = array( 'name' => 'Jason', 'address' => '123 Main St.', 'age' => '25'); To access Jason's address you would echo $person['address'];

So that brings us to the main objective here: Creating a function that returns multiple values.
// This is handy if you had a function that could do a database call and return a bunch of values. You could then access those values as an array

function animals() {
$dog = "odie";
$cat = "garfield";
$rabbit = "bugs";
$donkey = "eeyore";

$animals = array (
dog => $dog,
cat => $cat,
rabbit => $rabbit,
donkey => $donkey
);

return $animals;
}
// set variable equal to the function returning the array

$animals = animals();

//access the values through the keys of the array

echo $animals['dog'];
echo "
";
echo $animals['cat'];
echo "
";
This method has all sorts of uses. This is extremely useful when dealing with a database and you want to consolidate your fetch operations in a function but want to separate your styling code from the function.

Hope you find this useful!

Thursday, September 12, 2013

Passing SESSION variables between Domain and Subdomain

An issue I came across recently was that when I set a Session variable at the subdomain level of my site I couldn't access that session variable at the root level. In other words, when I set $_SESSION['name']= "John"; at http://john.harbison.com I couldn't access $_SESSION['name'] at www.harbison.com. That sucks! I did find the answer though thanks to Jeroen on Stack Overflow.

The solution is to give your SESSION a Name! You need to name the session before you even start the session. So here is the code:
$some_name = session_name("some_name");
session_set_cookie_params(0, '/', '.some_domain.com');
session_start();
I love the internet!

Monday, August 19, 2013

Make Subdomain a Variable with PHP

I wanted to create a dynamic page using a subdomain as a variable. What a user would do is put in a subdomain and it would pull up a page based off of that subdomain. For example: A user would type in http://car.file-drive.com and it would pull up a picture of a car. Of course car could be anything. It could be a list of names or anything at all. The way that it works is with a Wildcard subdomain. Here are the steps to do this.

1. Setup Wildcard Subdomain at your domain Registrar

You can skip this step if you host your dns record locally or with your hosting provider. I don't so I had to add the Wildcard record. My registrar is Godaddy so I just logged in, edited my DNS record and added a new CNAME record. I set the Host to * and it POINTS TO @

2. Setup Wildcard Subdomain with Web Host

My web host is Host Gator. I had to log in to the CPANEL and navigate to the subdomain settings. I added a new wildcard subdomain here for File-drive.com. So the settings again were * which is the character that means "any" just like a wild card in poker which can be any card. If this is an "add on" domain you'll need to point the subdomain to the proper root directory of your domain. File-drive is an add on so I had to point * to public_html/file-drive.com

3. Use PHP to Pull out the Variable

Now that the wildcard subdomain is set my domain can have any and as many subdomains as I want. What we have to do from this point is to break apart the url and get the variable from the subdomain. Will use
<?php $subdomain = array_shift(explode(".",$_SERVER['HTTP_HOST'])); ?>
to get the subdomain and set it to $subdomain.

At this point you can do all sorts of things. You could echo or print out the subdomain. You could make a database call using the subdomain variable. You could do pretty much anything you want to.
Enjoy!

Tuesday, August 13, 2013

Add A Google Map to Wordpress Contact Page

This tutorial is assuming you know how to create a page or a post in Wordpress. Additionally this will provide info to add a map to any page, not just a contact page.

Get your Map code

  1. Goto Google and Google the address you want to get your map for - click on the map picture that results. If that doesn't work goto https://maps.google.com/ and do your search.
  2. Once the result is displayed change your view to whatever you want to display on your page. You might change to a satellite view or a traffic. You may want to tweak other settings in this window. If it is displaying on this page then it will display on your page
  3. Click the Link Icon (photo below 1.)
  4. Click and copy the <iframe> code (Photo above 2.)
  5. Go to WordPress and click to edit the page/post you want to add your map to
  6. In your editor Click "Text" tab (Photo above 1.) If you have content on this page already you will need to place your cursor after/before the content you would like the map displayed
  7. Update or Save your page/post

To Resize the Google Map

If your map isn't the size you want you can edit the code that Google supplies and change the size.
  • In the code that you pasted above look for the Width and Height Parameters. It should look like: <iframe width="425" height="350" . . . . These parameters describe the size of the map that displays
  • To make the width larger type in a larger number. I changed mine from 425 to 800. This is in Pixel increments. This number should be less that 1028
  • To make the height of your map larger edit the height="350" value. Change the 350 to something larger. I used 700.
  • Once you've set the correct sizes click Update and save your post
  • Be sure to leave the "Quotation Marks" around the numerical value
Good Luck!

Monday, August 12, 2013

Customizr Slider: Create and edit new slider


The customizr Theme for Wordpress features a very large slider on the home page that is somewhat difficult to edit. The theme has a demo slider installed with little to no direction on how to edit it. below are the steps necessary for creating and editing the slider.

1. Create and image for the slider

The dimensions the default slider is 1200 X 500

2. Upload the image

Click the "Add new" link under Media.
Select your file and upload (you can upload more sliders if you want to at this time. Adding them to the slider is the same once the slider has been created)

3. Click "Edit"

Editing Media: In this window you can add captions, descriptions and add this media to your slider. We first need to create a new slider. Near the bottom of the page there will be a section called "Slider Options"
  1. By default this will be set to "No" - Change it to yes.
  2. The title - this will be the Main title of your slide
  3. Description - is the text below the title on the slide
  4. Link - You can choose the slide to link to a page of the site
  5. Choose a Slider - If this is your first time creating a slider you will have to make a slider to add your photo to. In the box type out the name of your slider and hit "Add a slider"
Once you've completed your first media upload and created your new slider. you can just repeat the process on the rest of your slide items.

4. Add a slide from Media Library

Upload your new image and from the media library click "Edit". From the new window navigate to the slider options at the bottom. You can Add a title and description like described in number 3. above. This time though:

  1. Instead of creating a new slider, use the drop down to select the slider
  2. You can delete the entire slider from this screen
  3. To arrange the items in the slide, just drag and drop them into order
Finally once you've finished editing the slider be sure to save that slide's information before navigating away. Click the "Update" button

Set Your New Slider to the Home Page

  1. Click Appearance on the left Bar - then select Customiz'it.
  2. Next Click "Front Page" from the left Nav
  3. choose your new slider from the "SLIDER OPTIONS".
  4. Save and close
For more information check out the links below.
Message board on theme

Thursday, July 18, 2013

Apache Rewrites to hide variables

I've been absolutely fighting with rewrites over the past week. Why do you want to know about rewrites? You can easily hide variables with it.

Example

 Let's say you want to hide a variable in a clean url. I want http://www.store.com/catfood.html instead of http://www.store.com/product.html?p=1234233

If you are dealing with apache servers then you'll be dealing with the .htaccess file. Here are the steps required to make that example happen.
Step 1: open .htaccesss. If you are on a linux server running Apache you should have one. If it isn't showing then it might be a hidden file. If you don't have an .htaccess file then you need to make one. It should be in the root of your public_html folder on your server.
Step 2: RewriteEngine On   #you need this bit of code to turn on Rewrites
Step 3: RewriteRule PATTERN SUBSTITUTION FLAG and example would look like this RewriteRule ^(.*)?\.html product.php?p=%1$1 [QSA] 

The Pattern is "^(.*)?\.html" which uses a regular expression to say Any characters before .html. So if someone types in toyota-camry.html it will take "toyota-camry" from the url - what it does with it is explained in the next part

The Substitution says "okay you have toyota-camry as a variable - we'll place that variable in your substitution and on the server we'll run product.php?p=toyota-camry. That means you with this rewrite we can obstruct the variable from view and the end user will just see the clean url. Pretty cool.
So this little .htaccess rewrite handles hiding the variable and give your site a clean looking URL. What this doesn't do is actually process the variable. If you are using PHP you'd need some $_GET methods to process the product id that is being passed to it. That is another lesson all together.

The Flag is [QSA].  There are lots of different flags, but what that flag means is "append query string from request to substituted URL".  This way you can add on additional query items to the newly formed URL.  Basically it means you could have catfood.html?t=wet and it would pass the varible properly without ignoring the variable.

Some other cool things you can do with rewrites:

  • You are trying to create dynamic subdomains. Otherwise you'd have to setup each instance in the DNS record. (This one requires adding a wildcard A record on the DNS record)
    •  RewriteEngine on
      RewriteCond %{HTTP_HOST} ^(.*)\.exampleDomain\.com
      RewriteRule ^(.*)$ http://exampleDomain.com/%1/$1 [L]
  • You want to hide directories and file structure. http://www.domain.com/gallery.html instead of http://www.domain.com/pictures/gallery1/index.php
    •  RewriteEngine on
      RewriteRule gallery.html$ pictures/gallery1/index.php [L]

Additional resources:

This is an absolutely fantastic resource by addedbytes Url rewriting for Beginners