un p'tit gadget pour creer des popups avec paramètre(s)
Posté : 22 août 2005, 18:21
/**
* Sert a creer une instruction javascript pour afficher un popup avec passage de 0 à n parametres via l'url
*
*/
class Popup
{
/**
* Popup avec 0 ou 1 parametre
*
* @param string $page
* @param int $width
* @param int $height
* @param string[facultatif] $nomparam
* @param mixed[facultatif] $param
* @return string
*/
public static function pp($page,$width,$height,$nomparam=null,$param=null)
{
$code="window.open('".$page."?".$nomparam."=".$param."','', 'width=".$width.",height=".$height.",left=' + ((screen.width -".$width.")/2) + ',top=' + ((screen.height - ".$height.")/3) );";
return $code;
}
/**
* Popup avec n parametres
*
* @param string $page
* @param int $width
* @param int $height
* @param tableau_2D $param
* @return string
*/
public static function multi_pp($page,$width,$height,$param)
{
$code="window.open('".$page."?";
$code.=$param[0][0]."=".$param[0][1];
$nb_param=count($param);
for($i=1;$i<$nb_param;$i++) $code.="&".$param[$i][0]."=".$param[$i][1];
$code.="','', 'width=".$width.",height=".$height.",left=' + ((screen.width -".$width.")/2) + ',top=' + ((screen.height - ".$height.")/3) );";
return $code;
}
}
exemple:
<?php
$param=array(array('toto','param_un'),array('tutu','param_deux'),array('titi','param_trois'));
?>
<input type="button" value="kjhkh" onClick="javascript:<?php echo Popup::pp('tata.php',400,300,'tutu','param'); ?>">
<br>
<input type="button" value="kjhkjh" onClick="javascript:<?php echo Popup::multi_pp('tata.php',400,300,$param); ?>">
donc plus qu'à récupérer les paramètres avec $_GET.accessoirement le popup sera centré horizontalement(propriété 'left') et aux 2/3 en partant du bas (propriété 'top').
pour php4 il suffit d'enlever le 'public static' lors des déclarations de méthode.