Foros del Web » Programando para Internet » PHP »

imprimir numero como string con php

Estas en el tema de imprimir numero como string con php en el foro de PHP en Foros del Web. imprimir numero como string con php Hola foreros, tengo una duda, estoy exportando los datos de una tabla de la BD a excel, pero cuando ...
  #1 (permalink)  
Antiguo 15/05/2010, 09:26
 
Fecha de Ingreso: diciembre-2008
Ubicación: http://www.solucionesrios.tk/
Mensajes: 413
Antigüedad: 15 años, 4 meses
Puntos: 19
Busqueda imprimir numero como string con php

imprimir numero como string con php

Hola foreros, tengo una duda, estoy exportando los datos de una tabla de la BD a excel, pero cuando exporto los números estos salen sin el primer 0, y necesito que salgan con el 0, les pego el codigo de la exportación para ver que me pueden ayudar, y gracias por adelantado.

Código PHP:

<?php require_once('../Connections/ternerita.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  if (
PHP_VERSION 6) {
    
$theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  
$theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}
}

$currentPage $_SERVER["PHP_SELF"];



if (!empty(
$_SERVER['QUERY_STRING'])) {
  
$params explode("&"$_SERVER['QUERY_STRING']);
  
$newParams = array();
  foreach (
$params as $param) {
    if (
stristr($param"pageNum_Recordset3") == false && 
        
stristr($param"totalRows_Recordset3") == false) {
      
array_push($newParams$param);
    }
  }
  if (
count($newParams) != 0) {
    
$queryString_Recordset3 "&" htmlentities(implode("&"$newParams));
  }
}

            
$prefijo_sql " SELECT * ";
            require_once(
'../Connections/ternerita.php'); 

            
$condiciones = array();
              if (!empty(
$_POST['nombre'])) {
                 
$condiciones[] = "nombre";
              }
              if (!empty(
$_POST['celular'])) {
                 
$condiciones[] = "celular";
              }
            if (!empty(
$_POST['PIN'])) {
                 
$condiciones[] = "PIN";
              }
            if (!empty(
$_POST['telefono'])) {
                 
$condiciones[] = "telefono";
              }
            if (!empty(
$_POST['direccion'])) {
                 
$condiciones[] = "direccion";
              }
            if (!empty(
$_POST['correo'])) {
                 
$condiciones[] = "correo";
              }
            if (!empty(
$_POST['sexo'])) {
                 
$condiciones[] = "sexo";
              }
            if (!empty(
$_POST['profesion'])) {
                 
$condiciones[] = "profesion";
              }
              if (
count($condiciones) > 0) {
                
$prefijo_sql " SELECT " implode","$condiciones );
              } else {
                  
$prefijo_sql " SELECT * ";
              }     
            
mysql_select_db($database_ternerita$ternerita);
$query_Recordset1 $prefijo_sql." FROM suscriptor ORDER BY id_ususario ASC ";
$Recordset1 mysql_query($query_Recordset1$ternerita) or die(mysql_error());
$row_Recordset1 mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 mysql_num_rows($Recordset1);

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=reporte.xls");

