Forum Moderators: coopster

Message Too Old, No Replies

Slight Modification

Printing

         

inuwolf

6:31 am on Feb 10, 2006 (gmt 0)

10+ Year Member



The script I've been tweaking prints a 7-column CSV file to a 7-column html table. It uses $field to represent the data in each field (see the code in bold). Ironically, I need to desimplify this so the data is entered in each row as $field1, $field2...$field7, rather than just $field (assuming $fieldX belongs to column X). Only this way can I express the data printed to certain columns as links, for instance; otherwise the data is printed directly to the table and I cannot customize it. This really is the LAST step in my table, and if anyone can help me put this to bed I will be eternally thankful. :)

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>";

jatar_k

7:16 am on Feb 10, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could also just use a counter, start it at 0 and do what you need to on the proper iteration. Reset the counter inside the outer foreach and it should work flawlessly.

maybe add a switch on $counter in your inner foreach.

inuwolf

4:25 pm on Feb 10, 2006 (gmt 0)

10+ Year Member



I can do if/else statements and while loops, but because I don't understand the foreach function I'm not sure how to approach it. Your suggestion sounds like what I'm attempting, jatar_k, but I'm ashamed to admit I have trouble with anything other than visual pointers.

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

jatar_k

4:45 pm on Feb 10, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well I sure don't want you going away, so maybe I shouldn't continue ;)

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

inuwolf

5:04 pm on Feb 10, 2006 (gmt 0)

10+ Year Member



Jatar_k, thank you!
Thank you! Thank you!
...
THANK YOU!

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!

:)

jatar_k

10:10 pm on Feb 10, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well if your hunger for CSV files needs some food for thought start here
[webmasterworld.com...]

>> 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 ;)