Hi,

I'm working on a function that will format a text file and output an html table.
I learned how to remove white spaces and unwanted lines. I also found how to remove elements in an array.
I come out with something like this:

periods:123456789
points:2063745839121249555641965

I would like to create a table and separate the characters in cells:

<tr><td>periods:</td><td>1</td><td>2</td> etc.</tr>
<tr><td>points:</td><td>206</td><td>374</td> etc. </tr>

How I do that? 😕

    I'm not quite sure I understand, but if periods and points are arrays, why don't you just do this:

    echo "<tr><td>Periods</td>";
    
    foreach ($periods as $element)
       {
       echo "<td>$element</td>";
       }
    
    echo "</tr>";
    

    You can use similar logic for points.

      It's generally easier to work from an array of items than from a long string. If you must do it this way, here is a snippet of code to accomplish exactly what you describe:

      	$periods = "123456789";
      	$points = "2063745839121249555641965";
      
      echo "<tr><td>periods:</td>";
      $l = strlen($periods);
      for($j=0; $j < $l; $j++) {
      	echo "<td>{$periods[$j]}</td>";
      }
      echo "</tr>\n";
      
      echo "<tr><td>points:</td>";
      $l = strlen($points);
      for($j=0; $j < $l; $j+=3) {
      	$p = substr($points, $j, 3);
      	echo "<td>$p</td>";
      }
      echo "</tr>\n";
      

        Maybe this will explain better what I'm looking for:

        <?php
        
        $fp = @fopen("score.data", "r") or die("Couldn't open file");
        $data = fread($fp, filesize($fp));
        
        
        while(!feof($fp))
        {
        $data .= fgets($fp, 1024);
        }
        
        fclose($fp);
        
        $values = explode("\r\n", $data);
        //remove unwanted lines
        $data = trim($values[4]);
        $data = trim($values[9]);
        $data = trim($values[10]);
        $data = trim($values[11]);
        $data = trim($values[12]);
        $data = trim($values[13]);
        //remove sections on some strings
        $values[26] = substr_replace($values[26], '', 0, 9) . "\n"; 
        $values[26] = substr_replace($values[26], '', 28, 1) . "\n";
        $values[5] = substr_replace($values[5], '', -4, -1) . "\n"; 
        $values[5] = substr_replace($values[5], '', -3, -1) . "\n"; 
        $values[8] = substr_replace($values[8], '', 0, 8) . "\n"; 
        $values[7] = substr_replace($values[7], '', 0, 8) . "\n"; 
        
        //output text
        
        echo " " . $values[0] . " " . $values[1] . "<br>";
        echo " " . $values[2] . "<br>";
        echo " " . $values[5] . " " . $values[26] . "<br>";
        echo " " . $values[7] . "<br>";
        echo " " . $values[8] . "<br>";
        echo " " . $values[15] . " " . $values[36] . "<br>";
        
        
        
        ?> 

        This is what I get:

        Score card
        9/5/2004
        Hole 1 2 3 4 5 6 7 8 9 Ou 10 11 12 13 14 15 16 17 18 I Tot
        Alan Bck P CL -7 3 2 4 4 6 3 3 4 5 34 65 0 4
        SnapperPro Bck P CL -6 3 3 5 4 4 4 3 4 4 34 66 0 12
        Back 386 157 428 517 568 417 117 429 592 3611 Back 351 451 461 567 435 210 453 591 180 3699 7310

          instead of posting your source, post the data file and what you expect to get out of it.

            Originally posted by majik-sheff
            instead of posting your source, post the data file and what you expect to get out of it.

            Ok, here it goes:

            Original file:

            Score
            2004-09-19
            
            
            Hole				1	2	3	4	5	6	7	8	9	Out				
            Par				4	3	4	4	5	4	3	4	5	36	Tot	Mul	Gim	
            H- 0001_Manny	Bck	P	CL	-11	3	2	4	4	4	3	2	3	5	30	61	0	6	
            H- 0067 Jörgen	Bck	P	CL	-9	3	3	4	4	4	3	2	3	5	31	63	0	5	
            
            
            
            
            
            
            Back				386	157	428	517	568	417	117	429	592	3611				
            Middle				386	157	428	517	568	417	98	429	592	3592				
            Forward				338	130	396	483	539	386	98	387	552	3309				
            Junior				386	157	428	517	568	417	98	429	592	3592				
            Ladies				267	104	333	399	488	353	76	296	501	2817				
            Handicap				18	14	10	1	5	8	16	2	11					
            Conditions - Wind: Breezy  Greens: Moderate  Green Speed: Slow  																		
            
            
            
            
            Hole				10	11	12	13	14	15	16	17	18	In	Tot			
            Par				4	4	4	5	4	3	4	5	3	36	72	Mul	Gim	
            H- 0001_Manny	Bck	P	CL	-11	3	4	4	4	3	2	5	4	2	31	61	0	6	
            H- 0067 Jörgen	Bck	P	CL	-9	4	4	4	5	4	2	4	3	2	32	63	0	5	
            
            
            
            
            
            
            Back				351	451	461	567	435	210	453	591	180	3699	7310			
            Middle				351	451	461	567	435	210	453	591	180	3699	7291			
            Forward				329	416	418	531	391	178	417	551	141	3372	6681			
            Junior				351	451	461	567	435	210	453	591	180	3699	7291			
            Ladies				291	367	333	439	318	151	370	426	108	2803	5620			
            Handicap				17	9	7	13	15	3	6	4	12					
            Conditions - Wind: Breezy  Greens: Moderate  Green Speed: Slow  																		
            
            Custom Pin Positions 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11

            This is what I want:

              I messed around with this for a few minutes but I don't think you'll ever have a script that will do this and be solid. I suggest you move the data into a database, this will give you so much more controll over the output, especially if these statistics are going to be changing.

              At any rate, here's some code that I threw together which comes close to what you want. I don't know where to pull the scores or status from, so this table only produces Hole, Par, and Handicap:

              $file = "doc.txt";
              
              $contents = file($file);
              
              echo "<table border=\"1\">";
              foreach ($contents as $line)
                 {
                 $words = preg_split("/\t|,| /", $line);
              
                 // Notice how fragile this is.  If the second element of $words ceases to be either "Hole", "Par", or "Handicap" this script will break.
                 if (($words[1] == "Hole") || ($words[1] == "Par") || ($words[1] == "Handicap"))
                    {
                    echo "<tr>";
                    foreach ($words as $word)
                       {
                       if (($word != "") && ($word != "Mul") && ($word != "Gim"))
                          {
                          echo "<td>$word</td>";
                          }
                       }
                    echo "</tr>";
                    }
                 }
              echo "</table>";
              

                Originally posted by remnant
                I messed around with this for a few minutes but I don't think you'll ever have a script that will do this and be solid. I suggest you move the data into a database, this will give you so much more controll over the output, especially if these statistics are going to be changing.

                At any rate, here's some code that I threw together which comes close to what you want. I don't know where to pull the scores or status from, so this table only produces Hole, Par, and Handicap:

                $file = "doc.txt";
                
                $contents = file($file);
                
                echo "<table border=\"1\">";
                foreach ($contents as $line)
                   {
                   $words = preg_split("/\t|,| /", $line);
                
                   // Notice how fragile this is.  If the second element of $words ceases to be either "Hole", "Par", or "Handicap" this script will break.
                   if (($words[1] == "Hole") || ($words[1] == "Par") || ($words[1] == "Handicap"))
                      {
                      echo "<tr>";
                      foreach ($words as $word)
                         {
                         if (($word != "") && ($word != "Mul") && ($word != "Gim"))
                            {
                            echo "<td>$word</td>";
                            }
                         }
                      echo "</tr>";
                      }
                   }
                echo "</table>";
                

                [/B]

                Thanks remnant. I do have an error with your script: "Undefined offset: 1 on line 23" The table shows at the bottom of the screen though. Sorry for my ignorance.

                I would find this much easier to do if I could convert the text in cvs format.

                  Thanks remnant. I do have an error with your script: "Undefined offset: 1 on line 23" The table shows at the bottom of the screen though. Sorry for my ignorance.

                  I would find this much easier to do if I could convert the text in cvs format.

                  Hmm, I don't know about that error. If you posted lines 21-25 I might be able to help you with that, but I'm not sure.

                  Did you mean CSV format? If you open the file in Excel, you can save it as a CSV but I'm not too sure this will help much.

                    Write a Reply...