danser81 Posted June 17, 2018 Share Posted June 17, 2018 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted June 17, 2018 Share Posted June 17, 2018 You could use something like the glob() function to loop through the directory and process individual csv files Quote Link to comment Share on other sites More sharing options...
danser81 Posted June 18, 2018 Author Share Posted June 18, 2018 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>'; ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted June 18, 2018 Share Posted June 18, 2018 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted June 18, 2018 Share Posted June 18, 2018 Have you tried any debugging? for instance $files = glob("*.csv") ; echo '<pre>', print_r($files, 1), '</pre>';; Quote Link to comment Share on other sites More sharing options...
danser81 Posted June 18, 2018 Author Share Posted June 18, 2018 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 :/ Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted June 18, 2018 Share Posted June 18, 2018 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); } 1 Quote Link to comment Share on other sites More sharing options...
danser81 Posted June 18, 2018 Author Share Posted June 18, 2018 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 :) Quote Link to comment Share on other sites More sharing options...
benanamen Posted June 18, 2018 Share Posted June 18, 2018 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. Quote Link to comment Share on other sites More sharing options...
danser81 Posted June 18, 2018 Author Share Posted June 18, 2018 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. Quote Link to comment Share on other sites More sharing options...
danser81 Posted June 18, 2018 Author Share Posted June 18, 2018 Thank you to everyone for your help, with that and a little trial and error I was finally able to develop a solution that works properly! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.