Friday, September 23, 2011

PHP: Output any string using JavaScript

Output any string from PHP using JavaScript document.write() function.

 
function echoWithJS($_str) {
    $str = str_replace(
        array('"', '</', chr(10), chr(11), chr(13)),
        array('\\"', '<\\/', '', '', ''),
        $_str
    );
    $out = '
        <script language="javascript" type="text/javascript">
            //<!--
            '.'document.write("'.$str.'");
            //-->
        </script>
    ';
    echo $out;
}


Thursday, September 8, 2011

Google +1 vs robots.txt

The presence of Google +1 button on a page will override robots.txt restrictions for that page. Google +1 button should only be placed with pages with content intended to be public, Google crawler will ignore robots.txt blocks for that page and fetch it.

Google's Top Contributors - page & map

Trafic web gratuit - WSTATS.NET & Google Analytics

Statistici trafic web gratuite pentru toate site-urile tale.

Informatii trafic site oferite gratuit folosind statistici Google Analytics.

WSTATS.NET - GRATIS, FARA ABONAMENT

Monday, September 5, 2011

mcedit undo command

How to undo in Midnight Commander editor: CTRL + u

Sunday, September 4, 2011

Mysql unix timestamp to date

Mysql unix timestamp to date:  FROM_UNIXTIME(unix_timestamp) function.

Example:
SELECT *, FROM_UNIXTIME(time_expire) texp FROM _queue ORDER BY texp;

Friday, September 2, 2011

PHP - Sort multi-dimensional array by one key

The following function sorts an array (with the form shown in the example below) by any key and supports all PHP sort types (SORT_STRING, SORT_NUMERIC) and sort direction.
Oldies but goldies: as years passed by, the function served well and was allways there when needed.
Obs: array_multisort() PHP's native function can be also of use for similar jobs (mehr oder weniger), but the function presented below doesn't make use of it.

Example sort array by key:
$a = array(
    array('name' => 'John', 'salary' => 1245),
    array('name' => 'Paul', 'salary' => 1105),
    array('name' => 'Ralf', 'salary' => 2232),
);
// Sort by name (string)
$a1 = ar1_sort($a, 'name', SORT_STRING);
// Sort by salary (int)
$a2 = ar1_sort($a, 'name', SORT_NUMERIC, 'desc');

function ar1_sort($a, $sort_key, $sort_type = SORT_NUMERIC, $direction = 'asc') {
    $t = array();
    foreach($a as $k => $v) {
        $t[$k] = $v[$sort_key];
    }
    asort($t, $sort_type);
   
    $res = array();
    foreach($t as $k => $v) {
        $res[] = $a[$k];
    }
   
    if($direction == 'desc') {
        $res = array_reverse($res);
    }
   
    return $res;
}


Test MySQL - nivel introductiv

Test MySQL - foarte util si recomandat pentru a invata MySQL

1) Sa se creeze tabela studenti care sa contina urmatoarele campuri:
id_student - int(11) UNSIGNED auto_increment PRIMARY_KEY
nume - varchar(100)
prenume - varchar(100)
data_nastere - date

2) Sa se insereze in tabela studenti urmatoarele date (nume, prenume, data_nastere):
'Popescu', 'Valentin', '1985-10-24'
'Ionescu', 'Marian', '1982-07-24'
'Iliescu', 'Ioana', '1985-09-10'
'Mutu', 'Adrian', '1979-04-12'

3) Sa se scrie:
a) un query care sa afiseze toti studentii al caror nume incepe cu "I"
b) un query care sa afiseze toti studentii al caror nume se termina cu "escu"
c) un query care sa afiseze toti studentii care sunt nascuti in 1985 (se foloseste functia YEAR, exemplu: SELECT YEAR(data_nastere) FROM studenti)

Baze de date relationale

4)
a) Sa se creeze tabela bac_probe care sa contina urmatoarele campuri:
id_proba - int(5) UNSIGNED auto_increment PRIMARY_KEY
proba - varchar(100)
b) Sa se insereze urmatoarele probe de bacalaureat: 'Matematica', 'Limba romana - scris', 'Limba romana - oral', 'Limba engleza - oral', 'Geografie', 'Fizica'

5)
a) Sa se creeze tabela bac_rezultate care sa contina urmatoarele campuri:
id_rezultat - int(11) UNSIGNED auto_increment PRIMARY_KEY
id_proba - int(5) UNSIGNED
id_student - int(11) UNSIGNED
nota - tinyint(2) UNSIGNED
b) Pentru a preveni existenta duplicatelor, perechea de campuri id_proba si id_student se defineste ca fiind UNIQUE (un student poate sustine o singura data o proba)
c) Sa se insereze note pentru studenti la toate probele disponibile (note intre 1 si 10); se insereaza valori pentru campurile: id_proba, id_student, nota (trebuie sa fie in total: 4 studenti * 6 probe = 24 de note)

6) Sa se obtina urmatoarele informatii:
a) Media notelor la toate probele pentru studentul Ionescu Marian (id_student = 2); se foloseste functia AVG (average = medie; exemplu: SELECT AVG(nota) FROM bac_rezultate WHERE id_student = 2)
b) Sa se afiseze numele, prenumele si media notelor la toate probele pentru fiecare student, ordonat descrescator dupa medie; se foloseste subquery
c) Sa se afiseze numele, prenumele si nota obtinuta la proba 'Fizica' de studentul Ionescu Marian; se foloseste JOIN (LEFT JOIN)