Archive for the ‘Webdevelopment (english)’ Category

Firefox 3.5.x problem with css files parsed by php

Monday, February 8th, 2010

A few years ago I built a Website, styled with CasCading-Stylesheets.
Because of using vars in this external Stylesheet, i had to rename it from styles.css to styles.php.

So it’s being parsed by the PHP Interpreter, but the file headers has to be modified because it should not be a real php-file, it should to be used like a css file… this header modification works fine for that:
header('content-type: text/css');

The Website works fine for years… up to now.

I upgraded my Firefox to Version 3.5.7 and when i now visit the Website, the external CSS is not being used anymore! In Safari there is no Problem! In IE there is no Problem!

I tried to find other Websites with php files used as css and i saw there is the same problem with my Firefox!

(Edited)
okay I just looked at one other Website, but there was really the same problem… bad luck.

But now I found the problem… There was given a wrong charset in my php.ini file.
(I know, I should use UTF-8… but this Website is very old and my first real project)
We’ve made a transfer of the Website to another Server a few days ago and in the php.ini file i have written ISO-8559-1 instead of ISO-8859-1.

I dont know why, but this is the reason why the CSS has completely not been interpreted by Firefox, Safari and IE did its job…

select_year´s and select_month´s option selected by params

Friday, July 31st, 2009

I searched for an opportunity to have the options of a selectbox selected that have been chosen by the user and sent by its form.

1. Checking if the form with the selectboxes was sent or not.
If it has not been sent, i use the current month and year. Otherwise the params['date'] is filled and can be used.

<% if params[:date] == nil
	@month = Time.new.month
	@year = Time.new.year
else
	@month = params[:date][:month]
	@year = params[:date][:year]
end %>

2. The selectboxes are generated and the selected option is the local variable (current date, or detected from params)
When using params we have a string that must be converted to an integer with to_i method.

<%= select_month(@month.to_i, {:add_month_numbers => true }) %>
<%= select_year(@year.to_i, {:start_year => 2006}) %>

short PHP if statement written in one line (ternary operator)

Saturday, June 6th, 2009

Its an easy way to write short If-Statements in PHP, also known as the ternary operator.
Ternary because of the three operands: a condition, a result for true, and a result for false.

But I always need to look up this snippet, so now I write it down for reference.

$notice = ($price < 20) ? 'cheap' : 'expensive';

This code is equal to this:

if($price < 20) {
    $notice = "cheap";
}
else {
    $notice = "expensive";
}

If the variable “price” is less than 20, the variable “notice” is set to “cheap”.
Otherwise (price is equal, or greater than 20) the notice is set to “expensive”.

domain matching regex

Monday, May 11th, 2009

A regular expression that validates a given domain without its top level domain. I didnt need to validate the tld because this comes from a selectbox, so its predefined and always valid. The http protocol or the second level domain www. are both optional, it can be written or not.

$regex = "/^(?:(http:\/\/)?)(?:(w{3}\.)?)([A-Z0-9\-".utf8_encode("äÄöÖüÜ")."]{3,})$/i";
if(!preg_match($regex,$domainname)) {
	return false;
}

So the string can look like this:
http://www.liquidbass or www.liquidbass or http://liquidbass

detailed Description:

/^

This is the beginning of the regex. The ^ is used for searching all matches from the beginning of the given string.

(?:(http:\/\/)?)(?:(w{3}\.)?)

Here we have the optional protocol and the optional second level domain www. …

([A-Z0-9\-".utf8_encode("äÄöÖüÜ")."]{3,})

This part of the regex allows all characters from a to z, hyphens and all numerics from 0 to 9.
Additionally german special chars are allowed too.
All these possible characters must match a stringlength of minimum 3.

$/i

the dollar stands for the strings end, so it would be searched for matches from beginning to the end.
the /i stands for case sensivity - the given string can be in capital letters or in lowercase.

truncate text-strings without cutting words

Thursday, April 2nd, 2009

This is an easy way to truncate strings without cutting words after n characters.

bad one:
Lorem ipsum dolor sit amet, hy…

good one:
Lorem ipsum dolor sit amet, hymenaeos …

$string = "Lorem ipsum dolor sit amet, pellentesque wisi ut congue eget quam.";
$strLength = 30;
echo substr ( $string, 0, strrpos ( $string, ' ', - ( strlen ( $string ) - $strLength ) ) );

set multilingual pageTitle in a view or element

Sunday, March 22nd, 2009

I have searched for a long time until i found the reason why i couldnt make a pageTitle multilingual in a view or in an element.

First I tried to edit the title in this way and became unhappy because it was written in the content area of my page and not in the titletag.

$this->pageTitle = __('new PageTitle');

The second parameter “true” will avoid displaying the text directly.

$this->pageTitle = __('new PageTitle', true);

So, when you write it in this way, it works!

Installing Prototype/Scriptaculous into your CakePHP App

Saturday, March 21st, 2009

I wanted to install the prototype framework with the scriptaculous effects to my cakePHP Application, but after uploading the prototype files into the webroot/js folder and linking them in the layout, i got a failure message like “Undefined variable: javascript”.

This variable has to be defined in the Applications Helper, so you can put this line of code in your AppHelper class. If you dont know what to do: create a file named “app_helper.php” in your /app folder and insert these lines of code.

class AppHelper extends Helper {
	var $helpers = array('Html','Javascript','Ajax');
}

The $javascript variable is now available and you can insert your javascript files (the prototype framework in my excample) like this in the head of your layout.

		echo $javascript->link('scriptaculous/lib/prototype');
		echo $javascript->link('scriptaculous/src/scriptaculous');

		// Scripts from a view
		echo $scripts_for_layout;

apache´s access.log parsing

Tuesday, March 10th, 2009

Sure, this is just a little thing, but i liked to show the apaches long lined access.log in a better readable way.

Just opening the access.log file with PHP and parsing line per line….


$file = "/path/to/access_log";
@$fp = fopen($file,"r");

if (!$fp) {
    echo $file ."not existent!\n";
}
else {
    while (!feof($fp)) {
        $zeile = fgets($fp, 230);
        $buffer  = explode(" ", $zeile);

        $datum = substr($buffer[3], 1);        

        echo "IP-Address: ".$buffer[0]."\n";
        echo "Date: ".$datum."\n";
        echo "DeliveredPage: ".$buffer[6]."\n";
        echo "Backlink: ".$buffer[10]."\n";
        echo "User-Agent: ".$buffer[11]."\n";
        echo "

\n"; } } @fclose($fp);

toggle checkboxes listed in groups

Sunday, March 8th, 2009

I have just needed a toggling function made with javascript for checkboxes wich are grouped in lists. It should look like a hierarchical tree-like structure. When I check the box of my first tree-category, all related subcategories automatically must getting checked too.

I have used the prototype framework for other functionalities on the website, so I preferred to take use of it for helping me by developing this. Prototype framework comes with form functions like getInputs(), but this function gets completely all input fields of a form and not from a specified list, so this did not help me in this situation.

This is a silhouetted visualization of my grouped checkboxes:

  1. [x] Category 1
    1. [x] product 1
    2. [x] product 2
    3. [x] product 3
  2. [ ] Category 2
    1. [ ] product 1
    2. [ ] product 2
    3. [ ] product 3

(more…)