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>

Tuesday 28 June 2011

PHP, JSON basic example using Topsy Otter

Topsy Otter

Otter API is a RESTful HTTP web service to Topsy. Topsy is a search engine that ranks links, photos and tweets by the number and quality of retweets they receive. The quality of retweets is determined by the influence of the Twitter users.

Below I have used PHP to display results of an Otter call which returns JSON. The results are for the most popular stories on wired.com today.

<?php
$data = json_decode(file_get_contents("http://otter.topsy.com/search.json?q=site:wired.com&window=d"));

foreach ($data as $name => $value)
{
 echo $value->total.'<br />';
 getAllItems($value->list);
}

function getAllItems($iarr)
{
foreach((array)$iarr as $itemName => $itemValue)
{
echo $itemValue->content.'<br />';
}
}
?>

SIMPLE LAYOUT #37

This example makes use of borders and spacing. 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 37</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'>
<style>
body
{
font:0.96em/1.5em Sans-serif;
width:60em;
margin:auto;
color:#333333;
}
header, nav, section, footer, aside
{
margin:0.5em;
border:0.5em solid #EEEEEE;
}
header, nav, section, footer
{
padding:0.5em;
}
header, section
{
width:36.5em;
}
header, section
{
float:left;
}
nav, aside
{
float:right;
}
section, footer
{
clear:both;
}
nav
{
text-align:center;
width:18em;
height:3em;
padding-top:2em;
background:#94D5F8 url(images/bluefade.png) repeat-x;
}
aside
{
width:19em;
}
img
{
margin:0 0 -0.35em 0;
}
p
{
text-align:justify;
margin-bottom:0.5em;
}
h1
{
color:#0693E2;
font:4em 'Quattrocento', arial, serif;
text-transform:lowercase;
}
h1 .last-letter
{
color:#AAAAAA;
}
nav a
{
margin:0.5em;
text-decoration:none;
color:#FFFFFF;
}
nav a:hover
{
text-decoration:underline;
}
</style>
</head>
<body>
<br />
<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>
<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>
<aside>
<img src="http://lorempixum.com/291/300/technics" />
</aside>
<footer><p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p></footer>
</body>
</html>

PHP, JSON basic example using delicious

Here is an example of extracting and displaying JSON results through PHP. In this instance, I am using my delicious feed.
Having received the data and decoded it into an array, I like to see the structure of the data. This is where the <pre> tags come in handy. Once you have seen the structure, you know how to traverse and pick out the values you need. Given that the code below is so small, I have commented it.



<?php
/* Get the data and put it into an array */
$data = json_decode(file_get_contents("http://feeds.delicious.com/v2/json/guitarbeerchocolate"));

/* Display the structure of the data so that we know what to extract */
/* Comment out once understood */
echo '<pre>';
print_r($data);
echo '</pre>';

/* Traverse the array, picking out the values you need */
foreach ($data as $name => $value)
{
echo $value->u.'<br />';
echo $value->d.'<br />';
getAllItems($value->t);
echo $value->dt.'<br />';
echo $value->n.'<br />';
echo $value->a.'<br />';
}

/* Some values in this case, tags are themselves arrays so traverse those too */
function getAllItems($iarr)
{
foreach($iarr as $item)
{
echo $item.', ';
}
echo '<br />';
}
?>

Monday 27 June 2011

PHP Form, jQuery, working in IE

Some days, you start by saying, "'I'll just get this out of the way, then I can concentrate on something heavier". You then write code without testing because you know how it works, and apart from a little syntax change, you get it working fine. About 15 minutes. This was my experience with writing a page with a form. It stored the data in a text file, then updated the page tag with the new contents. It worked fine in Chrome and Firefox. Then the dreaded IE pulled me back. 3 hours later, I find the solution. Below are code examples to a common problem.
Create a html form.
On submission, write the contents of the form to a file.
Once the file has been updated, refresh a section showing the new content.

Here is the code for readdata.php. Notice the @ before file_get_contents. This suppresses a warning if the file does not already exist.

<?php
foreach (explode("\n", @file_get_contents('data.data')) as $value)
{
echo $value."<br />";
}
?>
Here is the code for writedata.php. Nothing particularly clever here.

<?php
file_put_contents('data.data', $_POST['stuff']."\n", FILE_APPEND);
?>
Now to the main page. A few of important items here:
  1. Only $.get works in IE .load doesn't.
  2. $.ajaxSetup({cache: false}) is also required for IE otherwise you get all sorts of content varients coming through.
  3. I used the jQuery Form Plugin from http://jquery.malsup.com/form/ It saves a lot of problems.


Have fun.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Read and write using jquery</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" />
<style>
body
{
font-family:Sans-serif;
line-height:1.5em;
}
</style>
<script src="http://www.google.com/jsapi"></script>
<script>
google.load("jquery", "1");
google.load("jqueryui", "1");
</script>
<script type="text/javascript" src="jquery.form.js"></script>
<script>
$.fn.readStuff = function()
{
$.get('readdata.php', null, function(data)
{
    $('section').html(data);
});
$('#stuff').val('');
}
$(document).ready(function()
{
$.ajaxSetup({cache: false});
$('form').ajaxForm(function()
{
$(this).readStuff();
});
});
</script>
</head>
<body>
<h1>PHP and jQuery read/write test</h1>
<h2>Write data</h2>
<form action="writedata.php" method="POST">
<input type="text" name="stuff" id="stuff" />
<input type="submit" />
</form>
<h2>Read data</h2>
<section>
<?php
include 'readdata.php';
?>
</section>
</body>
</html>