Web Development Archive

PHP content not showing in TinyMCE

I’m developing a simple news CMS for a client and decided to try TinyMCE since it looked good and I hadn’t tried it before. I downloaded it, uploaded it the server and put the code into the CMS. I tested it by adding a news item and it worked great!

The problem was when I did the edit script – the same form put with PHP echoing the news contents in to the textarea. When TinyMCE loaded there was no content there – I could briefly see the code in the textarea before it got transformed into the WYSIWYG window.

I turns out that it was the Firefox plugin Firebug causing it to disappear – I uninstalled this and it worked fine!

jQuery and Innerfade on mouseover and mouseout

So the website I’m working on at the moment has four boxes across the bottom. One of the designers suggested that when you move your mouse over them, the fade between a series of images. You can see what I mean on the Richmond Retreats website.

My first thought was to use Innerfade with mouseover to fade in the div and start the fading. Turns out it wasn’t as simple as I thought it would be and there’s not much in the way of help on the net either. What I ended up doing was having four static images and then four hidden divs with all the images to to run using innerfade. This means I can use the hover call to fadeOut the static div, fadeIn the other div and then start innerfade. I just then fadeOut one and fadeIn the other when the mouse is moved away again. The main bit of code you’ll want is:


$(document).ready(function()
{
$("#box-a").hover(function()
{
$("#box-a-h").fadeIn('fast');
$("#box-a-h").innerfade({
speed: 2000,
timeout: 3000,
});
},function()
{
$("#box-a-h").fadeOut("slow");
});
});

Hope someone finds this useful.

Using COUNT in the WHERE clause of MySQL statements

I’ve got a huge report for the Medicology MTI that has been running extremely slow due to the amount of data and queries. I’ve been trying to speed it up to make accessing and analysing the results easier.

The users have to answer 56 questions to complete the survey. This means that most for the calculations there’s currently an “if” statement to check they completed all 56 questions and then skip the row of the mysql result set if they haven’t.

I haven’t checked if it will make it quicker but surely checking the amount of answers completed in the SQL query will result in less data and a speed increase. The problem is that the COUNT(*) I’m using to get the number of answers is in the SELECT part of the statement which is executed after the WHERE clause, which is where it needs to be:

SELECT id, COUNT(id) AS cnt FROM answers WHERE cnt>=56 GROUP BY id

The above returns an error. What you need to do is modify the query as follows:

SELECT id, COUNT(id) AS cnt FROM answers WHERE cnt>=56 GROUP BY id HAVING cnt>=56

That will return the required result set.

ga_shade – a white DIV breaking my site

I just went to our main company website, Medicology, and there was a large white semi-transparent block over the top of the entire screen. Since I use something like this for displaying AJAX content I panicked a bit and then dived into my JS and CSS files to see what was going on.

After finding nothing out of the ordinary I searched the entire set of source files for “alpha” hoping to to find a snippet of CSS that was causing my woes. Unfortunately it came back with nothing. I then decided to do the same with the source from one of the pages in Firefox and came back with a div at the bottom of the page with the ID “ga_shade”.

A quick Google led me to find that it is Google Analytics placing it there as part of the whole “content overlay” thing they do. Problem is that I wasn’t using content overlay and as such the menu bar for it wasn’t available and I couldn’t turn it off, let alone click on any links. Not happy. And suprised that Google can make slip-ups of this kind.

Sort yourselves out boys. Don’t just assume that because it works in IE6 it works in everything – try testing stuff in FF for us.

IE6 and PNG transparency woes. Still…

We have several sites we’ve been developing for a range of conferences we are running (Safeguarding and Q, G & E being two) and to help reduce the development time, our designer used a few PNGs so that we could use a standard layout template and then alter a CSS sheet to create a new site. This plan was working well until I finally found some time to do a bit of housekeeping and checked Google Analytics.