?>
<table width="200" border="1">
  <tr>
    <?php if (!empty($_POST['nombre'])) { ?><td align="center"><strong>NOMBRE</strong></td> 
    <?php }?>
    <?php if (!empty($_POST['celular'])) {?> <td align="center"><strong>CELULAR</strong></td> 
    <?php }?>
    <?php if (!empty($_POST['PIN'])) {?> <td align="center"><strong>PIN</strong></td> 
    <?php }?>
    <?php if (!empty($_POST['telefono'])) {?> <td align="center"><strong>TELEFONO</strong></td> 
    <?php }?>
    <?php if (!empty($_POST['direccion'])) {?> <td align="center"><strong>DIRECCION</strong></td> 
    <?php }?>
    <?php if (!empty($_POST['correo'])) {?> <td align="center"><strong>CORREO</strong></td> 
    <?php }?>
    <?php if (!empty($_POST['sexo'])) {?> <td align="center"><strong>SEXO</strong></td> 
    <?php }?>
    <?php if (!empty($_POST['profesion'])) {?> <td align="center"><strong>PROFESION</strong></td> 
    <?php }?>
  </tr>
  <?php do {?>
  <tr>
   <?php if (!empty($_POST['nombre'])) { ?>   <td><?php echo $row_Recordset1['nombre'];?></td><?php }?>
   <?php if (!empty($_POST['celular'])) {?>   <td><?php print($row_Recordset1['celular']);?></td><?php }?>
   <?php if (!empty($_POST['PIN'])) {?>  <td><?php echo $row_Recordset1['PIN']; ?></td><?php }?>
   <?php if (!empty($_POST['telefono'])) {?>  <td><?php echo $row_Recordset1['telefono'];?></td><?php }?>
   <?php if (!empty($_POST['direccion'])) {?>  <td><?php echo $row_Recordset1['direccion'];?></td><?php }?>
   <?php if (!empty($_POST['correo'])) {?>   <td><?php echo $row_Recordset1['correo'];?></td><?php }?>
    <?php if (!empty($_POST['sexo'])) {?>   <td><?php echo $row_Recordset1['sexo'];?></td><?php }?>
    <?php if (!empty($_POST['profesion'])) {?> <td><?php echo $row_Recordset1['profesion'];?></td><?php }?>    
  </tr>
  <?php } while ($row_Recordset1 mysql_fetch_assoc($Recordset1));?>
</table>
<?php mysql_free_result($Recordset1);?>
__________________
http://www.solucionesrios.tk/

Visita mi Web!
  #2 (permalink)  
Antiguo 07/06/2010, 07:19
 
Fecha de Ingreso: diciembre-2008
Ubicación: http://www.solucionesrios.tk/
Mensajes: 413
Antigüedad: 15 años, 4 meses
Puntos: 19
Respuesta: imprimir numero como string con php

Ya lo resolvi yo mismo usando una clase PHPExcel y asignandole ciertos parametros al formato aqui el codigo para quien lo necesite:

Código PHP:

<?php 

require_once('../Connections/ternerita.php');

if (!
function_exists("GetSQLValueString")) {
function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  if (
PHP_VERSION 6) {
    
$theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  
$theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}
}

$currentPage $_SERVER["PHP_SELF"];



if (!empty(
$_SERVER['QUERY_STRING'])) {
  
$params explode("&"$_SERVER['QUERY_STRING']);
  
$newParams = array();
  foreach (
$params as $param) {
    if (
stristr($param"pageNum_Recordset3") == false && 
        
stristr($param"totalRows_Recordset3") == false) {
      
array_push($newParams$param);
    }
  }
  if (
count($newParams) != 0) {
    
$queryString_Recordset3 "&" htmlentities(implode("&"$newParams));
  }
}

            
$prefijo_sql " SELECT * ";
            require_once(
'../Connections/ternerita.php'); 

            
$condiciones = array();
              if (!empty(
$_POST['nombre'])) {
                 
$condiciones[] = "nombre";
              }
              if (!empty(
$_POST['celular'])) {
                 
$condiciones[] = "celular";
              }
            if (!empty(
$_POST['PIN'])) {
                 
$condiciones[] = "PIN";
              }
            if (!empty(
$_POST['telefono'])) {
                 
$condiciones[] = "telefono";
              }
            if (!empty(
$_POST['direccion'])) {
                 
$condiciones[] = "direccion";
              }
            if (!empty(
$_POST['correo'])) {
                 
$condiciones[] = "correo";
              }
            if (!empty(
$_POST['sexo'])) {
                 
$condiciones[] = "sexo";
              }
            if (!empty(
$_POST['profesion'])) {
                 
$condiciones[] = "profesion";
              }
              if (
count($condiciones) > 0) {
                
$prefijo_sql " SELECT " implode","$condiciones );
              } else {
                  
$prefijo_sql " SELECT * ";
              }     
            
mysql_select_db($database_ternerita$ternerita);
$query_Recordset1 $prefijo_sql." FROM suscriptor ORDER BY id_ususario ASC ";
$Recordset1 mysql_query($query_Recordset1$ternerita) or die(mysql_error());
$row_Recordset1 mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 mysql_num_rows($Recordset1);

