2012-02-10 85 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 2012-02-10 14:40:21

+1

我建議你使用你最喜歡的搜索引擎,並尋找a)PHP教程b)HTML教程... – ManseUK 2012-02-10 14:51:50

回答

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; 
     } 
?> 

雖然代碼的系統產生實在是太醜 - 內嵌樣式,唉。 -_-

0

它的一個非常簡單的邏輯,對不起,我不使用你的完整代碼:

的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> 

那簡直是所有你需要做的我思考,請與mysql_fetch_array() http://php.net/manual/en/function.mysql-fetch-array.php工作,它甚至更快

我希望的代碼是否正確,如不看什麼陣列$result$value就像通過使用var_dump()。沒有測試它,並沒有寫PHP 4周或類似這樣的東西

編輯: 我昨晚做了一個錯誤,對不起,這裏有一點修正。

我認爲你的數據庫表的設計就像。

表(ID INT AUTO_INCREMENT,東西VARCHAR(255)NOT NULL,PRIMARY KEY(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>"; 
    } 
?> 
相關問題