An array to table problem...

Discussion in 'PHP' started by rbrown, Feb 28, 2007.

  1. #1
    I have a wicked head cold and just can't think this through....

    I have a text file I want to put on a server and it will contain results similar to this (the spaces are in the file, not for clarity):

    1 7541 antonella
    2 4160 space the final frontier
    3 3680 game cheats for ps2

    etc...

    Now the file has 500 lines. So what I need to do for the first 300 is, I want to be able to list them in a four column table like this:

    Colspan 4 for single line header
    Column 1 1-75
    Column 2 76-150
    Column 3 151-225
    Column 3 226-300

    Then the next 200 needs to be in another four column table but like this:

    Colspan 4 for single line header
    Column 1 1-50
    Column 2 51-100
    Column 3 101-150
    Column 3 151-200

    I can open the file, parse the spaces out, change the output order and print all 500 lines with this:

    
    $file_handle = fopen("list.txt", "rb");
    while (!feof($file_handle) ) {
    $line_of_text = fgets($file_handle);
    $line_of_text = preg_replace('/       /', '^', $line_of_text);
    $parts = explode('^', $line_of_text);
    print trim($parts[0]," ") . trim($parts[1]," ") . trim($parts[2]," ")."<br>";
    }
    fclose($file_handle);
    
    PHP:
    That code will output all 500 lines like this:

    1 7541 antonella
    2 4160 space the final frontier
    3 3680 game cheats for ps2


    So I'm trying to get it to make the table. I tried setting a key but I couldn't get it to output. Only the last line of the file would show up.
    Then I tried a bunch of different for and while loops and locked up the dev server on a couple of them. So I'm not even going to list them here.


    So if anyone can help I would appreciate it... I just can't think with all the cold medicine I'm taking.

    Thanks,
    Bob
     
    rbrown, Feb 28, 2007 IP
  2. hemolack

    hemolack Guest

    Messages:
    31
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Is the text file going to grow at all or will it always have 500 lines?
     
    hemolack, Feb 28, 2007 IP
  3. rbrown

    rbrown Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It will always have 500 lines.
     
    rbrown, Feb 28, 2007 IP
  4. rbrown

    rbrown Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Oh and the spaces didn't show up in the example, but there are 7 spaces in between each of the three "sections":
    1 [7 spaces] 7541 [7 spaces] antonella
    2 [7 spaces] 4160 [7 spaces] space the final frontier

    So when it outputs it look like this in each column:
    
    $part_0=trim($parts[0]," ");// section 1
    $part_1=trim($parts[1]," ");");// section 2
    $part_2=trim($parts[2]," ");");// section 3
    $part_3 = preg_replace('/ /', '-', $part_2);");// section 3
    
    echo '<b>'.$part_0.')</b>&nbsp;<a href="http://someurl/files/'.$part_3.'">'.$part_2.'</a>&nbsp;Results:&nbsp;'.$part_1.'<br>';
    PHP:
    Then repeat down the column until the number reaches last column number. then it wil start the next column.
    Hope this makes sense.

    Thanks,
    Bob
     
    rbrown, Feb 28, 2007 IP
  5. hemolack

    hemolack Guest

    Messages:
    31
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes it makes sense. The following code worked for me, but the text file I used had 1 space between each field rather than 7. It should still work, though -- I didn't change any of the pattern matching.

    
    <?PHP
    
    $file_handle = fopen("list.txt", "rb");
    $itr = 0;
    while(!feof($file_handle)) {
    	$line[$itr] = fgets($file_handle);
    	$line[$itr] =  preg_replace('/       /', '^', $line[$itr]);
    	$itr++;
    }
    print "<table><tr><th colspan=\"4\">Table heading</th></tr>\n";
    print "<tr><td>";
    print "<table><tr><td>";
    for($i = 0; $i < 300; $i++) {
    	if($i % 75 == 0 && $i != 0) {
    		print "</table></td><td><table>";
    	}
    	$parts = explode('^', $line[$i]);
    	print "<tr><td>$i -- " . trim($parts[0], " ") . "</td></td>" . trim($parts[1], " ") . "</td><td>" . trim($parts[2], " ") . "</td></tr>";
    }
    print "</td></tr></table>\n";
    print "</td></tr></table>\n";
    
    print "<table>\n";
    print "<tr><td>";
    print "<table><tr><td>";
    for($i = 300; $i < 500; $i++) {
    	if($i % 50 == 0 && $i != 300) {
    		print "</table></td><td><table>";
    	}
    	$parts = explode('^', $line[$i]);
    	print "<tr><td>$i -- " . trim($parts[0], " ") . "</td></td>" . trim($parts[1], " ") . "</td><td>" . trim($parts[2], " ") . "</td></tr>";
    }
    print "</td></tr></table>\n";
    
    fclose($file_handle);
    
    ?>
    
    PHP:
     
    hemolack, Feb 28, 2007 IP
  6. rbrown

    rbrown Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I see where you are going with this... It's not displaying perfect, it looks like it is showing "section 2" in four columns first (not sure if in right order, need to compare) then sections 1 and 3 in the next four with the key values.

    Then it does the same thing for the next 200.

    Give me a few mins to verify exactly what is happening.

    Thanks,
    Bob
     
    rbrown, Feb 28, 2007 IP
  7. rbrown

    rbrown Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Sorry for the delay... I had a server problem I need to take care of...

    You had two closing "td's" together and missing a cell and table close at the end...
    That is why it was displaying weird.
    So it works now...
    But I needed an extra table spacer and table header and didn't need all the cells for the results.

    I have included both versions in case anyone one else would like to use the code.

    Thank you very much....
    Bob

    Your Version:
    ================================
    <?PHP
    $file_handle = fopen("list.txt", "rb");
    $itr = 0;
    while(!feof($file_handle)) {
        $line[$itr] = fgets($file_handle);
        $line[$itr] =  preg_replace('/       /', '^', $line[$itr]);
        $itr++;
    }
    print "<table><tr><th colspan=\"4\">Table heading</th></tr>\n";
    print "<tr><td>";print "<table><tr><td>";
    for($i = 0; $i < 300; $i++) {
        if($i % 75 == 0 && $i != 0) {
            print "</table></td><td><table>";
        }
        $parts = explode('^', $line[$i]);
        print "<tr><td>$i -- " . trim($parts[0], " ") . "</td><td>" . trim($parts[1], " ") . "</td><td>" . trim($parts[2], " ") . "</td></tr>";
    }
    print "</td></tr></table>\n";
    print "</td></tr></table>\n";
    print "<table>\n";print "<tr><td>";
    print "<table><tr><td>";
    for($i = 300; $i < 500; $i++) {
        if($i % 50 == 0 && $i != 300) {
            print "</table></td><td><table>";    
    }
        $parts = explode('^', $line[$i]);
        print "<tr><td>$i -- " . trim($parts[0], " ") . "</td><td>" . trim($parts[1], " ") . "</td><td>" . trim($parts[2], " ") . "</td></tr>";
    }
    print "</td></tr></table></td></tr></table>";
    fclose($file_handle);
    
    ?>
    PHP:
    ================================

    My Version:
    (note: added the "/ n's" to make it easier to see if it was outputting correctly.)
    ================================
    <?PHP
    $file_handle = fopen("list.txt", "rb");
    $itr = 0;
    while(!feof($file_handle)) {
    $line[$itr] = fgets($file_handle);
    $line[$itr] =  preg_replace('/       /', '^', $line[$itr]);
    $itr++;
    }
    print "<table border=\"1\">\n<tr>\n<th colspan=\"4\">First Table heading</th>\n</tr>\n";
    print "<tr>\n<td valign=\"top\">\n";
    print "<table border=\"1\">\n";
    for($i = 0; $i < 300; $i++) 
    {
    if($i % 75 == 0 && $i != 0) {
    print "</tr>\n</table>\n</td>\n<td valign=\"top\"><table border=\"1\">";
    }
    $parts = explode('^', $line[$i]);
    print "<tr><td valign=\"top\"><small><b>".trim($parts[0], " ").")</b>&nbsp;<a href=\"http://someurl.com/".preg_replace('/ /', '-', trim($parts[2], " "))."\">".ucwords(trim($parts[2], " "))."</a>&nbsp;=&nbsp;".trim($parts[1], " ")."</td></tr>";
    }
    print "</td></tr></table>\n";
    print "</td></tr></table>\n";
    print "<table border=\"1\">\n";
    print "<tr>\n<th colspan=\"4\">Spacer Table heading</th>\n</tr>\n<tr>\n<th colspan=\"4\">Second Table heading</th>\n</tr>\n<tr><td valign=\"top\">";
    print "<table border=\"1\">";
    
    for($i = 300; $i < 500; $i++) 
    {
    if($i % 50 == 0 && $i != 300) {
    print "</table></td><td valign=\"top\"><table border=\"1\">";
    }
    $parts = explode('^', $line[$i]);
    print "<tr><td valign=\"top\"><small><b>".trim($parts[0], " ").")</b>&nbsp;<a href=\"http://someurl.com/".preg_replace('/ /', '-', trim($parts[2], " "))."\">".ucwords(trim($parts[2], " "))."</a>&nbsp;=&nbsp;".trim($parts[1], " ")."</td></tr>";
    }
    print "</td></tr></table>\n</td></tr></table>\n";
    fclose($file_handle);
    ?>
    
    PHP:
    ===============================
     
    rbrown, Feb 28, 2007 IP