Webdesign,code & ...stuff!

Aug 22, 2009

Javascript: window.open pop-up at center

In javascript - window.open opens a popup that by default opens at random location of the window. Below is a javascript function that will open a pop-up in the center of the client window. Hope it may save many additional lines of javascript code.

function popupwindow(windowUri, windowHeight, windowWidth, windowName)
{
var centerWidth = (window.screen.width - windowWidth) / 2;
var centerHeight = (window.screen.height - windowHeight) / 2;

newWindow = window.open(windowUri, windowName, 'resizable=0,width=' + windowWidth +
',height=' + windowHeight +
',left=' + centerWidth +
',top=' + centerHeight);

newWindow.focus();
return newWindow.name;
}



Please notice parameters that are passed to window.open.

Aug 12, 2009

PHP_EOL : PHP End Of Line

PHP_EOL is apparently used to find the newline character in a cross-platform-compatible way, so it handles DOS/Mac/Unix issues.

Most of the PHP I write is for generating HTML, and I use <br /> instead of actual newlines, so haven't used (I don't even know) this constant before. Just until I need to
explode a block of string to be separated by the end of the line. It's something like the file() function can do but I only want to parse a string, I dont want to put it into another file.

There were no \r\n added to the end of each line on the string that would be used as identifier for the explode. I know I can just add it myself but the string is dynamic, it's from a user input.

So I googled for some workaround but no luck, I don't know, may be its my search keywords. I wander at php.net and landed on
this page.
There I found about PHP_EOL, so I tried to use it and voila, works like magic.

Sample code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
$str
='IT sa CITE Batch 6
Glendon Alimin
John Reeve Belarmino
Carl Anthony Campos
Alex Culango
Joenar Dalumpines
Ryan Dupay
Victoriano Eco II
Ronald Enclonar
Johnric Godinez
Jerry Laborte
Jim Carl Luzano
Licerio Maeza
Joseph Marvin Magdadaro
Ramil James Mangapis
Ryan Christoper Maramot
Mitchell Masinas
Paulino Muñez
Francis Ray Ranile
Reynold Salceda
Jeb Agustin Saldariega
Rholyn Sampan
Kenneth Sotto
Michael Sumayang
Chrissan Versoza
Roel Villarba '
;

$arr=explode(PHP_EOL,$str);
print_r($arr);
?>


the above code returns:
Array
(
[0] => IT sa CITE Batch 6
[1] => Glendon Alimin
[2] => John Reeve Belarmino
[3] => Carl Anthony Campos
[4] => Alex Culango
[5] => Joenar Dalumpines
[6] => Ryan Dupay
[7] => Victoriano Eco II
[8] => Ronald Enclonar
[9] => Johnric Godinez
[10] => Jerry Laborte
[11] => Jim Carl Luzano
[12] => Licerio Maeza
[13] => Joseph Marvin Magdadaro
[14] => Ramil James Mangapis
[15] => Ryan Christoper Maramot
[16] => Mitchell Masinas
[17] => Paulino Muñez
[18] => Francis Ray Ranile
[19] => Reynold Salceda
[20] => Jeb Agustin Saldariega
[21] => Rholyn Sampan
[22] => Kenneth Sotto
[23] => Michael Sumayang
[24] => Chrissan Versoza
[25] => Roel Villarba
)

Aug 7, 2009

Reindex Array in PHP

If you want to re-index starting to zero, simply do the following:

1
2
3
4
5
<?php
$arr
=array(9=>'Reynold',8=>'Tabal',4=>'Salceda');
$start_zero = array_values($arr);
print_r($start_zero);
?>

Array(
[0] => Reynold
[1] => Tabal
[2] => Salceda
)

If you need it to start at one, then use the following:

1
2
3
4
5
<?php
$arr
=array(9=>'Reynold',8=>'Tabal',4=>'Salceda');
$start_one = array_combine(range(1, count($arr)), array_values($arr));
print_r($start_one);
?>

Array(
[1] => Reynold
[2] => Tabal
[3] => Salceda
)

Here are the manual pages for the functions used:
array_values()
array_combine()
range()