Say you got a table view like this
seed_id name country
------- ---------- -------
1 John America
2 Jose Mexico
3 Khan Pakistan
and you'd like to paint the HMTL vertically as follows;
seed_id 1 2 3
name John Jose Khan
country America Mexico Pakistan
This kind of view comes very handy in two cases.
You want to print the table view or you want to compare fields side by side.
In print view, on a table with 50 fields, even printing a single record view does not get you a viewable print out. The paper will cut out the print proably at the 10th field or so.
but with a vertical view, it does not matter how many fields the table have.
Similarly, when you compare records side by side, like in this beautiful example
you get a very useful view.
I can sit down write this library function now but don't have the time at this moment. But I'm sure someone out there has either the time for it, or already has written it. Would you please share it please?
getview ($dbh,"select * from tableX where 1=1","vertical");
There's probably a better way that looping...
$table = array();
foreach($result_row as $row)
{
foreach(array('seed_id','name','country') as $index)
{
$table[$index][] = $row[$index];
}
}
Something like that should re-organise your array into the structure you'd need
You can use \G
flag.
SELECT * FROM mytbl \G
UPD: sample article: http://slaptijack.com/software/enabling-vertical-g-output-in-the-mysql-client/
eggyal I'm not answering my question. but this is the only way that I know at stackoverflow to repost a code as a follow up to my original question.
Anyway, I tried your link. ( that is Transposing multidimensional arrays in PHP ) But, it does not work for my case.
You can try it yourself and see. I attached two function needed to try this out. All you need is a mysql $dbh connection to give this function a go. You will see that in my function, I tapped into that flipdiagonally
function that was up voted 24 times in that link.
When you call the function with the $direction_v_or_h being set to h,
it works. but that's not news to us. It's the v
mode I'm after.
Try it with limit like this
SQL_getview($dbh, "select * from yourTable limit 2","h");
and
SQL_getview($dbh, "select * from yourTable limit 2","v");
The error that I get is this; repeated for every single field in the table
Warning: Invalid argument supplied for foreach() in D:\Hosting\5291100\html\blueprint\sql.php on line 739
function SQL_getview($dbh,$sql,$direction_v_or_h = 'h')
{
$result = $result = mysql_query($sql,$dbh);
$fields_num = mysql_num_fields($result);
if ($direction_v_or_h == "h"):
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>
";
while($row = mysql_fetch_row($result))
{
echo "<tr>";
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>
";
}
echo "</table>";
else:
if (1): //turn this to 0 to see the good old print_r workaround
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo "<td>{$field->name}</td>";
}
echo "</tr>
";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
$row = flipDiagonally($row);
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>
";
}
echo "</table>";
else:
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<pre>";
print_r ($row);
echo "</pre>";
echo "<hr>";
}
endif;
endif;
mysql_free_result($result);
}
function flipDiagonally($arr) {
$out = array();
foreach ($arr as $key => $subarr) {
foreach ($subarr as $subkey => $subvalue) {
$out[$subkey][$key] = $subvalue;
}
}
return $out;
}