0

I am developing a simple plugin that allows for an admin to edit a CSV and then a normal user to be able to view the table and generate quotes from the prices stored in the table etc.

Everything works including the two pages (shown by shortcode hooks), the form shows and at the moment we just return a 0 value but obviously when the CSV can be accessed then we can start generating quotes as we can grab the value from the table.

My problem is that when I use the following code to push the CSV into an Array, it doesn't seem to work and I'm not entirely sure why. Apologies for the bad formatting, I'm a messy coder while things aren't working - I promise I'll fix it later!

$csvData = file_get_contents('products.csv');
$lines = explode(PHP_EOL, $csvData);
$array = array();
foreach ($lines as $line) {
        $array[] = str_getcsv($line);
}

If I were to print then I just get 'Array', and if I were to write the code to print the entirety of the Array, I get nothing either.

On a standalone PHP test it works, but in WordPress it doesn't seem to. I've only been doing WP development since yesterday afternoon so this side of things is quite new to me. Once then CSV can be handled as an Array then I can complete the quoting algorithm.

Also, one quick question, is this the best way to handle data? I was toying with the idea of an SQL database but I want the whole plugin to be self contained, so I've opted for a CSV to exist within the plugins folder.

EDIT: when I delete the plugin, the CSV is under the 'files to be deleted' section, so it is being uploaded and does exist alongside the functional PHP file.

2
  • Kind of better. Look for the URL of your CSV file... and HTACCESS too... Commented Nov 16, 2016 at 13:21
  • The CSV is in the same folder as the PHP I referenced in the question and is named 'products.csv' Commented Nov 17, 2016 at 8:45

1 Answer 1

0

Answering my own question just in case someone comes across it looking for an answer. Managed to figure it out!

We have to get the URL as follows, we can't just reference it like we normally would on a server

plugins_url( 'products.csv', __FILE__ );

The above code returns the information we need, rather than just referencing products.csv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.