Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    How do I automatically generate this table ?

    Hi
    My script generates an array full of results that I want to display
    in a table.

    The table needs to be 112 columns by 112 rows
    And the data is in an array that looks like this:

    Array (
    [0] => Array ( [0] => 0 [1] => 1 [2] => 96 [3] => 1512 [4] => 52 )
    [1] => Array ( [0] => 0 [1] => 2 [2] => 95 [3] => 1512 [4] => 73 )
    [2] => Array ( [0] => 0 [1] => 3 [2] => 95 [3] => 1512 [4] => 72 )
    [3] => Array ( [0] => 0 [1] => 5 [2] => 88 [3] => 1512 [4] => 176 )
    [4] => Array ( [0] => 0 [1] => 6 [2] => 93 [3] => 1512 [4] => 100 )
    [5] => Array ( [0] => 0 [1] => 7 [2] => 83 [3] => 1512 [4] => 247 )
    [6] => Array ( [0] => 0 [1] => 8 [2] => 92 [3] => 1512 [4] => 118 )
    [7] => Array ( [0] => 0 [1] => 9 [2] => 90 [3] => 1512 [4] => 147 )
    [8] => Array ( [0] => 0 [1] => 10 [2] => 90 [3] => 1512 [4] => 145 )
    [9] => Array ( [0] => 0 [1] => 11 [2] => 94 [3] => 1512 [4] => 83 )
    [10] => Array ( [0] => 0 [1] => 12 [2] => 94 [3] => 1512 [4] => 89 )
    [11] => Array ( [0] => 0 [1] => 13 [2] => 96 [3] => 1512 [4] => 50 )
    [12] => Array ( [0] => 0 [1] => 14 [2] => 79 [3] => 1512 [4] => 309 )
    [13] => Array ( [0] => 0 [1] => 15 [2] => 94 [3] => 1512 [4] => 86 )
    [14] => Array ( [0] => 0 [1] => 16 [2] => 95 [3] => 1512 [4] => 71 )
    [15] => Array ( [0] => 0 [1] => 17 [2] => 89 [3] => 1512 [4] => 165 )
    [16] => Array ( [0] => 0 [1] => 18 [2] => 96 [3] => 1512 [4] => 55 )
    [17] => Array ( [0] => 0 [1] => 19 [2] => 93 [3] => 1512 [4] => 100 )
    [18] => Array ( [0] => 0 [1] => 20 [2] => 95 [3] => 1512 [4] => 69 )
    [19] => Array ( [0] => 0 [1] => 21 [2] => 79 [3] => 1512 [4] => 307 )
    [20] => Array ( [0] => 0 [1] => 22 [2] => 81 [3] => 1512 [4] => 285 )

    ...

    [12204] => Array ( [0] => 111 [1] => 105 [2] => 94 [3] => 1515 [4] => 86 )
    [12205] => Array ( [0] => 111 [1] => 106 [2] => 88 [3] => 1515 [4] => 174 )
    [12206] => Array ( [0] => 111 [1] => 107 [2] => 89 [3] => 1515 [4] => 155 )
    [12207] => Array ( [0] => 111 [1] => 108 [2] => 93 [3] => 1515 [4] => 106 )
    [12208] => Array ( [0] => 111 [1] => 109 [2] => 95 [3] => 1515 [4] => 61 )
    [12209] => Array ( [0] => 111 [1] => 110 [2] => 96 [3] => 1515 [4] => 47 ) )

    Element 0 is row
    Element 1 is column
    Element 2 is percentage

    I want to produce a table with the percentage recorded in the cells
    according to the row and column.

    I guess I can do this with for loops in php to build the table ??

    Some help with the code would be appreciated as I am not sure
    how to do this.



    Thanks


    .
    Last edited by jeddi; 10-25-2013 at 10:06 PM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,899
    Thanks
    15
    Thanked 226 Times in 226 Posts
    Your data array contains more information than is really needed, you only need to display the percentage, unless you want to include the column and row number with it. You can use a single for loop with modulus 112 to determine where to insert a </tr><tr> to start a new row.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,961
    Thanks
    2
    Thanked 305 Times in 297 Posts
    You can do this without using any explicit loops (let php array functions do the work) -

    Code:
    // assuming your array is in $data
    
    $data = array_chunk($data,112); // make each row an array
    
    function _extract($a){
    	return $a[2]; // get just the percent value	
    }
    
    // replace each row/array with the corresponding table row
    function _build($a){
    	return '<tr><td>'. implode('</td><td>',array_map('_extract',$a)) ."</td></tr>\n";
    }
    
    $data = array_map('_build',$data);
    
    echo '<table>' . implode('',$data) . '</table>'; // combine all the table rows and output in a table tag
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    jeddi (10-26-2013)

  • #4
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    That's just great - just what I was needing and
    as I haven't used "array_map" before I really would not have
    known how to do that


    If I want to place an if statement in the display of the cell value,
    how would I do that ?

    e.g. if I want to change the color of a cell to red when the percentage
    value falls below 90 % ??

    Thanks



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,961
    Thanks
    2
    Thanked 305 Times in 297 Posts
    You would modify the _extract() function code to test and put a <span></span> tag around the value to style it with a css class that results in the color you want.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #6
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    OK - I have modified that _extract() function
    and I am running it now (takes over 5 minutes )

    I have been trying to see how I can put the values of
    $a[0] and $a[1] in the table to produce correct column headings
    and row titles.

    That way when I see a red number, I can quickly see which pair of
    data elements it refers to.

    These two data elements refer to files that the script compares. So a
    RED result will highlight a problem between 2 files and I need to know which 2 files
    - so if I can get $a[0] and $a[1] in the headings for column and rows, that would identify the problem.

    I have tried a couple of things but I just get errors

    Thanks for helping



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #7
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,961
    Thanks
    2
    Thanked 305 Times in 297 Posts
    You could use a title= ' ... ' attribute in the <span> tag to list the $a[0] and $a[1] values for the cell.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #8
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    EXCELLENT !!

    I modified it so it chunks into 20 columns - enough for my screen

    and I added the span to all cells so I get the file names.

    Great !!!

    Thanks for your help CFMaBiSmAd.




    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •