Monday, 8 December 2008

upgrading to php 5 with Heart Internet

create a .htaccess file with the line:

SetEnv DEFAULT_PHP_VERSION 5

Tuesday, 2 September 2008

Deleting unwanted images of the server.

What I wanted to do was to write a script that after loading an image would then check the image folder for any unwanted image, which would then be deleted.

So first of all I wanted to get a list of files in the folder - for this we need to use readdir - see http://uk3.php.net/manual/en/function.readdir.php

function list_folder($folder){

$dh = opendir($folder);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
sort($files);
rsort($files);
return $files;

}


////////////////////////////////////

//Then i needed to make an array of a list of images that I do want to keep.

//my list looks a bit like this

$images_wanted_array[0] = $row['img1'];
$images_wanted_array[1] = 'thum'.$row['img1'];

/* once have these two list then I run a foreach inside a foreach statement.
So each file gets checked against the list of wanted files. If theres a match I get the result '$success'. After checking the whole wanted list I do a check for any succesful matches and only delete if there aren't any.
Here's the function I wrote.
*/

function deleting_unwanted_images($images_array, $wanted_array, $path){ // this function takes an array of images and they aren't on the wanted_array list they will be deleted.

foreach ($images_array as $f) {
unset($success);
foreach ($wanted_array as $i){
if ($f == $i){
$success = TRUE;
}
}
if (!$success){
$file_to_delete = $path.$f;
unlink($file_to_delete);
echo '
The file'.$f.' - has been deleted
';
}else {
echo '
There has been a match so we are going to keep the file '.$f.'
';
}

}

Monday, 1 September 2008

importing a database .csv to mysql

** this post also included a solution for Invalid query: Duplicate entry '127' for key errors **

The mission -> I have databases given to me in .csv format created in Microsoft Excel and I would like to import them into my mysql database so I can manipulate them from there.

The Solution -> the key to writing the solution for this can be found by using http://uk3.php.net/fgetcsv .

here's the code I wrote from this page. You will need to create your table first that we write to.

#############################################################################

$row = 1;
$handle = fopen ("database.csv","r");
// ip-to-country.csv must be in the same directory with this php file
// customer number here will represent the row it's on database given.

while ($data = fgetcsv ($handle, 1000, ",")) {
$query = "INSERT INTO database_table(`id`, `field1`, `field2`, `field3`)

VALUES('', '".$data[0]."', '".$data[1]."', '".$data[2]."')";
$result = mysql_query($query) or die("Invalid query: " . mysql_error().__LINE__.__FILE__);
$row++;
}
fclose ($handle);

############################################################################


The only error I came across was
Invalid query: Duplicate entry '127'

and this was down to database table I had created as I had the 'id' field set to TINYINT(3) - the resolution was to change this to INT(6)

Thursday, 12 June 2008

php strtotime returning 1970-01-29 - Adding month to date

Adding a month to a date
I was having problems using strtotime. The line i was using was


$next_available_date_end = strtotime("+2 months", next_available_date_start);

and the result I was getting was the date

1970-01-29
I'm not sure why but opted to use mktime instead.
Here is how I accomplished this

$original_date = 2008-12-01;

$original_date_array = explode('-', $original_date ); // splits the date up

list($temp_year, $temp_month, $temp_day) = $original_date_array; // puts the array into the value year,month, day in sequence required

$temp_month++; // adds a month - we can do this because PHP will interperate 13 as the January in the next year

$date_a_month_on= date('Y-m-d', mktime(0, 0, 0, $temp_month, $temp_day, $temp_year));

for more on dates check

http://uk.php.net/manual/en/function.mktime.php&http://uk.php.net/manual/en/function.strtotime.php

Monday, 2 June 2008

Passing variables around a class

I'm expecting a fair few of my following blogs to be about using Classes.
It's one of the tools of PHP that I have not really used. Functions yes but
not Classes, I have however hve been trying to use other peoples classes.
This I feel is bad pracise without fully understanding how and why classes
should be used.
I have been using the class activecalender which can be found at
www.phpclasses.org for a booking script. The changes I have been making to
class have been hacks to the code itself. After some studying into classses
it seems this is not the best practise but what i should do is make an
extended class, known as a child class.

'class myclass extends myparent'

Child classes then inherent member functions and member variables from its
parent. I will be coming back to this topic in a future blog, firstly I will
be looking at repackaging some of my own code into classes.
My original problem that has created this tipping point was that I could not
pass variables around the class . The solution is very simple.
in the Call up to the class set the value of the variable you wish to pass
around the class

$cal->item =$item;

from inside the class set up variable

var = $item;

and form inside the function

$this->item

it's that easy!!!

Thursday, 29 May 2008

testing for File with file_exists

in my code I have a check to see if there is a thumbnail image if not I then resize the image and make a thumbnail. Before running I need to check that I have a big image to crop in the first place.
I do this with
if (!file_exists($thumbnailImg) && file_exists($imgFull))

however the argument file_exists($imgFull) comes back as TRUE even if the folder exists.
one way to get around this problem would be on the command

$imgFull = $pathFull.$img1;
I could check on $img and if it doesn't exist fill it with a value that deliberatly gives an error

if (strlen($img1) > 4){ // counts that $img1 has more than 4 characters ie 1.gif has 5

$imgFull = $pathFull.$img1;}
else
{$imgFull = $pathFull.'error.txt';}


you can check this code working at self catering cornwall .biz

Tuesday, 27 May 2008

Improving listings for HillsideGlastonbury.co.uk

Hillside Glastonbury.

Earlier this month I did some general maintenance work for Hillside Glastonbury B&b and Self Catering . As well as adding lightview image viewer so to make the viewing of images clearer and easier ( to see the effect check Primrose Cottage and click on the main image) I also added several features in the hope we would get the site a better listing with Google.


The idea was to intergrate the site with Googles applications more - There is a list of Google Business tools at Google Services. On this occasion I have added Google Search of Hillside Glastonbury, Google Map of Hillside, Ashwell Lane, Glastonbury, Somerset, BA6 8BG, Hillside Glastonbury Blog and Google Anayltics for accurate stats on our listing with them.