$filename 'reporte_excel.xls';

header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream');
header('Content-Type: application/download');
header("Content-Disposition: attachment;filename={$filename}");
header('Content-Transfer-Encoding: binary');

error_reporting(E_ALL);

set_include_path(get_include_path() . PATH_SEPARATOR '../Clases/Classes/');

include 
'PHPExcel.php';

include 
'PHPExcel/IOFactory.php';

$objPHPExcel = new PHPExcel();

$objPHPExcel->getProperties()->setCreator("Mi Ternerita");
$objPHPExcel->getProperties()->setLastModifiedBy("Mi Ternerita");
$objPHPExcel->getProperties()->setTitle("Mi Ternerita");
$objPHPExcel->getProperties()->setSubject("Listado de Suscriptores");
$objPHPExcel->getProperties()->setDescription("Listado de Suscriptores");
$objPHPExcel->getProperties()->setKeywords("office 2007 suscriptores listado openxml php");
$objPHPExcel->getProperties()->setCategory("file office 2007 suscriptores listado");

$objPHPExcel->setActiveSheetIndex(0);
if (!empty(
$_POST['nombre'])) { $objPHPExcel->getActiveSheet()->setCellValue('A1','NOMBRE'); }
if (!empty(
$_POST['celular'])) { $objPHPExcel->getActiveSheet()->setCellValue('B1','CELULAR'); }
if (!empty(
$_POST['PIN'])) { $objPHPExcel->getActiveSheet()->setCellValue('C1','PIN'); }
if (!empty(
$_POST['telefono'])) { $objPHPExcel->getActiveSheet()->setCellValue('D1','TELEFONO'); }
if (!empty(
$_POST['direccion'])) { $objPHPExcel->getActiveSheet()->setCellValue('E1','DIRECCION'); }
if (!empty(
$_POST['correo'])) { $objPHPExcel->getActiveSheet()->setCellValue('F1','CORREO'); }
if (!empty(
$_POST['sexo'])) { $objPHPExcel->getActiveSheet()->setCellValue('G1','SEXO'); }
if (!empty(
$_POST['profesion'])) { $objPHPExcel->getActiveSheet()->setCellValue('H1','PROFESION'); }
$xi 2;
do {
    
    if (!empty(
$_POST['nombre'])) { $objPHPExcel->getActiveSheet()->setCellValue('A'.$xi$row_Recordset1['nombre']); }
    if (!empty(
$_POST['celular'])) { 
        
$objPHPExcel->getActiveSheet()->getCell('B'.$xi)->setValue($row_Recordset1['celular']);
        
$objPHPExcel->getActiveSheet()->getStyle('B'.$xi)->getNumberFormat()->setFormatCode('00000000000');
    }
    if (!empty(
$_POST['PIN'])) { $objPHPExcel->getActiveSheet()->setCellValue('C'.$xi$row_Recordset1['PIN']); }
    if (!empty(
$_POST['telefono'])) { $objPHPExcel->getActiveSheet()->setCellValue('D'.$xi$row_Recordset1['telefono']); }
    if (!empty(
$_POST['direccion'])) { $objPHPExcel->getActiveSheet()->setCellValue('E'.$xi$row_Recordset1['direccion']); }
    if (!empty(
$_POST['correo'])) { $objPHPExcel->getActiveSheet()->setCellValue('F'.$xi$row_Recordset1['correo']); }
    if (!empty(
$_POST['sexo'])) { $objPHPExcel->getActiveSheet()->setCellValue('G'.$xi$row_Recordset1['sexo']); }
    if (!empty(
$_POST['profesion'])) { $objPHPExcel->getActiveSheet()->setCellValue('H'.$xi$row_Recordset1['profesion']); }
    
$xi++;
    
} while (
$row_Recordset1 mysql_fetch_assoc($Recordset1));

$objPHPExcel->setActiveSheetIndex(0);
$objWriter PHPExcel_IOFactory::createWriter($objPHPExcel'Excel5');
$objWriter->save('php://output');

mysql_free_result($Recordset1);

?>
__________________
http://www.solucionesrios.tk/

Visita mi Web!

Etiquetas: numero, string
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 18:57.