I was quite shocked to find that almost half of our users are still visiting us with the rediculous IE6, complete with it’s security flaws, lack of compatibility and other problems. The main problem in this instance is the lack of proper support for PNGs and in particular, alpha transparency. This was causing our nice design with the rounded corners to be displayed as big blocks of light grey all over the place.

Apart from the slight anoyance that the only reason these people are still on IE6 is because they are ignoring or not using Windows Update, and are therefore probably part of a Botnet, it means that IE’s awful legacy is still with us and is likely to be so for some years to come until these PCs finally die. We therefore STILL need to code all our new sites for IE with all the hacks and tweaks we’ve been using in the past.

In this case I found an off the shelf solution that worked quite well for us in the excellent SuperSleight that for us, allowed us to keep most our PNGs with the transparency enabled and just change a few of the core images. I never did look two hard at why all the PNGs weren’t fixed (we replaced the logo with a JPEG) but maybe your site will be fixed without a hitch.

“Last month” incorrect with PHP Date & strtotime functions

I use a lot of business style reporting in our companies website where displaying figures by month is important. However, at certain points in the year using the PHP date and strtotime functions to pull data from our DB works incorrectly. For example, today and tomorrow (30th & 31st March), using “last month” in the strtotime function will actually return the timestamp for 2nd & 3rd March, not the 27th & 28th February that the developer may have been expecting.

The reason for this? It’s because the Romans were superstitious of even numbers and thus we have a very disorganised calendar system that doesn’t always match up with the decimal and binary way we now work in.

If you do want to get the timestamp of a date that falls in the previous month, the below code should help:

$last = strtotime(date(‘Y-m-01′).’ -1 month’);

This will (at the moment) return the 1st February.

The 15% VAT Change: Software development lessons learned

With the suprise announcement that the government is going to reduce the VAT rate to 15%, there was much media coverage about how this may or may not help the economy, whether the high street will actually pass on the savings to consumers and as per usual, nothing much about how this affects the IT services that support the ever-increasing amount of small businesses that are moving online.

I’m sure I wasn’t alone in suffering a feeling of dread when I heard that the new rate is going to go into effect on the 1st December 2008, leaving us just 5 days to plan, implement & test any changes that need to be made. For the majotity of the businesses I run sites for, this was a minor thing since I tend to follow these simple rules:

  • Store the VAT charged with the payment record – don’t rely on coding to calculate it post-transaction
  • Store the VAT amount in a configuration file or database value – never hard code it

In these cases all I need to do is change the VAT amount on the morning of the 1st and everything will be fine. There is one company though that wasn’t so easy to change.

VAT & Services
Our primary client, Medicology, provides services in the form of training courses. People decide to book onto one of their courses, usually well in advance and they receive a 28 day invoice which is then paid. In this companies case, the tax point (the point at which the VAT is calculated and paid) is the point at which the course is run. This has major implications for them:

  • If a customer had booked onto a course in September, they would have been invoiced at 17.5%
  • However, if the course they are attending runs in January, they should have been invoiced at 15%
  • This would therefore require the original invoice to be credit noted at n17.5% and then a new invoice generated at 15%

This was potentially devastating for the company as they may have hundreds of invoices, some paid, some unpaid which need credit noting, re-invoicing and in some cases, refunds issued and new payments taken.

Luckily, due to the short notice, the HMRC has said they will waive this process and let the existing invoices stand. This will save the company hundreds of man-hours and thus thousands of pounds. From a customer point of view, they are not loosing out by paying the extra VAT since the majority of courses are paid for by the NHS Trusts who can claim back the VAT, whatever the amount.

I will be keeping a close eye on the press for what will happen in 13 months time when the rate will increase and my guess is, the government won’t be so kind about letting people off paying additional VAT. In this case, their courses running in 2010 may have to be re-issued so as soon as the new VAT rate is announced, I will be making changes ot the system to calculate the VAT amount based on the tax point as opposed to the current amount.

