Maybe we should go right to my ultimate goal of getting the file output into an array that JavaScript CAN operate on.
I was thinking of mentioning that. However, and we can try/do that, in the long run you will be better off seeing how this stuff evolves yourself, so you can trouble shoot it if and when the need arises, or at least get a fuller sense of it. We can accomplish both things. To that end, first let's make doubly sure you get the CTRL-U idea. Here's my current test page's raw code (php_help.php):
PHP Code:
<?php
echo '
-- some javascript code --
</script>
<script>
';
$file = fopen("Awards.csv","r");
$x = fgetcsv($file, 1000, ',');
print_r ($x);
fclose($file);
echo '
</script>
-- some more javascript code
-- some html code --
';
?>
There's no way you can ever see that unless you invent it (which you mostly or entirely did in this case), or someone else shows you what their server side code is/gives you access to their server so you can download/view the raw file. This is what it looks like if I navigate to it:
-- some javascript code -- -- some more javascript code -- some html code --
Now I will show you what it looks like (here) in view source (ctrl-u) mode:
-- some javascript code --
</script>
<script>
Array
(
[0] => Date
[1] => Time
[2] => Time Zone
[3] => Name
[4] => Type
[5] => Status
[6] => Gross
[7] => Fee
[8] => Net
[9] => Note
[10] => From Email Address
[11] => To Email Address
[12] => Transaction ID
[13] => Item Title
[14] => Item ID
[15] => Option 1 Name
[16] => Option 1 Value
[17] => Option 2 Name
[18] => Option 2 Value
[19] => Reference Txn ID
[20] => Custom Number
[21] => Receipt ID
[22] => Balance
[23] =>
)
</script>
-- some more javascript code
-- some html code --
Now I'm going to put it somewhere you can look at it:
http://john.dynamicdrive.com/demos/t...p/php_help.php
This, by the way is the Awards.csv file I'm using (just one line - doesn't have to be, but for demo purposes is sufficient, that said, it does nothing more than give us something to grab for this exercise. A fuller file would present more possibilities):
Code:
Date, Time, Time Zone, Name, Type, Status, Gross, Fee, Net, Note, From Email Address, To Email Address, Transaction ID, Item Title, Item ID, Option 1 Name, Option 1 Value, Option 2 Name, Option 2 Value, Reference Txn ID, Custom Number, Receipt ID, Balance,
Anyways, navigate to that link and view my php_help.php file both normally in the browser, and using the browser's "view source" via CTRL-U, and confirm that both views are as I have quoted above. If not, report what you do see. I'm fairly confident though that you will see what I've presented in my quotes for each view - but again, if not - stop and tell me as best you can what the problem is.
OK, now assuming that all is working and you can also do something like that on your own on your server, or in your sandbox environment (like WAMP or XAMP), consider this PHP file:
PHP Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
table, td {border: 1px solid gray;}
.td2, .td9, .td11, .td12, .td18, .td15, .td16 {display: none;}
</style>
</head>
<body>
<table>
<tbody>
<?php
if (($handle = fopen("Awards.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<tr>";
for ($c=0; $c < $num; $c++) {
echo "<td class='td$c'>$data[$c]</td>";
}
echo "</tr>\n";
}
fclose($handle);
}
?>
</tbody>
</table>
</body>
</html>
As you probably know or could discover, this will create a table out of the data in the csv file with the first row being the headings and each subsequent row (if any) showing an entry with all available data (as exists) for each column in the headings. It also gives each td a class (.td# or .td##, etc.) that can potentially be used to style the columns or to hide them (display none). Here's that file:
http://john.dynamicdrive.com/demos/t...test_2help.php
It would have a lot more info if the csv file had more than one line. Yet, even with just the one line, we see how we can control, through a combination of PHP and css which columns are seen. That might be enough or primarily enough for your purposes, you can always act upon the resulting table with javascript in order to refine it further.
Now if still required, we get to the slightly trickier part of how to get the info into a javascript variable. I cannot guarantee this will work in all cases, but it's viable and can be trouble shot if need be. One way to produce a javascript variable (a multidimensional array) from a csv file:
PHP Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<script>
var myAwards = [];
<?php
$row = 0;
if (($handle = fopen("Awards.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "myAwards[$row] = [];";
for ($c=0; $c < $num; $c++) {
echo "myAwards[$row][$c] = '$data[$c]';\n";
}
$row++;
}
fclose($handle);
}
?>
alert(myAwards);
</script>
</body>
</html>
The alert (near the end) is just a primitive way of demonstrating that the array has been constructed and need not be used in the finished code:
http://john.dynamicdrive.com/demos/t...test_3help.php
Also, after dismissing the alert, use CTRL-U to view the javascript code that was produced by this page.
And, as noted before, this is just a one line csv file. With more lines, a more complex array will be created. I'm sure there are other ways. This was the easiest that presented itself to me at this point.
Just to underscore the use of of CTRL-U, I had to resort both to it and the browsers's error console in order to get the code correct for just this simple demo. So obviously, these are talents that will likely come in handy for fine tuning whatever you have specifically in mind.
I know this is a lot to take in. Feel free to ask questions. Just do me the favor of starting from the earliest point where I lost you and leaving anything that comes after that for later.
Bookmarks