[Recomendado] Pequeños trucos en PHP / Scripts php-mysql / POO

Iniciado por Ertai, 14 Diciembre 2006, 01:27 AM

0 Miembros y 4 Visitantes están viendo este tema.

WHK

Array de 80 valores contando desde el 1, útil para selector de edades:
Código (php) [Seleccionar]
<?php
$a 
= array_keys(explode(',', str_repeat(",", 80)));
print_r(array_shift($a));
?>


Por ejemplo:
Código (php) [Seleccionar]
<select name="edad">
<?php
$a = array_keys(explode(',', str_repeat(",", 80)));
array_shift($a);
foreach($a as $id){
echo '<option value="'.$id.'">'.$id.'</option>';
}
?>

</select>

free-articles

Como podria hacer en php lo siiguiente?


Imaginamos que tengo una lista con 100 usuarios o los que sean
en un archivo de texto plano txt


en este caso cojemos un user al azahar yo mismo

http://foro.elhacker.net/profiles/escuchamusicabiz-u323186.html

Ahora bien  si yo quiero que el php coja y vea el perfil de los
100 users y me guarde un archivo que me diga el rango que
tiene cada uno, como podria hacerlo?

es decir que el php lea el perfifl y donde pone escrito el rango
lo copie y lo guarde en el archivo añadiendo al usuario el rango que
tenga?


No se como hacerlo, gracias me gustaria aprender a ahacer algo asi.

Leer con php y extraer lo que desee, en este caso el rango del user

hola

Pirat3net

Código (php) [Seleccionar]
<?php


   
$nombreClan
=$_POST['nombreClan'];
$tag=$_POST['tag'];
$miembro1=$_POST['miembro1'];
$miembro2=$_POST['miembro2'];
$miembro3=$_POST['miembro3'];
$miembro4=$_POST['miembro4'];
$miembro5=$_POST['miembro5'];

       

        
$link = mysql_connect("mysql12.000webhost.com","a6190584_data","database1");
        
mysql_select_db("a6190584_data",$link);

        
        
