Jump to content

Import csv from ftp and display user meta


danser81

Recommended Posts

So I admittedly am very elementary with php but my client originally communicated that they had a csv with information about each user that they upload to their ftp each night and needed the information displayed on each user’s page on their Wordpress site. Which I was able to write the php script for, awesome. Turns out they upload one csv PER USER that the title of the csv matches with one of the user meta fields instead so now I’m stuck. Any advice on how I might go about this? 

Link to comment
Share on other sites

So clearly I have no idea what I am doing here, any insight on how totally far off I am? 

<?php

foreach (glob("*.csv") as $filename) {
    if ( $filename == $user['wpc_cf_client_id'] )
 echo '<div id="open-support">';


$handle = fopen($filename, "r");
echo '<table>';

if ($header) {
    $csvcontents = fgetcsv($handle);
    echo '<tr>';
    foreach ($csvcontents as $headercolumn) {
        echo "<th>$headercolumn</th>";
    }
    echo '</tr>';
}
// displaying contents
while ($csvcontents = fgetcsv($handle)) {
    echo '<tr>';
    foreach ($csvcontents as $column) {
        echo "<td>$column</td>";
    }
    echo '</tr>';
}
echo '</table>';
fclose($handle);
}

echo '</div>';

?>

 

Link to comment
Share on other sites

As I do not have your folder structure or files (and therefore cannot run the code), and I cannot see your screen, you need to give a little more information about what is happening when you run it.

  • In what way is it failing?
  • What happens that shouldn't?
  • What shouldn't happen that does?
Link to comment
Share on other sites

It's coming up blank, as though it isn't working at all or isnt finding a match. But I get no errors or anything on the screen. The wp_cf_client_id is the custom field id that should be matching the name of each csv on the server to display the contents of the csv on the page by user. I was hoping I could figure this out myself but looks like I may have to outsource it :/

Link to comment
Share on other sites

Are you sure your file (presumably index.php) is in the same directory as your csv file?  Try running the following.  Also, make sure errors are enabled and instead of echoing to the screen, using syslog() is also helpful.


 

<?php

function test($v){
    echo '<pre>', print_r($v, 1), '</pre>';
}
test(glob("*.csv"));
test(__DIR__);
foreach (glob("*.csv") as $filename) {
    if ( $filename == $user['wpc_cf_client_id'] )
        test($filename);
    echo '<div id="open-support">';


    $handle = fopen($filename, "r");
    echo '<table>';

    if ($header) {
        $csvcontents = fgetcsv($handle);
        test($csvcontents);
        echo '<tr>';
        foreach ($csvcontents as $headercolumn) {
            echo "<th>$headercolumn</th>";
        }
        echo '</tr>';
    }
    // displaying contents
    while ($csvcontents = fgetcsv($handle)) {
        test($csvcontents);
        echo '<tr>';
        foreach ($csvcontents as $column) {
            echo "<td>$column</td>";
        }
        echo '</tr>';
    }
    echo '</table>';
    fclose($handle);
}

 

Link to comment
Share on other sites

5 minutes ago, Barand said:

Have you tried any debugging? for instance


$files = glob("*.csv") ;
echo '<pre>', print_r($files, 1), '</pre>';;

 

Well that helped me figure part of the issue out, the csv was name .CSV (of course) so I went in and changed the case and it displays. Except it is displaying the same result for each user so now back to the drawing board for that. Thank you for your help :)

Link to comment
Share on other sites

What you should really be doing is addressing the process that is creating the CSV files rather then what to do afterward. I can almost guarantee you there is a bigger problem and better solutions in the initial process.

Tell us about what is going on in the beginning. Also describe what the REAL problem to be solved is, not the attempt to solve it. This smells of an XY Problem.

Link to comment
Share on other sites

15 minutes ago, benanamen said:

What you should really be doing is addressing the process that is creating the CSV files rather then what to do afterward. I can almost guarantee you there is a bigger problem and better solutions in the initial process.

Tell us about what is going on in the beginning. Also describe what the REAL problem to be solved is, not the attempt to solve it. This smells of an XY Problem.

Unfortunately I don't have control over the initial process and I completely agree. I'm just trying to come up with a solution given what I have to work with. I have many unanswered process questions for this client that aren't worth my time or resources. 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.