Tutorial Associative Array Syntax

by in , 0

Simple

$carParts = array( 
   'Tires'=>100, 
   'Window'=>1042, 
   'DoorHandle'=>917 
);

Array of Associative Arrays

public $notifyPartners = array(
			array(
				'name' => 'Twitter', 
				'tag'  => 'Social Network', 
				'url'  => ''),
			array(
				'name' => 'Campaign Monitor', 
				'tag'  => 'Email Marketing', 
				'url'  => ''),
			array(
				'name' => 'Sendloop', 
				'tag'  => 'Email Marketing', 
				'url'  => ''),
			array(
				'name' => 'Highrise', 
				'tag'  => 'CRM', 
				'url'  => '')
);

Looping

foreach ($carParts as $key => $value) {
 echo $key.'=>'.$value.'<br />';
}
while ($element = each($carParts)) {
 echo $element['key'];
 echo ' - ';
 echo $element['value'];
 echo '<br />';
}

Tutorial Applying Even/Odd Classes

by in , 0

<div class="example-class<?php echo ($xyz++%2); ?>">

Used inside a loop, class names would be named .example-class0 and .example-class1 alternating. Increasing the "2" number allows you to increment in thirds or fourths or whatever you need:

class="class<?php echo ($xyz++%3); ?>"

Classes: class0 class1 class2 class0 class1 class2

Change number to 4, this is the result: class0 class1 class2 class3 class0 class1 class2 class3

Tutorial Append Non-Breaking Space Between Last Two Words

by in , 0

<?php

function word_wrapper($text,$minWords = 3) {
       $return = $text;
       $arr = explode(' ',$text);
       if(count($arr) >= $minWords) {
               $arr[count($arr) - 2].= '&nbsp;'.$arr[count($arr) - 1];
               array_pop($arr);
               $return = implode(' ',$arr);
       }
       return $return;
}


?>

Reference URL

Tutorial Append Login Credentials to URL

by in , 0

The example here is if you had a form on a website that when submitted, needed to use that information to go to a special URL where the login information was all appeneded to the URL. You could have the form post with method GET, but that is limited to the typical ?variable=foo&variable2=bar format.

HTML Form

Typical form with three bits of information that submits to a file called ftp.php

<form action="../processing/ftp.php" method="post">
<p><label for="ftp-company-name">Company</label><input type="text" name="ftp-company-name" id="ftp-company-name" /></p>
<p><label for="ftp-user-name">User Name</label><input type="text" name="ftp-user-name" id="ftp-user-name" /></p>
<p><label for="ftp-password">Password</label><input type="password" name="ftp-password" id="ftp-password" /></p>
<p><input type="submit" id="ftp-submit" class="button" value="submit" /></p>
</form>

PHP file

This file reads in the POST variables (if they are set), builds the URL from them, and redirects to it. You'd probably want to clean up the POST variables for security purposes.

<?php

    if (isset($_POST["ftp-company-name"])) {
    
        $company = $_POST["ftp-company-name"];
        $username = $_POST["ftp-user-name"];
        $password = $_POST["ftp-password"];
        
        $url = "ftp://$username:$password@ftp2.edgeconsult.com/$company";
        
        header( "Location: $url" ) ;
        
    } else {
    
        // do nothing
        
    }

?>

Tutorial Adjust Server Time

by in , 0

Sometimes the time set on your server isn't accurate to what your local time is. If you can't change it, you can adjust it yourself.

$today = date('Y-m-d-G');
$today = strftime("%Y-%m-%d-%H", strtotime("$today -5 hour"));

If your server thought it was 12 midnight, but it was really 7 pm, this will roll the $today variable back five hours to be correct.

Redirect WWW / No-WWW htaccess

by in , 0

You should really be doing one or the other. For consistency, as well as SEO's, sake.

Force the www.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^your-site.com [NC]
RewriteRule ^(.*)$ http://www.your-site.com/$1 [L,R=301]

Remove the www.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^your-site.com$ [NC]
RewriteRule ^(.*)$ http://your-site.com/$1 [L,R=301]

Tutorial Use PHP inside JavaScript

by in , 0

This has only really been tested on Media Temple (gs) servers.

In the folder with the JavaScript, the .htaccess file should include:

<FilesMatch "^.*?api.*?$">
SetHandler php5-script
</FilesMatch>

In that above example, any file that includes the string "api" will be processed as PHP. Feel free to alter that RegEx.

Then in the JavaScript file itself, set the ContentType back to JavaScript:

<?php
	// Sets the proper content type for javascript
	header("Content-type: application/javascript");
?>

That will ensure browsers reading the file will interpret it as JavaScript. Now you can use <php ... ?> tags in the JavaScript file to do whatever PHP stuff you need to do.