Monday 7 November 2011

Getting single row results MySQL using PHP

I'm always using this code. Sometimes I know there should be/is only one record returned from my MySQL query. Especially if I have specified 1 result as in the query below. As long as I know the resulting column names, this is a very useful technique.

<?php

$query = mysql_query("SELECT * FROM `users` WHERE `username` = '{$username}' LIMIT 1;");
$row = mysql_fetch_assoc($query);
echo 'Hello '.$row['firstname'];
?>

Tuesday 1 November 2011

How to use PHP __autoload over multiple directories

Funny this. One of those things where you do a bit of surfing to find the easy answer. The resulting pages of your search contain the name of the solution you are looking for in the title, but the containing code solves a different problem.
So, here is the problem.
I am creating a PHP application which has multiple directories containing classes will need throughout it's build.
I want to use the PHP __autoload function.

I will need 2 PHP files to support this activity, but below this I will show how they are called.
First file, config.php. This will contain a class containing all the support data needed throughout my application.

<?php
class supportdata
{
public $supportArray = array('lib', 'helper');
}
?>
There. Not too difficult was it.
Next, my autoload.php.

<?php
require_once 'config.php';
function __autoload($class_name) 
{
$sd = new supportdata;
$classString = '';
foreach($sd->supportArray as $value)
{
$classString = $DOCUMENT_ROOT.$value.'/'.$class_name.'.php';
if(file_exists($classString))
{
require_once $classString;
}
}
}
?>
As you can see it calls config.php and makes use of the supportArray to iterate through the directories, checking to see if the class exists. If it does we do a require_once on it.

Now, here is how my autoloading is called.

<?php
include_once 'autoload.php';
class index
{
function __construct()
{


}
}
new index;
?>

In my index class I can now call any of the classes contained in the directories named in config.php. In fact, I can call any of the those classes, anywhere in my application.

Thursday 20 October 2011

How to use cache-manifest for localstorage

So, you want your website to continue working on someone's laptop, when they're on a train and going through a tunnel.

Here is a technique for doing just that.

First create a .htaccess file for your site and add to it the following line:
AddType text/cache-manifest    .manifest

Next, create a file called cache.manifest. In this file, add the lines:
CACHE MANIFEST
index.html

Continue to add entries for any file you wish to be cached such as additional pages, images, scripts, stylesheets etc.

Now let's create the index.html file which will be cached:
<!DOCTYPE html>
<html manifest=”cache.manifest”>
<head>
<title>Cached Page</title>
</head>
<body>
My cached page.
</body>
</html>

There. Wasn't too difficult was it.

Wednesday 19 October 2011

I was forced to find an IE Tester

First post in a long time. I've been very busy.

Normally, by keeping to W3C standards, I can develop a site and then do some tweaks for IE at the end. Recently developed an application in which I tested for IE along the way. Or so I thought. I was in fact, testing in IE8. Not good enough. The application came out a total mess in IE7. This was one particular customers IT Department browser of choice. This seems like a ramble but I'm getting to the point.

I then discovered IE Tester at http://www.my-debugbar.com/wiki/IETester/HomePage. This is an excellent tool if you need to view your work in various versions of IE without messing up your existing install. It works stand-alone and also happily access pages delivered by your localhost.

Well done DebugBar!

Friday 22 July 2011

Use PHP to get the current web address without the filename

Sometimes I need to grab the current web path without the current file name in order to pass a string in an email. As an example the current default location is http://www.effectivewebdesigns.co.uk/index.php, but in the future it may be http://www.effectivewebdesigns.co.uk/index.html or http://www.effectivewebdesigns.co.uk/default.html so sometimes I need to future proof.

<?php
$webAddress = 'http://'.$_SERVER['SERVER_NAME'];
$webAddress .= $_SERVER['REQUEST_URI'];
$webAddress = substr($webAddress, 0, 0-(strlen(basename($_SERVER['REQUEST_URI']))));
echo $webAddress;
?>

Lorem and Gibberish through PHP using the randomtext.me JSON API

Crikey! Is it so long since I did a post. OK. Here is yet another way of getting Lorem Ipsum or Gibberish to your page, while you test it out. There is a great generator at http://www.randomtext.me/ and they have helpfully supplied us with a JSON based API.

Below is an example ho grabbing 7 paragraphs of gibberish between 30 and 50 characters long. I then echo them to the page.


<?php
$data = json_decode(file_get_contents("http://www.randomtext.me/api/gibberish/p-7/30-50"));
echo $data->text_out;
?>

Monday 4 July 2011

SIMPLE LAYOUT #38

This example makes use of the Google Font API, spacing shades of grey. It's not a bad starting position.

See demo.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Design Template 38</title>
<!--[if IE]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="stylesheet" href="http://meyerweb.com/eric/tools/css/reset/reset.css" />
<link href='http://fonts.googleapis.com/css?family=Quattrocento&v1' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Oswald&v1' rel='stylesheet' type='text/css'>
<style>
body
{
font:0.9em/1.5em 'Quattrocento', arial, serif;
width:60em;
margin:auto;
background:#EEEEEE;
color:#333333;
}
h1, nav
{
font-family:'Oswald', arial, serif;
}
section
{
width:37em;
margin:0 0 0 4em;
}
header, section, aside
{
float:left;
}
aside, footer
{
clear:both;
}
nav
{
float:right;
text-align:center;
margin:0 0 2em 0;
}
aside
{
width:19em;
}
img
{
margin:0 0 0 0;
}
p
{
text-align:justify;
margin-bottom:1em;
}
h1
{
color:#0693E2;
font-size:4em;
text-transform:lowercase;
margin:0.75em 0 0 0;
}
h1 .last-letter
{
color:#AAAAAA;
}
nav a
{
display:block;
float:left;
width:6em;
text-decoration:none;
padding:4.4em 0 0 0;
color:#333333;
}
nav a:hover
{
color:#FFFFFF;
background:#94D5F8;
}
footer
{
padding:1em 0 0 0;
}
</style>
</head>
<body>
<header><h1>Header <span class="last-letter">1</span></h1></header>
<nav>
<a href="#">home</a>
<a href="#">services</a>
<a href="#">portfolio</a>
<a href="#">contact</a>
</nav>
<aside>
<img src="images/bigl.png" />
</aside>
<section>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
</section>
<footer><p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p></footer>
</body>
</html>