Thursday 19 December 2013

Quick overview of humans.txt for SEO

humans.txt is a TXT file that contains information about the different people who have contributed to building the website. Click here to access an example from humans.txt. The file is picked up by search engines.
Add a humans.txt to your site and reference it using the tag
<link type="text/plain" rel="author" href="humans.txt" />

Wednesday 11 December 2013

Dynamic progress bar for Twitter Bootstrap using PHP

Twitter Bootstrap offers some very useful progress bars. See http://getbootstrap.com/components/#progress
They are based on percentages, so you need to get the length of the shaded bar as a percentage of the whole bar for them to be accurate.

Below is a small example of how to do this with PHP.

<?php
$currentVal = 10;
$maxVal = 20;
$percentageVal = ($currentVal/$maxVal)*100;
?>
<div class="progress">
  <div class="progress-bar" role="progressbar" aria-valuenow="<?php echo $currentVal ?>" aria-valuemin="0" aria-valuemax="<?php echo $maxVal ?>"  style="width:<?php echo $percentageVal ?>%;">
    <span class="sr-only"><?php echo $percentageVal ?> Complete</span>
  </div>
</div>

Recursive delete for blogs and forums

Let's say, you've developed a blog or forum and you need to delete some entries. You will also want to delete the responses to those entries. In this example we'll use a table structure like this, where 'refid' is a field used to associate a response with it's referer :
`id`
`refid`
`userid`
`title`
`content`
`tags`
`created`
In our PHP we want to go through each blog entry which we'd like to delete and ascertain it's responses identified by 'refid' before deleting it. Thus :
function deleterecursiveblog($id)
  {
    $q = "SELECT * FROM `blog` WHERE `refid`='{$id}'";  
    $result = mysqli_query($con,$q);
    if(isset($result))
    {
      foreach($result as $key)
      {
        deleterecursiveblog($key['id']);  
      }
    }
    $q = "DELETE FROM `blog` WHERE `id`='{$id}'";  
    $result = mysqli_query($con,$q);
  }

Friday 6 December 2013

Bash techniques

I've been doing a lot of work in bash lately. I've been speeding up the time taken to create the website basics. Right now I can create a Twitter Bootstrap/Modernizr website with blogs etc within a couple of seconds by calling my bash script. So, below are some techniques I've learned along the way:
Prep-end all slashes in a web address with backslashes
webdir=`echo $webdir | sed s,/,\\\\\\\\\\/,g`
Get the last substring from a string separated by slashes
dbn=${longstring##*/}
Put the contents of a .sql into a database
mysql -u root -p password $dbn < $workingdir"/users.sql"
Change a line within a .ini file
sed -i -e '/DB_NAME =/ s/= .*/= '$dbn'/' config.ini
Hope they help!

Thursday 28 November 2013

Error reporting in PHP

I've seen quite a few blogs which go into great depth about error reporting in PHP. What I and almost everyone else wants when something is going wrong is... Tell me everything. So, here are the lines to put at the top of your .php file
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
?>
Job done.

Friday 22 November 2013

OOP Series : Interfaces

An interface is a template of mthods. Not properties. An interface is usually accompanied by an 'implements' clause. Classes which implement an interface must contain the declared methods. There is no defined means of declaring a JavaScript interface/implments combination.
PHP : template.class.php
<?php
interface template
{
function setWidth($width);
function setHeight($height);
}
?>
PHP : document.class.php
<?php
require_once 'template.class.php';
class document implements template
{
public $width, $height;
function setWidth($width)
{
$this->width = $width;
}

function setHeight($height)
{
$this->height = $height;
}
}
?>
Now a file (index.php) which creates a document object.
<?php
require_once 'document.class.php';
$doc = new document;
$doc->setWidth(10);
$doc->setHeight(10);
echo $doc->width.' '.$doc->height;
?>

OOP Series : Abstraction

Abstract classes are particularly useful if you are working in a team and on a large project. They allow the architect to specify what needs to be available in child classes without saying how. They can also be used to specify methods which must be available to all child classes. Abstract classes should't really be called, just referenced. In the example below I have an abstract class of book and a child class of childrensbook. The good news for me in writing the blog post is that there is no abstract class implementation in JavaScript.
PHP : book.class.php
<?php
/* You may only have child classes of this class */
abstract class book
{
function cover()
{
/* All child classes must have this function */
return 'Nice bright cover';
}

/* Must exists in a child class, but you can implement it as you see fit */
abstract function pages();
}
?>
PHP : childrensbook.class.php
<?php
require_once 'book.class.php';
class childrensbook extends book
{
function pages()
{
return 20;
}
}
?>
Now a file (index.php) which creates a childrensbook object.
<?php
require_once 'childrensbook.class.php';
$achildrensbook = new childrensbook;
echo $achildrensbook->cover().'<br />';
echo $achildrensbook->pages().'<br />';
?>