Ver Mensaje Individual
  #4 (permalink)  
Antiguo 29/05/2011, 21:09
Avatar de abimaelrc
abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: Css dinamico con PHP

Bueno algo así puedes hacer

index.php
Código PHP:
Ver original
  1. <?php
  2. if(!empty($_POST)){
  3.     $zIndex = (int)$_POST['z_index'];
  4.     $_SESSION[$_POST['id']] = $zIndex;
  5.     echo $zIndex;
  6.     exit;
  7. }
  8. ?>
  9. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  10. <html>
  11. <head>
  12. <link href="style.php" media="screen" rel="stylesheet" type="text/css" />
  13. <script type="text/javascript">
  14. function http(){
  15.     if(typeof window.XMLHttpRequest!='undefined'){
  16.         return new XMLHttpRequest();
  17.     }else{
  18.         try{
  19.             return new ActiveXObject('Microsoft.XMLHTTP');
  20.         }catch(e){
  21.             alert('Su navegador no soporta AJAX');
  22.             return false;
  23.         }
  24.     }
  25. }
  26.  
  27. function request(url, params, id){
  28.     var H=new http();
  29.     if(!H)return;
  30.     H.open('post',url+'?'+Math.random(),true);
  31.     H.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  32.     H.onreadystatechange=function(){
  33.         if(H.readyState==4){
  34.             document.getElementById(id).style.zIndex = H.responseText;
  35.  
  36.             H.onreadystatechange=function(){}
  37.             H.abort();
  38.             H=null;
  39.         }
  40.     }
  41.     var p='';
  42.     for(var i in params){
  43.         p+='&'+i+'='+escape(params[i]);
  44.     }
  45.     H.send(p);
  46. }
  47.  
  48. function addListener(element, type, expression, bubbling)
  49. {
  50.     bubbling = bubbling || false;
  51.  
  52.     if(window.addEventListener) { // Standard
  53.         element.addEventListener(type, expression, bubbling);
  54.         return true;
  55.     } else if(window.attachEvent) { // IE
  56.         element.attachEvent('on' + type, expression);
  57.         return true;
  58.     } else return false;
  59. }
  60.  
  61. window.onload = function(){
  62.     addListener(document.getElementById('div1'), 'click', function(){
  63.         request('<?php echo $_SERVER['PHP_SELF']; ?>',
  64.             {
  65.                 'id':'div1',
  66.                 'z_index':prompt('z-index',<?php echo array_key_exists('div1', $_SESSION) ? $_SESSION['div1'] : 1; ?>)
  67.             },
  68.             'div1'
  69.         );
  70.     });
  71.     addListener(document.getElementById('div2'), 'click', function(){
  72.         request('<?php echo $_SERVER['PHP_SELF']; ?>',
  73.             {
  74.                 'id':'div2',
  75.                 'z_index':prompt('z-index',<?php echo array_key_exists('div2', $_SESSION) ? $_SESSION['div2'] : 2; ?>)
  76.             },
  77.             'div2'
  78.         );
  79.     });
  80.     addListener(document.getElementById('div3'), 'click', function(){
  81.         request('<?php echo $_SERVER['PHP_SELF']; ?>',
  82.             {
  83.                 'id':'div3',
  84.                 'z_index':prompt('z-index',<?php echo array_key_exists('div3', $_SESSION) ? $_SESSION['div3'] : 3; ?>)
  85.             },
  86.             'div3'
  87.         );
  88.     });
  89. }
  90. </script>
  91. </head>
  92. <body>
  93. <div id="wrap">
  94.     <div id="div1"></div>
  95.     <div id="div2"></div>
  96.     <div id="div3"></div>
  97. </div>
  98. </body>
  99. </html>

style.php
Código PHP:
Ver original
  1. <?php
  2.     session_start();
  3.     header('Content-Type: text/css;');
  4. ?>
  5. *{ margin: 0; padding: 0; }
  6. html, body{ width: 100%; height: 100%; }
  7. #wrap{
  8.     position: relative;
  9.     width: 200px;
  10.     margin: 0 auto;
  11. }
  12. #div1{
  13.     width: 100px;
  14.     height: 100px;
  15.     background-color: #000;
  16.     position: absolute;
  17.     top: 1px;
  18.     left: 1px;
  19.     z-index: <?php echo array_key_exists('div1', $_SESSION) ? $_SESSION['div1'] : 1; ?>;
  20. }
  21. #div2{
  22.     width: 100px;
  23.     height: 100px;
  24.     background-color: #f00;
  25.     position: absolute;
  26.     top: 40px;
  27.     left: 30px;
  28.     z-index: <?php echo array_key_exists('div2', $_SESSION) ? $_SESSION['div2'] : 2; ?>;
  29. }
  30. #div3{
  31.     width: 100px;
  32.     height: 100px;
  33.     background-color: #00f;
  34.     position: absolute;
  35.     top: 20px;
  36.     left: 50px;
  37.     z-index: <?php echo array_key_exists('div3', $_SESSION) ? $_SESSION['div3'] : 3; ?>;
  38. }
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos