2012-02-10 8 views
0

各行にチェックボックスを含むテーブルを生成しようとしています。私はクエリの結果に基づいてテーブルを生成するための作業コードを以下に見つけました。各行にチェックボックスを含む追加の列を提供するコードをここに挿入することは可能ですか?クエリ結果からhtmlテーブルのチェックボックスを生成する

<?php 

    function SQLResultTable($Query) 
    { 
     $link = mysql_connect("localhost","root" , "") or die('Could not connect: ' . mysql_error());  //build MySQL Link 
     mysql_select_db("dbName") or die('Could not select database');  //select database 
     $Table = ""; //initialize table variable 

     $Table.= "<table border='1' style=\"border-collapse: collapse;\">"; //Open HTML Table 

     $Result = mysql_query($Query); //Execute the query 
     if(mysql_error()) 
     { 
      $Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>"; 
     } 
     else 
     { 
      //Header Row with Field Names 
      $NumFields = mysql_num_fields($Result); 
      $Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\">"; 
      for ($i=0; $i < $NumFields; $i++) 
      { 
       $Table.= "<th>" . mysql_field_name($Result, $i) . "</th>"; 
      } 
      $Table.= "</tr>"; 

      //Loop thru results 
      $RowCt = 0; //Row Counter 
      while($Row = mysql_fetch_assoc($Result)) 
      { 
       //Alternate colors for rows 
       if($RowCt++ % 2 == 0) $Style = "background-color: #FFCCCC;"; 
       else $Style = "background-color: #FFFFFF;"; 

       $Table.= "<tr style=\"$Style\">"; 
       //Loop thru each field 
       foreach($Row as $field => $value) 
       { 
        $Table.= "<td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp$value&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</td>"; 
       } 
       $Table.= "</tr>"; 
      } 

     } 
     $Table.= "</table>"; 

     return $Table; 
    } 

?> 
+0

はい、その可能。 – rcdmk

+1

あなたのお気に入りの検索エンジンを使って、a)PHPチュートリアルb)HTMLチュートリアル... – ManseUK

答えて

1

あなたは、各行の最後の列としてそれをしたい場合は、次のコードのように生成されていますが、本当に醜いですが

<?php 

     function SQLResultTable($Query) 
     { 
      $link = mysql_connect("localhost","root" , "") or die('Could not connect: ' . mysql_error());  //build MySQL Link 
      mysql_select_db("dbName") or die('Could not select database');  //select database 
      $Table = ""; //initialize table variable 

      $Table.= "<table border='1' style=\"border-collapse: collapse;\">"; //Open HTML Table 

      $Result = mysql_query($Query); //Execute the query 
      if(mysql_error()) 
      { 
       $Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>"; 
      } 
      else 
      { 
       //Header Row with Field Names 
       $NumFields = mysql_num_fields($Result); 
       $Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\">"; 
       for ($i=0; $i < $NumFields; $i++) 
       { 
        $Table.= "<th>" . mysql_field_name($Result, $i) . "</th>"; 
       } 
       $Table.= "<th>Checkbox column</th>"; 
       $Table.= "</tr>"; 

       //Loop thru results 
       $RowCt = 0; //Row Counter 
       while($Row = mysql_fetch_assoc($Result)) 
       { 
        //Alternate colors for rows 
        if($RowCt++ % 2 == 0) $Style = "background-color: #FFCCCC;"; 
        else $Style = "background-color: #FFFFFF;"; 

        $Table.= "<tr style=\"$Style\">"; 
        //Loop thru each field 
        foreach($Row as $field => $value) 
        { 
         $Table.= "<td>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp$value&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</td>"; 
         $Table.= "<td><input type="checkbox" name="nameHere" value="valueHere" ></td>"; 
        } 
        $Table.= "</tr>"; 
       } 

      } 
      $Table.= "</table>"; 

      return $Table; 
     } 
?> 

- インラインスタイル、ぐふ。

のindex.php:

<html> 
    <head> 
    <title>your page</title> 
    </head> 
    <body> 
    <form method="post"> 
     <table> 
     <caption>myTable</caption> 
     <thead> 
      <tr> 
      <th>column with checkbox</th> 
      <th>column with text</th> 
      </tr> 
     </thead> 
     <tbody> 
      <?php 
      // get your databaseresult to an array called $result 
      $connection = mysql_connect("server", "user", "password"); 
      mysql_select_database("databasename"); 
      $resultHash = mysql_query("SELECT * FROM mytable"); 

      while($row = mysql_fetch_array($resultHash)){ 
       $result[] = $row; 
      } 
      mysql_close($connection); // never forget to close the connection if not longer needed 

      foreach($result as $key => $value) 
      { 
       echo "<tr>\r\n"; 
       echo " <td><input type=\"checkbox\" name=\"yourCheckboxName".$key."\" /></td>\r\m"; 
       // $key is the index of your numeric $result array 
       echo " <td>".$value[0]."</td>\r\n"; 
       echo "</tr>\r\n"; 
      } 
      ?> 
     <tbody> 
     </table> 
    </form> 
    </body> 
<html> 

単にあなたが私を行うために必要なすべてのです-_-

0

そのは非常に単純ロジック、私はあなたの完全なコードを使用していない私を言い訳思うし、それがさらに速く、私は見ていない場合は、コードが正しいことを願う

mysql_fetch_array() http://php.net/manual/en/function.mysql-fetch-array.phpで作業してくださいどのような配列$result$valuevar_dump()を使用するようなものです。それをテストしていないし、このような何かPHPを書くことはなかった

編集: 私は昨晩ミスをした、申し訳ありません、ここで少し訂正です。

あなたのデータベーステーブルのデザインは似ていると思います。

テーブル(ID int型のAUTO_INCREMENT、何かのVARCHAR(255)NOT NULL、主キー(ID))

<?php 
    foreach($result as $index => $row){ 
    echo "<tr>"; 
    echo "<td>"; 
     echo "<input type='checkbox' name='yourname".$index."' />"; // now every checkbox has an unique identifier 
    echo "</td>"; 
    foreach($row as $column => $value){ 
     echo "<td>"; 
     echo "column = ".$column; 
     echo "\r\n"; 
     echo "value = ".$value; 
     echo "<td>"; 
    } 
    echo "</tr>"; 
    } 
?> 
関連する問題