【问题标题】:Generating checkboxes in html tables from query results根据查询结果在 html 表中生成复选框
【发布时间】:2012-03-03 00:25:53
【问题描述】:

我正在尝试生成一个每行都有复选框的表格。我在下面找到了一个工作代码,用于根据查询结果生成表。是否可以在此处插入一个代码,该代码将提供一个额外的列,该列将在每一行中包含复选框?

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

?> 

【问题讨论】:

  • 我建议你使用你最喜欢的搜索引擎并寻找 a) PHP 教程 b) HTML 教程 ...

标签: php html mysql checkbox html-table


【解决方案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;
        }
?>

虽然生成的代码真的很丑 - 内联样式,呃。 -_-

【讨论】:

    【解决方案2】:

    这是一个非常简单的逻辑,对不起,我没有使用你的完整代码:

    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 更快

    我希望代码是正确的,如果不是,请使用var_dump() 看看数组$result$value 是什么样的。没测试也没写php 4周之类的

    编辑: 昨晚搞错了,不好意思,这里稍微更正一下。

    我觉得你的数据库表设计是这样的。

    table(id int auto_increment, something 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>";
      }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2017-11-15
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多