Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从查询结果生成html表中的复选框_Php_Html_Mysql_Checkbox_Html Table - Fatal编程技术网

Php 从查询结果生成html表中的复选框

Php 从查询结果生成html表中的复选框,php,html,mysql,checkbox,html-table,Php,Html,Mysql,Checkbox,Html Table,我试图生成一个每行都带有复选框的表。我在下面找到了一个基于查询结果生成表的工作代码。是否可以在此处插入一个代码,该代码将提供一个额外的列,该列将在每行中包含复选框 <?php function SQLResultTable($Query) { $link = mysql_connect("localhost","root" , "") or die('Could not connect: ' . mysql_error()); //build M

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

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

?> 

如果要将其作为每行的最后一列:

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

我建议你使用你最喜欢的搜索引擎,寻找a)PHP教程b)HTML教程。。。
<?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>";
  }
?>