Forum Moderators: coopster
DrDoc and many others kindly helped me alternate row colors in my PHP table printing from a CSV file. Thank you, I really appreciate the help I've gotten here.
$i=-1;
$ff=fopen('DataFile.csv','r') or die("Can't open file");
while(!feof($ff)){
$i++;
$Data[$i]=fgetcsv($ff,1024);
}//Make columns sortable for each sortable column
foreach ($Data as $key => $row) {
$One[$key] = $row[0]; // could be $row['Colname']
$Two[$key] = $row[1]; //numeric example
... //you get the idea
$Seven[$key] = $row[6];} //numeric example$str = "<table border=\"1\" align=\"center\">";
$i = 0;
foreach($Data as $NewDat) {
if($NewDat[0]!= "") { //error trap
$str .= "<tr>";
foreach($NewDat as $field) $str.= "<td><center>$field</center></td>";
$str .= "</tr>\n";
}}
fclose($ff);
$str .= "</table>";
Because my website is nonprof and the php element of the site is negligible (what you've seen is basically all of it!), I've tried avidly to avoid paying a professional. This is the last time I plan to alter this script (a script I really don't understand), and if anyone can give me a little more help in putting this one to bed I will be eternally grateful--and will go away for a while.
Trying not to sound desperate,
Nik
alright, so I mentioned outer and inner loops and we are dealing with 7 elements (0 to 6)
so
1. foreach($Data as $NewDat) {
2. if($NewDat[0]!= "") { //error trap
3. $str .= "<tr>";
4. foreach($NewDat as $field) $str.= "<td><center>$field</center></td>";
5. $str .= "</tr>\n";
6. }}
1. outer foreach loop - this takes each line from your file and puts it into a var called $NewDat
2. this checks to see if the first element in the array is empty, if it is, then it skips that row and continues at line 1
3. this outputs the opening row tag
4. inner loop - this is the one we want to add to
5. outputs the closing row tag and a newline so the source of the webpage is easier to read
6. close our if from line 2 and our loop from line 1
seems easy enough, we need to add a counter to our outer loop before the inner one starts
1. foreach($Data as $NewDat) {
$counter = 0;
2. if($NewDat[0]!= "") { //error trap
3. $str .= "<tr>";
4. foreach($NewDat as $field) $str.= "<td><center>$field</center></td>";
5. $str .= "</tr>\n";
6. }}
done, now we need to use it inside, I will reduce the code here. You can replace line 4 with all of the following code
foreach($NewDat as $field) {
switch ($counter) {
case 0:
$str.= "<td><center>$field</center></td>";
break;
case 1:
$str.= "<td><center>$field</center></td>";
break;
case 2:
$str.= "<td><center>$field</center></td>";
break;
case 3:
$str.= "<td><center>$field</center></td>";
break;
case 4:
$str.= "<td><center>$field</center></td>";
break;
case 5:
$str.= "<td><center>$field</center></td>";
break;
case 6:
$str.= "<td><center>$field</center></td>";
break;
}
$counter++;
} each case represents a different element (column) in your array (row). just change the echo line for each individual column to do what you need.
I didn't test this at all by the way so an error is possible
You have made my day! Not only did you give me what I needed in this chaotic temporal realm, you've given me an understanding of it, which is probably more important. I can finally move past this borrowed script and do my duties as webmaster and maybe even work on some simple scripts of my own. Of course I will not go away; if anything I am now a more dedicated patron of webmasterworld--I might even drop by to ask basic questions about querying CSV databases.
Who knew altruism still exists? And on the net!
:)
>> you've given me an understanding of it
well I am always hoping that's the point of posting, to share knowledge. The whole teaching someone to fish thing ;)