Re: smart table construction from array

From: Date: Tue, 14 Aug 2001 18:36:33 +0000
Subject: Re: smart table construction from array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-62684@lists.php.net to get a copy of this message
This is the way I would do it: split your CSV into an array. Then traverse the array, saving any data and incrementing a counter for any empty element. Finally set up some conditional statements with the counter to output the table the way you want. Still with me? Here is some sample code I threw together. You may have to play with it to get it working the way you want... but it should at least give you a starting point. <HTML><BODY><Table Border=1> <?php $fp = fopen("file.csv",r); while (!feof ($fp)) { $buffer = fgets($fp, 4096); $linearr = explode(",",$buffer); $outarr = ""; echo "<TR>\n"; $colspan=0; for($j=0; $j < count($linearr); $j++) { if ($linearr[$j] == "") { $colspan++; } else { $outarr[] = chop($linearr[$j]); } } if ($colspan == 0) { echo "<TD>$outarr[0]</TD>\n"; } else { if(count($outarr) == 0) echo "<TD Colspan=$colspan></TD>\n"; else echo "<TD Colspan=$colspan>$outarr[0]</TD>\n"; } for($j=1; $j < count($outarr); $j++) { echo "<TD>$outarr[$j]</TD>\n"; } echo "</TR>\n"; } ?> </Table></Body></HTML> Sheridan Saint-Michel Website Administrator FoxJet, an ITW Company www.foxjet.com ----- Original Message ----- From: Peter Torraca <peter@torraca.net> To: <php-general@lists.php.net> Sent: Tuesday, August 14, 2001 11:02 AM Subject: [PHP] smart table construction from array > Hello All -- > > I have a csv file that I need to parse into a table. Getting the > file open and into stuffed into an array is not really a problem, but > I want PHP to adapt the table structure to the data on the fly. For > example, some data (from the hgitner.com stamp buying list): > > 26,used,3¢ Washington Dull Red type II,$1.00 ,-,- > "Fine + OG Hinged 232-40, 285-90, 294-99, 323-7 wanted. Call or ship!",,,,, > 534A,single,"2¢ Washington, type VI (imperf, offset)",$20.00 ,$12.00 ,$6.00 > 534B,single,"2¢ Washington, type Va (imperf, offset)",Needed Urgently!,, > > > I can easily get line 1 and 3 can easily into: > > <td>26</td><td>3¢ Washington Dull Red type > II</td><td>$1.00</td><td>-</td><td>-</td> > > but line 2 and 4 are my problem. I want PHP to take line 2 and see > that there is only one cell of data in the and four blanks in the > array and thus create: > > <td colspan=5>Fine + OG Hinged 232-40, 285-90, 294-99, 323-7 wanted. > Call or ship!</td> > > PHP should see that line 4 ends in two blanks and thus should output: > > <td>34B</td><td>single </td><td>2¢ Washington, type Va (imperf, > offset) </td><td colspan=3>Needed Urgently!" > > but I'm having some real trouble figuring out the logic/control > structures to do this. If there is a class out there that does this, > I'd be happy to hear of it. But I suspect my inexperience is the > real problem: I'm really hoping that someone can explain the logic of > how this might be done. > > thanks in advance. > > peter > > -- > Peter Torraca > peter@torraca.net > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.general (#62684) next »