Quick sort for associative arrays

16 May 2009 In: Algorithm, Off Topic, PHP

Here is the way of quick sort in associative arrays;

<? 
 
function qsort($a,$f) { 
       qsort_do(&$a,0,Count($a)-1,$f); 
} 
 
function qsort_do($a,$l,$r,$f) { 
       if ($l < $r) { 
               qsort_partition(&$a,$l,$r,&$lp,&$rp,$f); 
               qsort_do(&$a,$l,$lp,$f); 
               qsort_do(&$a,$rp,$r,$f); 
       } 
} 
 
function qsort_partition($a,$l,$r,$lp,$rp,$f) { 
       $i = $l+1; 
       $j = $l+1; 
 
       while ($j <= $r) { 
               if ($f($a[$j],$a[$l])) { 
                       $tmp = $a[$j]; 
                       $a[$j] = $a[$i]; 
                       $a[$i] = $tmp; 
                       $i++; 
               } 
               $j++; 
       } 
 
       $x = $a[$l]; 
       $a[$l] = $a[$i-1]; 
       $a[$i-1] = $x; 
 
       $lp = $i - 2; 
       $rp = $i; 
} 
 
?>

PHP Optimizing Tips

14 May 2009 In: PHP

Here is the list of 43 short tips you can use for writing an optimized and more efficient PHP code:

1. If a method can be static, declare it static. Speed improvement is by a factor of 4.
2. echo is faster than print.
3. Use echo’s multiple parameters instead of string concatenation.
4. Set the maxvalue for your for-loops before and not in the loop.
5. Unset your variables to free memory, especially large arrays.
6. Avoid magic like __get, __set, __autoload
7. require_once() is expensive Read the rest of this entry »

Searching strings in a multi-dimensional array

12 May 2009 In: PHP

I’m working on a security and trying to make searches in a multi-dimensional array
Here is the function;

<?php
function multi_array_search($needle, $haystack, $key, &$result, $searchlevel = 0) { 
  while(is_array($haystack) && isset($haystack[key($haystack)])) {
    if($searchlevel == 0 && key($haystack) == $key && $haystack[$key] == $needle) {
      $result = $haystack;
    } elseif($searchlevel > 0) {
      multi_array_search($needle, $haystack[key($haystack)], $key, $result, $searchlevel - 1);
    }
    next($haystack);
  }
}
?>

Example;
2-dimensional array, search by both key and value
$arr1 = array(
1 => array(‘id’ => 1, ‘name’ => ‘Ersin’, ‘gender’ => ‘male’),

12 => array(‘id’ => 12, ‘name’ => ‘Emel’, ‘gender’ => ‘female’),
);

<?php array_search_in_level('Emel', $arr1, 'name', $result, 1); ?>

$result is:
array(
‘id’ => int 12
‘name’ => string ‘Emel’ (length=4)
‘gender’ => string ‘female’ (length=4)
)

Free CSS Themes

12 May 2009 In: Bookmarks, CSS

I made a quick list for Free CSS theme sites.
Here is the list;

http://www.freecsstemplates.org/css-templates/

http://www.free-css-templates.com/

http://www.freecsstemplates.org/

http://csscreme.com/freecsstemplates/

http://www.templatemo.com/

http://www.solucija.com/free-templates

Introduction

The following HOWTO will show you exactly how to install the following packages on a Debian Etch or Ubuntu 7.06 system:

  • FFmpeg
  • FFmpeg-PHP
  • Mplayer + Mencoder
  • flv2tool
  • LAME MP3 Encoder
  • AMR (for 3gp file conversions)
  • Libogg
  • Libvorbis

Read the rest of this entry »

How to change language of Debian

7 May 2009 In: Linux

Hi,
İf you want to change debian’s system language you must follow these steps;

sudo apt-get update
sudo apt-get install debconf
sudo dpkg-reconfigure locales

Or you could add something like the following lines to /etc/environment

LANG=”en_US.UTF-8″
LANGUAGE=”en_US:en”

If you want different locales for different users, you can add that to their
.bashrc files

Here’s some links that touch on what you’re looking for:

http://gallery.menalto.com/wiki/Debian_locale_HowTo

http://melkor.dnp.fmph.uniba.sk/~garabik/debian-utf8/howto.html

Using Wilcard with Unlink in PHP

29 Apr 2009 In: PHP

I just found a way to using wilcard with unlink in PHP.
Here is the scenario;
files might be:
120a-some_file1.jpg
210w-some_file1.jpg
some_file123.jpg
480h-some_file1.jpg
somefile.doc

if you want to delete;
120a-some_file1.jpg
200w-some_file1.jpg
480h-some_file1.jpg

you can use this code;

<?php
foreach( glob('*-some_file1.jpg') as $file ){
        unlink($file);
}
?>

Change/Fix Wrong MySQL Encoding

24 Apr 2009 In: MySQL

Sometimes, especially when moving data from one server to another, you might find that you have your MySQL database incorrectly encoded. Problem with this first show itself if you have the database and encoded in one charset set to display your website in another. If this is the case then you will find strange characters appearing in your text, especially when using punctuation marks. If you are Unwilling or unable to change the character encoding on the site then you need to change how the data is encoded in the database.

The most common sort of thing you might want to do is change from iso-8859-1 (or windows-1252) to UTF-8. This can be done in one of two ways.

The first way is to simply alter the table so that the column contains a different charset.

ALTER TABLE table MODIFY col1 VARCHAR(50) CHARACTER SET 'utf8';

However, if your database has already been set up and your data has already been inserted in the wrong format then you can also update the data in the column using the CONVERT command. The following snippet turns our latin1 data into uncoded binary data and then into utf8.

UPDATE table SET col1=CONVERT(CONVERT(CONVERT(col1 USING 'latin1') USING BINARY) USING 'utf8');

You should also make sure that the connection to the database is done through a specific character set. This is done by using the
Read the rest of this entry »

Javascript CSS style changer

22 Apr 2009 In: Ajax, CSS

It may be useful sometimes to provide your visitors with the option to change the appearance of your website. Perhaps you may want to provide theme support for your site. Or you may simply want to provide a bigger font size version of your site for older users who may not be able to read the small-sized text you use by default. This article describes how you can use JavaScript to dynamically change the cascading style sheets, or CSS, of your website.

Here we go;
Read the rest of this entry »

Heatmap for your website

21 Apr 2009 In: Ajax, Bookmarks, PHP

ClickHeat is a visual heatmap of clicks on a HTML page, showing hot and cold click zones.

clickheat-screenshot
Requirements
- on the browser’s client: Javascript (tested on Firefox 2.0, Internet Explorer 6 and 7, Konqueror…)
- on the server: either Linux or Windows (since ClickHeat 1.3 release), Apache or Lighttpd (other may work fine), PHP, the graphic library GD2 (PNG support needed).

Features

- Low logging activity: a very few function calls to log a click, no server load rise should be noticed (have a look at Performance & optimization)
- A keyword is used to define the page upon Javascript code load, allowing you to group same pages.
- Screen sizes and browsers are logged, making possible the tracking of liquid CSS layouts (100% used width).

Download

Demo (user:demo pwd:demo)


Sponsors