I just hope they decide the amount sooner rather than later

IE7 File Uploads don’t work – nothing happens

So, Microsoft have a released a new Internet Explorer and as per usual, we’re going to have to re-code all our sites for it. So, what’s this versions problem: file uploads.

I have a site that uploads a file in the usual HTML form way and then the PHP on the server checks if it’s a JPEG and uses copy() to move it to a desired folder. I tested it in FireFox and everything was working fine and since it’s so simple a script I left it at that. Then a client with IE7 called me because he was trying to upload a jpeg and nothing was happening. I fired up IE and gave it a go and when I clicked on the Submit button it just sat there, doing sod all.

A bit of digging and I found that the problem was because I’d included the following in the code for the Submit Button:

onClick=”this.disabled = true;”

This was being executed by IE and then it appears it just sits there and forgots you clicked Submit. You can give IE a good kicking and get it working again by changing it to:

onClick=”this.disabled = true; document.form.submit();”

I hate IE and M$

1and1 Dedicated server and PEAR

I’ve recently had to do a complete reinstall on our 1and1 Root Linux server which is easy enough in itself as it’s just a case of going into the Control Panel and hitting Server Re-Image alogn with your choice of operating system. I went for Fedora Core 6 and within an hour everything was up and running. Within another hour I had my files uploaded (4Gb including images), MySQL databases loaded and my custom httpd.conf loaded. After that I thought the only hard bit would be the extras I had installed but with a quick “yum install ImageMagick” and “yum install webmin” then using Webmin to set up the firewall properly, everything was good to go.

With the sites back up I just needed to do a few quick checks to see if various bits of the website were working. Everything was fine except for PHP sending out emails. I knew that newer versions of Fedora were supposed to use QMail and not Sendmail so I headed off into the system to see if I could track down the problem. After a bit of messing around it seemed the mail server was fine (even with the MTA set as QMail) but nothing would be sent out using PHP. By saving the MTA to Sendmail I could get emails out using the mail() function but not using PEAR’s Mail package. Enabling ErrorReporting on one of my scripts threw up the errors:

Warning: main(Mail.php): failed to open stream: No such file or directory in
/var/www/html/email.php on line 15

Fatal error: main(): Failed opening required ‘Mail.php’
(include_path=’.:/usr/local/share/php/pear’) in
/var/www/html/email.php on line 15

I then loaded up a phpinfo() script and noticed the following line:

–without-pear

For some reason they’ve decided to be overly cautious and install PHP without PEAR support so even though PEAR was showing as being installed and commands such as “pear install Mime” were working, something with PHP was a bit pear shaped (get it, arf arf!). It turns out that the solution is simply to open up /etc/php.ini and add the following line:

include_path=”.;/usr/share/pear”

Everything seems to be working now, a bit more testing might be in order though

PHP Translation: English to French

I’m just about to get started on a new project for a client and it’s going to be my first multi-lingual site. The site is Visit Pyrenees and it’s a holiday home and hotel advertising site based around three villages in the Eastern Pyrenees. As far as a brand new project goes, it shouldn’t be too complicated and the only technically difficult bit will be the French and English versions.

As the site is aimed at UK tourists wanting to visit the area, the default language will be English, but I also want a French version for a number of reasons:

  • It adds to the USP of the site by attracting visitors speaking both languages
  • There will be a slight SEO benefit from the dual language site
  • Some of the advertisers will only speak French and will be editing their advert which will then need translating

There are basically two section to the site: the adverts for the various places of accommodation and the static information pages. Since I don’t speak much French, I’d like the static areas of the site to be editable by a none technical but French speaking person (such as the site owner). It therefore makes sense that the easiest way of implementing the site using a wysiwyg editor that lets the user select their language and then enter the text and any other HTML entities into the editor before saving it. The PHP script will then take the text and then create a version in the other language.

When I find something suitable that works, I’ll post a link or a zip file for download if I have to modify it.