Jump to content

read a cvs file and only displaying some data


manhattes
Go to solution Solved by benanamen,

Recommended Posts

I am trying to take specific dates out of a csv file.

 

Currently I am able to retrieve and display the data with the following code:

$url = "http://ichart.finance.yahoo.com/table.csv?s=" . $row2['Symbol'];  
echo "<html><body><table border=1>";
$f = fopen($url, "r");
$fr = fread($f, filesize($url));
fclose($f);
$ch = curl_init();
$timeout = 30;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
echo $row2['Symbol'];

$lines = array();
$lines = explode("\n",$data); 

for($i=0;$i<count($lines);$i++)
{
    echo "<tr>";
    $cells = array(); 
    $cells = explode(",",$lines[$i]); 
    for($k=0;$k<count($cells);$k++)
    {
       echo "<td>".$cells[$k]."</td>";
    }
    // for k end
    echo "</tr>";
}
// for i end
echo "</table></body></html>";

Can I set the script to only grab dates that are on the first of the month or do I have to create a new table and store the information?

I guess I dont understand how I would make a variable to be equal to the date column since it is in a csv file.

 

 

Link to comment
Share on other sites

How you do this depends on how you will use it. You are pulling financial data from Yahoo. If you were to have a LOT of requests, Yahoo could block you. How often is the data updated? It may make sense to parse the data and store it for repeated requests for some amount of time then, after that time expires, fetch new data. But, that's really a different questions. Also, do you ONLY need the date with a specific date? If so, add the logic to the code that reads the data.

 

So, to your code. If the resources is really a CSV file, then the current process is flawed. You don't need to split using \n or on the commas. In fact, that can break if there are other line breaks or commas in the data. PHP has functions for reading csv files which you should use: http://php.net/manual/en/function.fgetcsv.php

 

When reading each line, you will want to test the date field to see if it matches the ones you are looking for. Without knowing the format of that field it is impossible to provide any sample code. Run your code above and change the for loop to this and show us the output

 

for($i=0;$i<count($lines);$i++)
{
    echo "<pre>".print_r($lines[0], true)."</pre>";
}
Link to comment
Share on other sites

  • Solution

I will leave it up to you to handle the dates

 

 

<?php
$url  = "http://ichart.finance.yahoo.com/table.csv?s=f";
$file = file($url);
foreach ($file as $value)
    {
    if (stristr($value, '2015-09-10'))
        {
        echo $value;
        }
    }
?>
Link to comment
Share on other sites

That almost worked but I think because of the output, the fact the dates are not in the same format is throwing it off. It is also returning the entire row of data:

Thursday, October 01, 2015

2011-02-11,5.00,5.09,4.51,4.55,1899700,4.55

 

Can $value be broken into an array maybe?

Link to comment
Share on other sites

I fixed this question. The date format function can only be used on a $date variable so I needed to create it first before using date_format.

I know my table isn't uniform but that is also why I use PHP to manipulate the text.

Here is the fixed  code:

$date = date_create($row2['Completion Date']);
$newdate = date_format($date, 'Y-m-d');
//echo $newdate;
foreach ($file as $value)
    {
	
    if (stristr($value, $newdate))
        {
       // echo $value;

Thanks for your help @benanamen.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.