2012-05-11 12 views
1

說你有這樣的顯示一個MySQL表垂直 - 進行比較或打印出的目的

seed_id  name   country 
-------  ----------  ------- 

1   John   America 
2   Jose   Mexico 
3   Khan   Pakistan 

表格視圖,你想垂直畫的HMTL如下:

seed_id  1    2   3 
name  John   Jose  Khan 
country  America  Mexico  Pakistan 

這種觀點在兩種情況下非常方便。

您想要打印表格視圖或想要並排比較字段。

在打印預覽,在表50場,甚至在打印單個記錄視圖不會讓你一個可見的打印出來。該報紙將在第十場左右切實印出照片。

但是在垂直視圖中,表格有多少個字段並不重要。

同樣,當你並排在這個美麗的例子

http://www.dpreview.com/products/compare/side-by-side?products=nikon_d90&products=nikon_d3&products=nikon_d4&sortDir=ascending

比較記錄的一面,就像你會得到一個非常有用的視圖。

我可以坐下來寫信這個庫的功能,但沒有在這一刻的時間。但是我確定有人有時間或者已經寫了。請您分享一下嗎?

getview($胸徑, 「SELECT * FROM TableX的其中1 = 1」, 「豎直」);

回答

0

有可能是循環更好的方式...

$table = array(); 
foreach($result_row as $row) 
{ 
    foreach(array('seed_id','name','country') as $index) 
    { 
    $table[$index][] = $row[$index]; 
    } 
} 

類似的東西應該重新組織你的數組結構你需要

+1

見http://stackoverflow.com/a/3423692/623041 – eggyal

+0

哈,那是相當時髦! – Scuzzy

6

可以使用\G標誌。

SELECT * FROM mytbl \G 

UPD:樣品文章:http://slaptijack.com/software/enabling-vertical-g-output-in-the-mysql-client/

+0

這是客戶端特定的(它可以在['mysql'](http://dev.mysql.com/doc/en/mysql.html)命令行工具中使用,但可能不在PHP中?)。 – eggyal

+0

我不太確定,最好試試。 – deadrunk

+0

這必須用於命令行界面。因爲當我在PHP代碼中的mysql_query中嘗試時,它返回語法錯誤。 –

0

eggyal我沒有回答我的問題。但這是我知道在stackoverflow重新發布代碼作爲我原來的問題後續行爲的唯一方法。

無論如何,我試過你的鏈接。 (即Transposing multidimensional arrays in PHP)但是,它不適用於我的情況。

你可以自己試試看。我附加了兩個需要試用的功能。你只需要一個mysql $ dbh連接就可以實現這個功能。你會看到,在我的功能中,我接觸到了在該鏈接中投票次數達到24次的flipdiagonally函數。

當你調用與$ direction_v_or_h功能被設定爲h,它的工作原理。但這對我們來說並不是新聞。這是我追求的v模式。

與極限嘗試像這樣

SQL_getview($dbh, "select * from yourTable limit 2","h"); 

SQL_getview($dbh, "select * from yourTable limit 2","v"); 

,我得到的是這樣的錯誤;重複每一個領域中的表

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>\n"; 
     while($row = mysql_fetch_row($result)) 
     { 
       echo "<tr>"; 
       foreach($row as $cell) 
         echo "<td>$cell</td>"; 
         echo "</tr>\n"; 
     } 
     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>\n"; 
      while($row = mysql_fetch_assoc($result)) 
      { 
        echo "<tr>"; 
         $row = flipDiagonally($row);     

        foreach($row as $cell) 
          echo "<td>$cell</td>"; 
          echo "</tr>\n"; 
      } 
      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; 
} 
+0

僅供參考如果你想添加這樣的後續,你可以編輯你的原始問題並將其包含在那裏。首先,你的'flipDiagonally'函數不是我鏈接的答案中的代碼(它使用'array_map'來轉置數組)。其次,您需要在整個結果集的數組上調用「flipDiagonally」,而不是在單個行上(使用'mysql_ *'庫,我認爲您首先需要通過遍歷結果來構建這樣一個數組;但如果你使用PDO代替 - 而你真的*應該* - 然後你可以使用['fetchAll'](http://www.php.net/manual/en/pdostatement.fetchall.php)) – eggyal