1. Home
  2. Prices
  3. Guided Tour
  4. New Shop
  5. Shop Review
  6. Ecommerce Marketing
  7. Ecommerce TV
  8. Magento Developers
  9. Portfolio
  10. Blog
  11. Contact

PHP Tips

Archive for the ‘PHP Tips’ Category

PHP Tips – Remove That Bad Character

Posted on: 29th Jul 2010 By: Robert Kent No Comments

There is a very common occurrence in websites. A bad character will appear on your web page for no reason whatsoever. Or so you thought…

bad characters in action

Some amateur designers and developers can sometimes forget to play web-safe. Meaning your pound signs should be written £ rather than £ or your & symbols should be written &. However you perform it these simple lapses in using the correct web-safe format for the correct character can always slip through the net.

Perhaps you have a function that prints data from a database. Perhaps the data in that database is plain text – i.e text that is not web-safe. What can you do?

Well here is one tip for you. If you are printing out lots and lots of plain text onto your website and getting lots of <?> bad character signs try using this simple function:

<?php echo utf8_encode($message); ?>

This should solve that problem in most cases. It’s very simple and very quick and easy to implement. Next time you see a bad character just give utf8_encode a go – you never know it may fix everything!

Categories: PHP Tips Tags:

PHP Tip – Find all Images in a Folder

Posted on: 26th Jul 2010 By: Robert Kent 2 Comments

There may come a point in your lives when you need to find some images in a folder. There are many reasons why this may be so, but not many that come to my mind immediately – only this – find all images and organize them for some reason.

Well here is a function that I’ve picked up from somewhere and adapted slightly.

<?php

$namecheck = "apple";

$dir = opendir ("../images");
while (false !== ($file = readdir($dir))) {
if (strpos($file, '.gif',1)||strpos($file, '.jpg',1) ) {

if(strpos($file, $namecheck)){
$myapples[] = $file;
}

}
}

foreach($myapples as $apple){

echo $apple;

}

?>

The above example will find all images in a specific folder with the word “apple” contained in the filename and print them out.

This can adapted in lots of ways so please use it for your own projects.

Thanks for visiting e-commerce website design, magento blog – home of the magento fox!

Categories: PHP Tips Tags:

Top 3 Include Paths in PHP

Posted on: 15th Jun 2010 By: Robert Kent No Comments

Just like my holiday this year – I’m going all inclusive…

Those of you who are PHP developers or who simply work with PHP as the best way to create websites know how important includes are in the overall structure of a website, how the maintenance time decreases and the tidiness of your coding increases after effective use of <?php include(‘this function’); ?>.

However there are times when using php includes can be a bit frustrating, especially when working in a complex folder structure. Includes are always relative, they include files that are local to themselves. This means that if you want to include your header.php from three-folders-deep you would need to type the following:

<?php include('../../../includes/header.php'); ?>

This can get tiresome – especially when you are four or file folders deep and your include strings are becoming astronomical. You can’t even link from root like you can with a header function:

<?php header('Location: /'); ?>

Nope. Not going to happen.

The Solutions

There are several solutions to this dilemma, some of which may work better than the others for your personal situation but I will show you my top 3 so you can decide for yourselves.

1. Relativity Script

This relativity script is something that I made while working on a back-end for an e-commerce website design. The front-end was simple enough but the back-end was more complicated. I decided to try out an automated path finding script that is useful as a way of finding your path and returning to root for all sorts of situations – includes included.

The script is as follows:

$place = $_SERVER['PHP_SELF'];

$place2 = explode('/', $place);

$path = "";

for ($i=0; $i<(count($place2)-2);$i++){

$path .= "../";

}

include($path.'includes/header.php');

This has many benefits but the downsides are that you really need to include this at the top of every page (or at least in the first include which you would have to include relatively). Not good enough really…but might be useful for some things.

2. PHP set include path

The set include path in php lets you define the include path for that file. this is a cleaner way to number 1 but is limited to includes only – which means you wont be able to link say $path.”/images/” to go to your root images folder (though you shouldn’t need to as you should just use /images/).

To use the php set include path enter the following code at the top of your pages:

// Works as of PHP 4.3.0
set_include_path('/inc');

// Works in all PHP versions
ini_set('include_path', '/inc');

However we are stuck again by the fact that this needs to be done on every page or in your first relatively included include.

This is where number 3 steps in and becomes my personal favourite.

3. .htaccess set include path

The beauty of the .htaccess file is that it is a broad setting. A site wide setting if you will. You can create an .htaccess file on the root of your site and it will affect every php file therein. What I found most spectacular about this method is that you can literally change your include path from folder to folder. Take my shop backend as the example. We have a front-end which needs template files including from the root layer. We can do this by creating an .htaccess file – adding the following code and uploading:

php_value include_path /var/www/vhosts/mywebsitename.co.uk/httpdocs/includes/

This will make all includes in the website include from that folder – you can now simply type in <?php include(‘header.php’); ?> no matter how many folders deep you are!

It doesn’t stop there however. If like me, you had an admin section that is using separate includes, perhaps for functions.php or similar, you can create another .htaccess file inside that folder which will override the previous one for that folder and recursive folders within that directory!

php_value include_path /var/www/vhosts/mywebsitename.co.uk/httpdocs/admin/includes/

Hey presto! You have now created a way to define really simple includes!

Oh and by the way – you know that long document path at the beginning of the htaccess script? Simply echo out some php to get that path:

<?php echo $_SERVER['DOCUMENT_ROOT']; ?>

And you now have everything you need.

Thanks for visiting our magento blog on e-commerce web design and please stay in touch with the magento fox!