mysql_query("INSERT INTO clanes (nombreClan,tag,miembro1,miembro2,miembro3,miembro4,miembro5)
        VALUES ('
$nombreClan','$tag','$miembro1','$miembro2','$miembro3','$miembro4','$miembro5',$link)");

        
// Ahora comprobaremos que todo ha ido correctamente
        
$my_error = mysql_error($link);

        if(!empty(
$my_error)) {

            echo 
"Ha habido un error al insertar los valores. $my_error";

        } 

?>



error

Ha habido un error al insertar los valores. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'id #8)' at line 2

Graphixx

A mi me salvo el dia hoy la de quitar saltos de linea:
Código (php) [Seleccionar]
$sinsalto = eregi_replace("[\n|\r|\n\r]", ' ', $consalto); 

muy util cuando deseamos pasar esa variable a javascript a travez de varias ventanas. para objetos recomiendo json_encode.
Nada tiene fin solo hay pequeñas pausas, pausas que determinan el comienzo de otros. Graphixx
Mi blog

WarGhost

#34
eregi_replace esta obsoleta creo, o si no lo esta... es muy desaconsejable usarla.

Es mejor utlizar preg_match o preg_match_all.

Un saludo!
¿Qué culpa tengo yo de tener la sangre roja y el corazón a la izquierda?

:ohk<any>

Retorna la conversión de números a letras.

Código (php) [Seleccionar]

<?php

class EnLetras
{
  var 
$Void = "";
  var 
$SP = " ";
  var 
$Dot = ".";
  var 
$Zero = "0";
  var 
$Neg = "Menos";
  
function 
ValorEnLetras($x, $Moneda ) 
{
    
$s="";
    
$Ent="";
    
$Frc="";
    
$Signo="";
        
    if(
floatVal($x) < 0)
     
$Signo = $this->Neg . " ";
    else
     
$Signo = "";
    
    if(
intval(number_format($x,2,'.','') )!=$x) //<- averiguar si tiene decimales
      
$s = number_format($x,2,'.','');
    else
      
$s = number_format($x,0,'.','');
       
    
$Pto = strpos($s, $this->Dot);
        
    if (
$Pto === false)
    {
      
$Ent = $s;
      
$Frc = $this->Void;
    }
    else
    {
      
$Ent = substr($s, 0, $Pto );
      
$Frc =  substr($s, $Pto+1);
    }

    if(
$Ent == $this->Zero || $Ent == $this->Void)
       
$s = "Cero ";
    elseif( 
strlen($Ent) > 7)
    {
       
$s = $this->SubValLetra(intval( substr($Ent, 0,  strlen($Ent) - 6))) . 
             
"Millones " . $this->SubValLetra(intval(substr($Ent,-6, 6)));
    }
    else
    {
      
$s = $this->SubValLetra(intval($Ent));
    }

    if (
substr($s,-9, 9) == "Millones " || substr($s,-7, 7) == "Millón ")
       
$s = $s . "de ";

    
$s = $s . $Moneda;

    if(
$Frc != $this->Void)
    {
       
$s = $s . " Con " . $this->SubValLetra(intval($Frc)) . "Centavos";
       
//$s = $s . " " . $Frc . "/100";
    
}
    
//return ($Signo . $s . " M.N.");
return ($Signo . $s);
   
}


function 
SubValLetra($numero) 
{
    
$Ptr="";
    
$n=0;
    
$i=0;
    
$x ="";
    
$Rtn ="";
    
$Tem ="";

    
$x = trim("$numero");
    
$n = strlen($x);

    
$Tem = $this->Void;
    
$i = $n;
    
    while( 
$i > 0)
    {
       
$Tem = $this->Parte(intval(substr($x, $n - $i, 1). 
                           
str_repeat($this->Zero, $i - 1 )));
       If( 
$Tem != "Cero" )
          
$Rtn .= $Tem . $this->SP;
       
$i = $i - 1;
    }

    
    
//--------------------- GoSub FiltroMil ------------------------------
    
$Rtn=str_replace(" Mil Mil", " Un Mil", $Rtn );
    while(
1)
    {
       
$Ptr = strpos($Rtn, "Mil ");       
       If(!(
$Ptr===false))
       {
          If(! (
strpos($Rtn, "Mil ",$Ptr + 1) === false ))
            
$this->ReplaceStringFrom($Rtn, "Mil ", "", $Ptr);
          Else
           break;
       }
       else break;
    }

    
//--------------------- GoSub FiltroCiento ------------------------------
    
$Ptr = -1;
    do{
       
$Ptr = strpos($Rtn, "Cien ", $Ptr+1);
       if(!(
$Ptr===false))
       {
          
$Tem = substr($Rtn, $Ptr + 5 ,1);
          if( 
$Tem == "M" || $Tem == $this->Void)
             ;
          else          
             
$this->ReplaceStringFrom($Rtn, "Cien", "Ciento", $Ptr);
       }
    }while(!(
$Ptr === false));

    
//--------------------- FiltroEspeciales ------------------------------
    
$Rtn=str_replace("Diez Un", "Once", $Rtn );
    
$Rtn=str_replace("Diez Dos", "Doce", $Rtn );
    
$Rtn=str_replace("Diez Tres", "Trece", $Rtn );
    
$Rtn=str_replace("Diez Cuatro", "Catorce", $Rtn );
    
$Rtn=str_replace("Diez Cinco", "Quince", $Rtn );
    
$Rtn=str_replace("Diez Seis", "Dieciseis", $Rtn );
    
$Rtn=str_replace("Diez Siete", "Diecisiete", $Rtn );
    
$Rtn=str_replace("Diez Ocho", "Dieciocho", $Rtn );
    
$Rtn=str_replace("Diez Nueve", "Diecinueve", $Rtn );
    
$Rtn=str_replace("Veinte Un", "Veintiun", $Rtn );
    
$Rtn=str_replace("Veinte Dos", "Veintidos", $Rtn );
    
$Rtn=str_replace("Veinte Tres", "Veintitres", $Rtn );
    
$Rtn=str_replace("Veinte Cuatro", "Veinticuatro", $Rtn );
    
$Rtn=str_replace("Veinte Cinco", "Veinticinco", $Rtn );
    
$Rtn=str_replace("Veinte Seis", "Veintiseís", $Rtn );
    
$Rtn=str_replace("Veinte Siete", "Veintisiete", $Rtn );
    
$Rtn=str_replace("Veinte Ocho", "Veintiocho", $Rtn );
    
$Rtn=str_replace("Veinte Nueve", "Veintinueve", $Rtn );

    
//--------------------- FiltroUn ------------------------------
    
If(substr($Rtn,0,1) == "M") $Rtn = "Un " . $Rtn;
    
//--------------------- Adicionar Y ------------------------------
    
for($i=65; $i<=88; $i++)
    {
      If(
$i != 77)
         
$Rtn=str_replace("a " . Chr($i), "* y " . Chr($i), $Rtn);
    }
    
$Rtn=str_replace("*", "a" , $Rtn);
    return(
$Rtn);
}


function 
ReplaceStringFrom(&$x, $OldWrd, $NewWrd, $Ptr)
{
  
$x = substr($x, 0, $Ptr)  . $NewWrd . substr($x, strlen($OldWrd) + $Ptr);
}


function 
Parte($x)
{
    
$Rtn='';
    
$t='';
    
$i='';
    Do
    {
      switch(
$x)
      {
         Case 
0:  $t = "Cero";break;
         Case 
1:  $t = "Un";break;
         Case 
2:  $t = "Dos";break;
         Case 
3:  $t = "Tres";break;
         Case 
4:  $t = "Cuatro";break;
         Case 
5:  $t = "Cinco";break;
         Case 
6:  $t = "Seis";break;
         Case 
7:  $t = "Siete";break;
         Case 
8:  $t = "Ocho";break;
         Case 
9:  $t = "Nueve";break;
         Case 
10: $t = "Diez";break;
         Case 
20: $t = "Veinte";break;
         Case 
30: $t = "Treinta";break;
         Case 
40: $t = "Cuarenta";break;
         Case 
50: $t = "Cincuenta";break;
         Case 
60: $t = "Sesenta";break;
         Case 
70: $t = "Setenta";break;
         Case 
80: $t = "Ochenta";break;
         Case 
90: $t = "Noventa";break;
         Case 
100: $t = "Cien";break;
         Case 
200: $t = "Doscientos";break;
         Case 
300: $t = "Trescientos";break;
         Case 
400: $t = "Cuatrocientos";break;
         Case 
500: $t = "Quinientos";break;
         Case 
600: $t = "Seiscientos";break;
         Case 
700: $t = "Setecientos";break;
         Case 
800: $t = "Ochocientos";break;
         Case 
900: $t = "Novecientos";break;
         Case 
1000: $t = "Mil";break;
         Case 
1000000: $t = "Millón";break;
      }

      If(
$t == $this->Void)
      {
        
$i = $i + 1;
        
$x = $x / 1000;
        If(
$x== 0) $i = 0;
      }
      else
         break;
           
    }while(
$i != 0);
   
    
$Rtn = $t;
    Switch(
$i)
    {
       Case 
0: $t = $this->Void;break;
       Case 
1: $t = " Mil";break;
       Case 
2: $t = " Millones";break;
       Case 
3: $t = " Billones";break;
    }
    return(
$Rtn . $t);
}

}
      
?>



Forma de uso:

La funcion ValorEnLetras nos exige 2 parámetros entre ellos las siglas de la moneda y el valor.

Código (php) [Seleccionar]

$num = 1200;
$valor=new EnLetras();
$literal = $valor->ValorEnLetras($num,"$USD");
echo $literal;


La función ValorEnLetras también puede enviar su segundo parámetro en blanco.

Código (php) [Seleccionar]
$literal = $valor->ValorEnLetras($num,"");

Saludos
Y es que a veces pienso que si no estuviera loco no podría salir adelante.
Lo que no se es capaz de dar, en realidad no se posee, uno es poseído por ello.