Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>OK, I'm going to start with some concepts I think are quite important to bring up.</p> <p><strong>Firstly</strong>, you need to get your CSV files into a structured data format of some sort (an array of split values will do for CSV). I'm going to use PHP's <code>str_getcsv()</code> for this. This function should be called inside a loop as it takes in a string and splits it up for you into an array.</p> <p><strong>Secondly</strong>, I have a strong belief that you should do all the logic work <strong>before</strong> you go to output your data. This way your output code is kept clean and easy on the eye (yours or somebody elses). So I'm going to do the logic before the output and just use a simple output array.</p> <p>So I start by recreating your CSV files in arrays:</p> <pre><code>$csv_file_1 = array( 'a, 5,', 'b, 10,', 'c, 24,', 'd, 21' ); $csv_file_2 = array( 'not, 1.87,', 'notatall, 1.78' ); </code></pre> <p><strong>In your case</strong>, you want to read CSV files into arrays, and I'd suggest the best way to do it would be to use PHP's <code>file()</code> function. Easy as:</p> <pre><code>// read csv files into arrays $csv_file_1 = file('your_first.csv'); $csv_file_2 = file('your_second.csv'); </code></pre> <p>Next, I want to loop through the first array, grab the two variables out of it, then loop through the second array inside that and do the calculations on the second original variable (we'll call the originals <code>var1</code> and <code>var2</code>, and the second array <code>factor_name</code> and <code>factor</code>):</p> <pre><code>foreach($csv_file_1 as $originals) { // get variables from each line list($var1, $var2) = str_getcsv($originals); // remove whitespace from second variable $var2 = trim($var2); $factors_output = array(); // get multiplier variables foreach($csv_file_2 as $factors) { list($factor_name, $factor) = str_getcsv($factors); // do calculation, add to temporary array $factors_output[] = $var2 * $factor; } // array_merge the original vars with the multiplied vars $output[] = array_merge(array($var1, $var2), $factors_output); } </code></pre> <p>Now, you have an array that looks like this:</p> <pre><code>Array ( [0] =&gt; Array ( [0] =&gt; a [1] =&gt; 5 [2] =&gt; 9.35 [3] =&gt; 8.9 ) [1] =&gt; Array ( [0] =&gt; b [1] =&gt; 10 [2] =&gt; 18.7 [3] =&gt; 17.8 ) [2] =&gt; Array ( [0] =&gt; c [1] =&gt; 24 [2] =&gt; 44.88 [3] =&gt; 42.72 ) [3] =&gt; Array ( [0] =&gt; d [1] =&gt; 21 [2] =&gt; 39.27 [3] =&gt; 37.38 ) ) </code></pre> <p>This is good for you, because you now have structured data with the results you want to output, and you have all the freedom in the world for what to do with that data. I'm going to output it the same way you have in your question. The foreach loop over your array will output the title rows only, then the second time you'll output the data rows.</p> <pre><code>&lt;table border="1"&gt; &lt;tr&gt; &lt;? foreach($output as $line) : ?&gt; &lt;td&gt;&lt;?=$line[0]?&gt;&lt;/td&gt; &lt;? endforeach; ?&gt; &lt;/tr&gt; &lt;? foreach($output as $line) : ?&gt; &lt;tr&gt; &lt;? foreach($line as $var) : ?&gt; &lt;td&gt;&lt;?=$var?&gt;&lt;/td&gt; &lt;? endforeach; ?&gt; &lt;/tr&gt; &lt;? endforeach; ?&gt; &lt;/table&gt; </code></pre> <p>Now, you've asked about efficiency. When dealing with CSV files being parsed by PHP into memory (arrays), there is always the possibility that your CSV could be large, and could chew up your server's memory pretty quickly (possibly causing it to crash, restart etc in extreme cases). You probably won't encounter this in your situation, but I don't know your situation. So it's always a good idea to <code>unset</code> variables when you're finished with them.</p> <pre><code>unset($output, $line, $csv_file1, csv_file2); </code></pre> <p><a href="https://eval.in/77698" rel="nofollow"><strong>Here's a demo of this example.</strong></a></p> <blockquote> <p>Documentation</p> </blockquote> <ul> <li><a href="http://php.net/manual/en/function.file.php" rel="nofollow">http://php.net/manual/en/function.file.php</a></li> <li><a href="http://php.net/manual/en/function.str-getcsv.php" rel="nofollow">http://php.net/manual/en/function.str-getcsv.php</a></li> <li><a href="http://php.net/manual/en/control-structures.foreach.php" rel="nofollow">http://php.net/manual/en/control-structures.foreach.php</a></li> <li><a href="http://php.net/unset" rel="nofollow">http://php.net/unset</a></li> </ul> <blockquote> <p>Further reading</p> </blockquote> <ul> <li><a href="http://en.wikipedia.org/wiki/Separation_of_presentation_and_content" rel="nofollow">http://en.wikipedia.org/wiki/Separation_of_presentation_and_content</a></li> <li><a href="http://wp.tutsplus.com/tutorials/creative-coding/improving-your-work-flow-separate-your-mark-up-from-your-logic/" rel="nofollow">http://wp.tutsplus.com/tutorials/creative-coding/improving-your-work-flow-separate-your-mark-up-from-your-logic/</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload