scod Posted June 8, 2008 Share Posted June 8, 2008 hello all! well, i want to print in a text file, a table like the ones that appears in mysql's console, something like this,.. +------+----------+------+ | id | order_no | name | +------+----------+------+ | 1 | 20 | bxxx | | 2 | NULL | bbbb | | 3 | NULL | cccc | +------+----------+------+ yep, I can do a tab delimited adding a '\t' after each record, but if the record its too long, the structure of the table just gets ugly anyone knows a class, function or way to do it without losing the presentation of the table? thank's Quote Link to comment Share on other sites More sharing options...
keeB Posted June 8, 2008 Share Posted June 8, 2008 It will take some programming. Actually sounds like a fun project, but what you need to do is the following: Get the largest string for each column and ensure you extend the column (using +---+ ) to that size. Optionally, you could set a total length for the entire table (90 chars for standard terminal please ) and /or set a cutoff length to add a newline. Quote Link to comment Share on other sites More sharing options...
scod Posted June 8, 2008 Author Share Posted June 8, 2008 It will take some programming. Actually sounds like a fun project, but what you need to do is the following: Get the largest string for each column and ensure you extend the column (using +---+ ) to that size. Optionally, you could set a total length for the entire table (90 chars for standard terminal please ) and /or set a cutoff length to add a newline. yep, actually I'm working in the function, but I'm takin so long, thanks for the tips c ya and thanks again Quote Link to comment Share on other sites More sharing options...
sasa Posted June 8, 2008 Share Posted June 8, 2008 try <?php $data = array( array('id' => 1, 'order_no' => 20, 'name' => 'bxxx'), array('id' => 2, 'order_no' => 'NULL', 'name' => 'bbbbbbb'), array('id' => 3, 'order_no' => 'NULL', 'name' => 'cccc') ); foreach ($data[0] as $k => $v) $max_len[$k] = strlen($k); foreach ($data as $d){ foreach ($d as $k => $v){ if ($max_len[$k] < strlen("$v")) $max_len[$k] = strlen("$v"); } } $table = ''; foreach ($max_len as $i) $table .= '+-'.str_repeat('-', $i); $table .= "+\n"; foreach ($max_len as $n => $i) $table .= '| '.str_pad($n, $i, ' ',0); $table .= "|\n"; foreach ($max_len as $i) $table .= '+-'.str_repeat('-', $i); $table .= "+\n"; foreach ($data as $d){ foreach ($d as $k => $v){ $table .= '| '. str_pad($v, $max_len[$k], ' ', 0); } $table .= "|\n"; } foreach ($max_len as $i) $table .= '+-'.str_repeat('-', $i); $table .= "+\n"; echo $table; ?> Quote Link to comment Share on other sites More sharing options...
scod Posted June 9, 2008 Author Share Posted June 9, 2008 all right! I'll try that, thanks a lot! Im implementing something similiar but I'n not quite done Quote Link to comment Share on other sites More sharing options...
scod Posted June 9, 2008 Author Share Posted June 9, 2008 worked just great!! thanks a lot Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.