Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Parsing CSV file in PHP
Old 09-11-2009, 02:34 PM Parsing CSV file in PHP
Junior Talker

Posts: 3
Name: Jay
Trades: 0
First off, hello. This is my first thread on this forum and it appears to be a very helpful resource for people ranging from beginner to advanced coders, I look forward to contributing as well as receiving advice from members.

That said, I am wondering how I would go about doing this properly...

Here is my current code...

Code:
<?
$row = 1;
$handle = fopen("http://finance.yahoo.com/d/quotes.csv?s=XTO+CHK+XOM+MSFT&f=snkcp4", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    echo "\n";
    $row++;
    for ($c=0; $c < $num; $c++) {
    echo "<span style=\"";
    if ($data[$c]>0) {
    echo "color: #009900\">";
    }
    elseif ($data[$c]<0) {
    echo "color: #990000\">";
    }
    else {
    echo "font-weight: bold\">";
    }
        echo $data[$c] . "</span>&nbsp - &nbsp\n";
        
    }
    }
fclose($handle);
?>
Currently the code calls for anything below zero to appear in red text to reflect adjustments in stock prices, and anything above zero to appear in green text... ideally, I would only want the [4th] column in every row to be displayed with these attributes.

Example: CSV appears as follows...

MSFT,Microsoft Corp,27.74,-0.01 --0.01%
GOOG,Google,450.00,+1.45 -+0.87

Any help would be appreciated.

Last edited by FRAZL; 09-11-2009 at 02:36 PM..
FRAZL is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-11-2009, 05:33 PM Re: Parsing CSV file in PHP
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Hum...
Add a counter....
PHP Code:
<?
$row 
1;
$handle fopen("http://finance.yahoo.com/d/quotes.csv?s=XTO+CHK+XOM+MSFT&f=snkcp4""r");
while ((
$data fgetcsv($handle1000",")) !== FALSE) {
  
$num count($data);
  echo 
"\n";
  
$row++;
  
$col=0;
  for (
$c=0$c $num$c++) {
    echo 
"<span style=\"";
    if (
$col==&& $data[$c]>0) {
      echo 
"color: #009900\">";
    }
    elseif (
$col==3 $data[$c]<0) {
      echo 
"color: #990000\">";
    }
    else {
      echo 
"font-weight: bold\">";
    }
    echo 
$data[$c] . "</span>&nbsp - &nbsp\n";
    
$col++;    
  }
}
fclose($handle);
?>
As I cannot highlith PHP code, look for rows with $col. there are 4 modifications.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 09-11-2009 at 05:34 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 09-11-2009, 06:41 PM Re: Parsing CSV file in PHP
Junior Talker

Posts: 3
Name: Jay
Trades: 0
tyvm
FRAZL is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Parsing CSV file in PHP
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.67075 seconds with 12 queries