Jump to content

Problem With A PHP csv script


marianne1616

Recommended Posts

Hi, Everyone!

 

Foremost, let me stress that I'm new to PHP.  In other words, be gentle here because I suspect that the solution to my problem is a simple one.  Also note that I'm not a complete newbie...you can speak geek to me to some degree and I'll 'get it' probably.  That having been said...on to the problem at hand:

 

We are building a website that requires several pages to grab individual .csv files and convert the data to be shown on a php page.  I've found a nice script to handle this for me.  It's clean and looks nice.  But I need it to do two things:

 

1.  Show a website link in a particular column, which is already in the .csv data  (in a column as 'example.com'; the website link is what we're looking for here)

2.  Sort the data by a particular column  (by number of visits)

 

Presently, all of the data appears but we have to sort manually before loading the .csv file to the server and the links do not appear.  The .csv is sent hourly to the server and we'd rather not manually sort it each time, of course.  Look here:

 

www.radiostats.net/losangelesallmedia.php

 

How do I do this?  Here is the script in its present form:

 

<?php

 

// Open the file

$fp = fopen('radiodata/losangelesallmediadata.csv','r') or die("can't open file");

 

// Start a table

$display = '<table id="csv" cellspacing="0" cellpadding="4" > <tr><td><b><u>Rank</b></u></td><td><b><u>Website</b></u></td><td><b><u>Visits</b></u></td><td><b><u>Global Visits</b></u></td><td><b><u>Gender</b></u></td><td><b><u>Age</b></u></td><td><b><u>Income</b></u></td>'."\n";

 

// Loop through each row in the CSV file

while($csv_line = fgetcsv($fp)) {

// Start a row

    $display .= '<tr>';

 

    // Loop through the columns in the CSV file

    foreach ($csv_line AS $col_value) {

        $display .= '<td>'.$col_value.'</td>';

    }

   

    // Close the row

    $display .= "</tr>\n";

}

 

// Close the table

$display .= '</table>';

 

// Close the file

fclose($fp) or die("can't close file");

?>

 

What am I missing here?  I'm clueless.  Please let me know your thoughts.  I'm eager to learn this as this is my first foray into the php world. 

 

Need more details?  Just let me know. 

 

Thanks in advance!

Marianne

 

 

 

 

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.