Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-04-2012, 04:47 PM   PM User | #1
raposu
New to the CF scene

 
Join Date: Oct 2012
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
raposu is an unknown quantity at this point
extract a csv

Hello, I need to extract a data.csv with data generated by this php (something that I can not because I always generates </ br>

If so, do you load the page automatically every so often?

generated data ".... data.php" are these:

Quote:
Categorias,04/11/2012 16:00,04/11/2012 15:00,04/11/2012 14:00,04/11/2012 13:00,04/11/2012 12:00,04/11/2012 11:00,04/11/2012 10:00,04/11/2012 09:00,04/11/2012 08:00,04/11/2012 07:00,04/11/2012 06:00,04/11/2012 05:00,04/11/2012 04:00,04/11/2012 03:00,04/11/2012 02:00,04/11/2012 01:00,04/11/2012 00:00,03/11/2012 23:00,03/11/2012 22:00,03/11/2012 21:00,03/11/2012 20:00,03/11/2012 19:00,03/11/2012 18:00,03/11/2012 17:00
Precipitacion,0.0,0.0,0.0,0.0,0.4,0.0,0.0,0.0,0.0, 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0. 2,0.0,0.2
Temperatura,10.8,10.4,10.0,10.1,9.4,9.1,8.7,8.9,8. 4,8.5,8.3,8.5,8.5,8.9,9.2,9.3,9.3,9.3,10.0,10.1,10 .0,10.0,10.1,10.5
Presion,968.5,968.6,968.3,968.4,968.4,968.2,968.2, 967.5,966.6,966.3,966.0,965.5,965.6,965.9,965.9,96 6.3,966.4,966.5,966.8,967.1,967.1,967.1,966.9,966. 4

Code:
<?php

// primero se abre el documento en modo lectura "r" (read)
$handle = fopen("http://www.aemet.es/es/eltiempo/observacion/ultimosdatos_oviedo_20121008173202.csv?k=ast&l=1249X&datos=det&w=0&f=temperatura&x=", "r");

// creamos una variable que la llamaremos CONTADOR_LINEA para ir contando las líneas del documento

$contador_linea = 1;

// creamos una variable que la llamaremos CONTADOR

$contador = 0;

// se hace un WHILE que irá sacando datos mientras siga habiendo en el documento

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
 
 // el bucle coge una línea con datos y las vamos guardando en diferentes matrices $fecha[1] para el primer dato de fecha, y lo mismo con temperatura, precipitacion y presión.
 
    // cuando llegue al final de while y repita de nuevo el procedimiento, con $fecha[2] se guardará el segundo dato de fecha. Lo mismo con temperatura, precipitacion y presión.
 
 //los datos los irá guardando a partir de la linea deseada. lo especificamos con un IF y la variable CONTADOR_LINEA
 
 if ($contador_linea >= 5){
   $contador++; //sumamos una unidad a CONTADOR y guardamos datos
   $Categorias[$contador] = $data[0];
   $Precipitacion[$contador] = $data[6];
   $Temperatura[$contador] = $data[1];
   $Presion[$contador] = $data[7];
 }
 //sumamos una unidad mas a contador_linea y seguimos con la linea siguiente
 $contador_linea++;
  
}

//cerramos el documento con FCLOSE.

fclose($handle);

// vamos a crear otra variable llamada RECUENTO. Con él vamos a ir pasando desde el principio todas las matrices.

$recuento=1;

// Comenzamos con Fecha. Escribimos primero la categoría con su coma.

echo 'Categorias,';

// Hacemos un bucle WHILE. Mientras la variable recuento sea menor o igual que CONTADOR (el número máximo de datos que tenemos guardados).

while($recuento <= $contador){
 
    //si recuento es igual a contador, significa que es el último, entonces lo escribimos sin la coma.
 
 // sino es igual entonces lo escribimos con coma
 
 if($recuento == $contador){
   echo $Categorias[$recuento];
 }
 else{
   echo $Categorias[$recuento].',';
 }
 //sumamos una unidad más a recuento y continuamos haciendo el while mientras siga siendo igual o menor que contador.
 $recuento++;
}

//escribimos el salto de línea puesto que ya acabamos con la primera categoría.

echo "<br/>";

// volvemos a resetear la variable RECUENTO a 1 y hacemos todo el procedimiento con otra categoría.

$recuento=1;

echo 'Precipitacion,';

while($recuento <= $contador){
 if($recuento == $contador){
   echo $Precipitacion[$recuento];
 }
 else{
   echo $Precipitacion[$recuento].',';
 }
 $recuento++;
}

echo "<br/>";


// volvemos a resetear la variable RECUENTO a 1 y hacemos todo el procedimiento con otra categoría.

$recuento=1;

echo 'Temperatura,';

while($recuento <= $contador){
 if($recuento == $contador){
   echo $Temperatura[$recuento];
 }
 else{
   echo $Temperatura[$recuento].',';
 }
 $recuento++;
}

echo "<br/>";
// volvemos a resetear la variable RECUENTO a 1 y hacemos todo el procedimiento con otra categoría.

$recuento=1;

echo 'Presion,';

while($recuento <= $contador){
 if($recuento == $contador){
   echo $Presion[$recuento];
 }
 else{
   echo $Presion[$recuento].',';
 }
 $recuento++;
}


?>

Thank's
raposu is offline   Reply With Quote
Old 11-05-2012, 01:42 AM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Is this the line causing the problem

Code:
//escribimos el salto de línea puesto que ya acabamos con la primera categoría.

echo "<br/>";
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 11-05-2012, 05:52 AM   PM User | #3
raposu
New to the CF scene

 
Join Date: Oct 2012
Posts: 9
Thanks: 1
Thanked 0 Times in 0 Posts
raposu is an unknown quantity at this point
I think not .....

The data.php extracted is correct, the problem is when I try to return the file to data.csv that always goes with </ br> between each line

I tried in every way and I can not
raposu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:29 AM.


Advertisement
Log in to turn off these ads.