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.


Friday, 9 May 2008

Oscommerce Adding a dropdown menu selector to catergories.php

This is a small section of a contribution that I am writing to add to the website
www.surfboardsuperstore.co.uk

The site is made with oscommerce. The problem I had was this

///////////////////FORUM///////////////////////////////////////////////////

I'm trying to write a contribution that includes adding extra value to my product tables.

I can get the code to work with an input field - but would ideally like to change this to a dropdown list. The code that is in question is



CODE





surferRecWeight); ?>




so instead of using tep_draw_input_field - i would like to use tep_draw_pull_down_menu. I know that the second arguement for the function tep_draw_pull_down_menu needs to be my array for the drop list. I have tried a couple of different ways to make the array ie


CODE
$ability_array = array('Expert', 'Average', 'Novice');


OR


CODE
$ability_array = array(0 => 'Expert', 1 => 'Average', 2 => 'Novice');


OR


CODE
$ability_array[0] ='Expert';
$ability_array[1] = 'Average';
$ability_array[2] = 'Novice';


either way I get the same result which is that the drop down list only has the first letter of my array element - 'e','a',' & 'n'.

does anyone know why. I know that the function allows strings more than one character ?



the code I am using to call up the function is



CODE
echo tep_draw_separator('pixel_trans.gif', '24', '15') . ' ' . tep_draw_pull_down_menu('surferRecAbility', $ability_array, $pInfo->surferRecAbility);

///////////////////END OF FORUM ENTRY//////////////////////////////


A very odd problem indeed, To solve this i wrote a hack ( probably very ugly) - after following the trail of the problem here the function that I needed to change was in admin/includes/functions/html_output.php and the function in question was tep_draw_pull_down_menu. so instead of messing this function up, after all every other pull down menu in admin seems to work fine! I created a new one. which is as follows

[code]

function tep_draw_pull_down_menu_two($name, $values, $default = '', $parameters = '', $required = false) {
$field = '';

if ($required == true) $field .= TEXT_FIELD_REQUIRED;

return $field;
}

[/code]

the only changes here are in $values[$i]. this is a much simplified version of what was there before, I take it because I created an array without keys etc that this is why it never worked. another solution therefore would be to rewrite the array in a format in keeping with oscommerce. However due to time constraints I will push on with the ugly hack ;)

Thursday, 1 May 2008

Making sure that theres no gaps in the filenames of image uploads.

The problem that needed to solved here was that users could upload there own images - and if the filename they were uploading has gaps or whitespace.
It took me a while to get my head around this one, eventhough the solution is fairly simple.
If you have solving the same problem here are a couple of links that may help. Firstly I posted this problem on a developer network forum. you can see that post at http://forums.devnetwork.net/viewtopic.php?f=1&t=82057 .
A tutorial that helps understand how file upload works in php is http://www.tizag.com/phpT/fileupload.php.

Any back to my code.

What I needed to do first was check to check if theres any spaces and replace with '_'

[code]
$img1 = str_replace ( ' ', '_', $img1 );

[/code]

then in move_uploaded_file change it to

[code]
$res = move_uploaded_file($_FILES['img1']['tmp_name'], $abpath .
$img1);
[/code]


staright forward, I have no idea now why I couldn't get my head around this straight away.

Tuesday, 29 April 2008

OSCOMMERCE Hide Categories & Products add on

I was trying to install the contribution for hiding product catergories on a heavily modded Oscommerce store -

this contribution can be found at http://www.oscommerce.com/community/contributions,5907/category,all/search,Hide+Categories+%26+Products

Following the instructions I got everything to work ok except for in the admin area, where we now have a a green and red circle that light up when the folder is active or inactive. After installing the contribution then all buttons would come up red.

the contribution code displaying these items is as follows

[code]

if ($categories['categories_status'] == '1') {
echo tep_image(DIR_WS_IMAGES . 'icon_status_greenl.gif', IMAGE_ICON_STATUS_GREEN, 12, 12) . '  ' . tep_image(DIR_WS_IMAGES . 'icon_status_red_lightl.gif', IMAGE_ICON_STATUS_RED_LIGHT, 12, 12) . '';
} else {
echo '' . tep_image(DIR_WS_IMAGES . 'icon_status_green_lightl.gif', IMAGE_ICON_STATUS_GREEN_LIGHT, 12, 12) . '' . '  ' . tep_image(DIR_WS_IMAGES . 'icon_status_redl.gif', IMAGE_ICON_STATUS_RED, 12, 12); }
?>
[/code]


after running test i discovered that the part of the code running this test

$categories['categories_status'] == '1'

was not receiveing any value. So I wrote this hack (albeit ugly and not in the preffered oscommerce format it does work !!)

FIND:
// CATEGORY STATUS
if ($categories['categories_status'] == '1') {

REPLACE WITH

$categories_id = $categories['categories_id'] ;
$selectquery = "SELECT * FROM categories WHERE categories_id = $categories_id ";
$result = mysql_query($selectquery)
or die ("Query failed");
$row = mysql_fetch_array($result);


// CATEGORY STATUS
if ($row['categories_status'] == '1') {