PHP函数循环SQL结果并生成HTML表

时间:2018-02-05 11:10:20

标签: php html mysql

我想编写一个php函数,它将以特定方式回显sql查询结果。

示例我的table 1 10.000 rows * 43 columns

NO NAME Prof Date of birth 1 John Teacher 1987 2 Nick Engineer 1976 3 4 5

等等。基于No integer(1-10.000),我想生成以下表格:

Name: John Prof: Teacher Date of birth: 1987

Name: Nick Prof: Engineer Date of birth: 1976

到目前为止我的代码:

`         

    $hostname = "";     
    $username = "";
    $password = "";
    $dbname = "db1";

    //connection to the database
    $con = mysqli_connect($hostname, $username, $password, $dbname)
        or die("Unable to connect to MySQL");
    mysqli_set_charset($con, 'utf8');
    /*echo "Connected to db1 database <br>";
    else
    echo "Could not connect"; */

    $query1 = "SELECT * FROM `table 1` WHERE CODE_NO = 1";
    $query2 = "SHOW COLUMNS FROM table 1";
    $result1 = mysqli_query($con, $query1);
    $result2 = mysqli_query($con, $query2);

    // Check result
    // Useful for debugging.
     if (!$result1) {
        $message  = 'Invalid query: ' . mysqli_error($con) . "\n";
        $message .= 'Whole query: ' . $query1;
        die($message);
    }

    echo "<table>"; // table tag in the HTML
        while($row = mysqli_fetch_array($result1))
        //Creates a loop to loop through results
        {
        echo

        "<tr>
            <th>CODE_NO:</th>
            <td>" . $row['CODE_NO'] . "</td>
        </tr>
        <tr>
            <th>FIRST_NAME:</th>
            <td>" . $row['FIRST_NAME'] . "</td>
        </tr>
        <tr>
            <th>SURNAME:</th>
            <td>" . $row['SURNAME'] . "</td>
        </tr>
        <tr>
            <th>Date of birth:</th>
            <td>" . $row['DOB'] . "</td>
        </tr>
        <tr>
            <th>Date of death:</th>
            <td> " . $row['DOD'] . "</td>
        </tr>";
        }
    echo "</table>"; //Close the table in HTML

    ?>`

是否可以对流程进行一次编码,而无需对任何内容进行硬编码,因此可以根据需要重复多次?

编辑:

$x = 1; $query = "SELECT * FROM {人员{1}}

3 个答案:

答案 0 :(得分:1)

如果我理解正确,这将满足你的需要:

function generateTableFromResult($result) {
   $html = "<table>";
   while($row = mysqli_fetch_array($result)) {
      foreach($row as $column => $value) {
        $html.="<tr><th>".$column."</th><td>".$value."</td></tr>";
      }
   }
   $html.="</table>";
   return $html;
}

// usage:
// ...
$result1 = mysqli_query($con, $query1);
echo generateTableFromResult($result1);

答案 1 :(得分:0)

您可以获得例外结果。 您需要使用值递增变量并在结尾处返回。

参见我的例子:

// Check result
// Useful for debugging.
 if (!$result1) {
    $message  = 'Invalid query: ' . mysqli_error($con) . "\n";
    $message .= 'Whole query: ' . $query1;
    die($message);
}

$table = "<table>"; // table tag in the HTML
    while($row = mysqli_fetch_array($result1)){
    //Creates a loop to loop through results
    $table .="<tr>";
    $table .="  <th>CODE_NO:</th>";
    $table .="  <td>" . $row['CODE_NO'] . "</td>";
    $table .="</tr>";
    $table .="<tr>";
    $table .="    <th>FIRST_NAME:</th>";
    $table .="    <td>" . $row['FIRST_NAME'] . "</td>";
    $table .="</tr>";
    $table .="<tr>";
    $table .="    <th>SURNAME:</th>";
    $table .="    <td>" . $row['SURNAME'] . "</td>";
    $table .="</tr>";
    $table .="<tr>";
    $table .="   <th>Date of birth:</th>";
    $table .="   <td>" . $row['DOB'] . "</td>";
    $table .="</tr>";
    $table .="<tr>";
    $table .="    <th>Date of death:</th>";
    $table .="    <td> " . $row['DOD'] . "</td>";
    $table .="</tr>";
    }
$table .= "</table>"; //Close the table in HTML
echo $table;
?>`

这是获得所需结果的可能方法。 但是,您使用json以json格式发送了一个数组,并在前端使用js构建表。

答案 2 :(得分:0)

从上方修改后的函数generateTableFromResult可以正常工作

function generateTableFromResult($result) {
   $html = "";
   $i = 0;
   $header = "<tr>";
   $body = "";
   while($row = mysqli_fetch_assoc($result)) {
      $i += 1;
      
      $body .= "<tr>\n";
      foreach($row as $column => $value) {
          if ($i == 1){
              $header .= "<th>$column</th>\n";
          }
         $body .= "<td>$value</td>\n";
      }
      $body .= "</tr>\n";
   }
   $header .= "</tr>\n";
   $html .= "<table> $header $body</table>";
   return $html;
}