php - 如何从 mysql 中检索行到 php/html 表中

标签 php html mysql

This is the php/html table output

监考员列是我希望 mysql 数据库中的行填满的地方,但输出不断重复相同的名称。 我想要的是输出应该是这样的 A先生 B先生 C先生 丁先生 A先生 B先生 C先生 丁先生 如果我们在数据库中只有 4 个名字(即按 ASC 顺序列出名字并一次又一次地重复)。 这是我的 php/html

<table class="table table-bordered table-hover table-striped">
    <caption>Timetable with number of students</caption>
         <thead>
              <tr>
                <th>#</th>
                <th>No. Students</th>
                <th>Course</th>
                <th>Date</th>
                <th>Start Time</th>
                <th>End Time</th>
                <th>Venue</th>
                <th>Duration</th>
                <th>Invigilator</th>
                <!-- <th>Edit/Del</th> -->
              </tr>
          </thead>
          <tbody>
                <?php
                    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
                    extract($row);
                    include('includes/shuffle.php');
                    while ($snm = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
                    $snm1 = $snm["snm_pdo"];
                ?>
           <tr>
           <?php echo "<td>{$id_pdo}</td>" ?>
           <?php echo "<td>{$snb_pdo}</td>" ?>
           <?php echo "<td>{$cos_pdo}</td>" ?>
           <?php echo "<td>{$dat_pdo}</td>" ?>
           <?php echo "<td>{$starttim_pdo}</td>" ?>
           <?php echo "<td>{$endtim_pdo}</td>" ?>
           <?php echo "<td>{$ven_pdo}</td>" ?>
           <?php echo "<td>{$dur_pdo}</td>" ?>
           <?php echo "<td>{$snm1}</td>" ?>

            </tr>
         <?php
                    }
                    }
                ?>

这是我包含的 shuffle.php

<?php  
    $con=mysqli_connect("localhost", "root", "", "timetable");
    if(!$con){
        die("Bros db no wan connect o" .mysqli_connect_error());
    }       

$query = "SELECT snm_pdo FROM staff_list ORDER BY snm_pdo ASC, RAND() LIMIT 3";
$results = mysqli_query($con, $query);mysqli_close($con);?>
  • This is the structure of the 2nd table(courses) 这是对 table_courses 的查询 这是在 data.inc_course.php 里面

    类数据{

    // database connection and table name
    private $conn;
    private $table_name = "courses";
    // private $table_name2 = "staff_list";
    // object properties
    public $id;
    public $snb;
    public $cos;
    public $dat;
    public $starttim;
    public $endtim;
    public $ven;
    public $dur;
    public function __construct($db){
        $this->conn = $db;
    }
    

//阅读商品

    function readAll($page, $from_record_num, $records_per_page){
        $query = "SELECT 
                    *
                FROM 
                    " . $this->table_name . "
                ORDER BY 
                    id_pdo ASC 
                LIMIT 
                    {$from_record_num}, {$records_per_page}";
        $stmt = $this->conn->prepare( $query );
        $stmt->execute();
        return $stmt;
    }
}

然后在我的 index.php 页面中有这个

<?php
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$records_per_page = 5;
$from_record_num = ($records_per_page * $page) - $records_per_page;
include_once 'includes/config.php';
include_once 'includes/data.inc_course.php';
include 'includes/shuffle2.php';
$database = new Config();
$db = $database->getConnection();
$product = new Data($db);
$stmt = $product->readAll($page, $from_record_num, $records_per_page);
$num = $stmt->rowCount();
?>
<?php include('includes/header.php'); 
$output1 = "
    <body>
        <div id='wrapper'>";
        echo $output1;
       include('includes/nav.php');
$output2 = "
       <!-- Page Content -->
        <div id='page-wrapper'>
            <div class='container-fluid'>
                <div class='row'>
                    <div class='col-lg-12'>
                        <h1 class='page-header'>Timetable Details</h1>
                    </div>
                    <!-- /.col-lg-12 -->

                    <!-- niyicode -->
                <div class='col-lg-12'>
                    <p>
                        <a class='btn btn-primary' href='courses/add.php' role='button'>Add Course</a>
                        <a class='btn btn-primary fleft' href='staff/add.php' role='button'>Add Staff</a>
                    </p>";
        echo $output2;
        if($num>0){
$output =  "           
        <table class= 'table table-bordered table-hover table-striped'>
            <caption>Timetable with number of students</caption>
            <thead>
                <tr>
                    <th>#</th>
                    <th>No. Students</th>
                    <th>Course</th>
                    <th>Date</th>
                    <th>Start Time</th>
                    <th>End Time</th>
                    <th>Venue</th>
                    <th>Duration</th>
                    <th>Invigilator</th>
                    <!-- <th>Edit/Del</th> -->
                </tr>
            </thead>
        <tbody>";
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    extract($row);
    if ($snm = $results->FETCH_ASSOC()) {
    $output .= ' <tr>' . PHP_EOL;
    $output .= '   <td>'.$row['id_pdo'].'</td>' . PHP_EOL;
    $output .= '   <td>'.$row['snb_pdo'].'</td>' . PHP_EOL;
    $output .= '   <td>'.$row['cos_pdo'].'</td>' . PHP_EOL;
    $output .= '   <td>'.$row['dat_pdo'].'</td>' . PHP_EOL;
    $output .= '   <td>'.$row['starttim_pdo'].'</td>' . PHP_EOL;
    $output .= '   <td>'.$row['endtim_pdo'].'</td>' . PHP_EOL;
    $output .= '   <td>'.$row['ven_pdo'].'</td>' . PHP_EOL;
    $output .= '   <td>'.$row['dur_pdo'].'</td>' . PHP_EOL;
    $output .= '   <td>'.$snm["snm_pdo"].'</td>' . PHP_EOL;
    // Add more here if appropriate .... . PHP_EOL;
    $output .= ' </tr>' . PHP_EOL;
}
}
$output .='      </tbody>
    </table>    ';
$results->free();
mysqli_close($mysqli);
echo $output;
$page_dom = "index.php";
include_once 'includes/pagination.inc_course.php';
}else{
?>
                    <div class="alert alert-warning alert-dismissible" role="alert">
                      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                      <strong>Warning!</strong> No Data. Please add data to view timetable
                    </div>
                    <?php
                    }
                    ?>
<?php   
$output5 = "                       </div>
                </div>
                <!-- /.row -->
            </div>
            <!-- /.container-fluid -->
        </div>
        <!-- /#page-wrapper -->
    </div>
    <!-- /#wrapper -->
    ";
        echo $output5;
?>
<!-- echo $output;                     -->
    <?php include('includes/footer.php'); ?>

这是用于 staff_list 表查询的文件 shuffle2.php

<?php  
    define('dbname', 'timetable');
    define('dbuserid', 'root');
    define('dbpasswd', '');
    define('dbhost', 'localhost');
    define('dbport', '3306');
    define('dbsocket', '');
    define('LBRK','<br />');

    // Connect to database
    $mysqli = new mysqli(dbhost, dbuserid, dbpasswd, dbname, dbport, dbsocket);
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error.LBRK;
     exit(1);
    }

    $query1 = "SELECT snm_pdo FROM staff_list ORDER BY snm_pdo ASC, RAND() ";

    $results = $mysqli->query($query1);
    // if ($results===false) {
    //     // Query failed
    //     echo "Failed to read the database table ''<br />" .LBRK;
    //     exit(1);
    // }


    ?>

最佳答案

这可能有帮助……

<?php  
    define('dbname', 'timetable');
    define('dbuserid', 'root');
    define('dbpasswd', '******');
    define('dbhost', 'localhost');
    define('dbport', '3306');
    define('dbsocket', '');
    define('LBRK','<br />');

    // Connect to database
    $mysqli = new mysqli(dbhost, dbuserid, dbpasswd, dbname, dbport, dbsocket);
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error.LBRK;
     exit(1);
    }

    $query = "SELECT * FROM staff_list ORDER BY snm_pdo ASC, RAND() LIMIT 3";

    $results = $mysqli->query($query);
    if ($results===false) {
        // Query failed
        echo "Failed to read the database table 'staff_list'<br />" .LBRK;
        exit(1);
    }

    // Process data

    $output = "<!DOCTYPE html>
    <html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\">
    <head>

    <meta charset=\"utf-8\">
    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"> 

    <title>Courses List</title>
    </head>
    <body>
        <table class=\"table table-bordered table-hover table-striped\">
            <caption>Timetable with number of students</caption>
            <thead>
                <tr>
                    <th>#</th>
                    <th>No. Students</th>
                    <th>Course</th>
                    <th>Date</th>
                    <th>Start Time</th>
                    <th>End Time</th>
                    <th>Venue</th>
                    <th>Duration</th>
                    <th>Invigilator</th>
                    <!-- <th>Edit/Del</th> -->
                </tr>
            </thead>
        <tbody>
";
while($row = $result->fetch_assoc()) {
    $output .= ' <tr>' . PHP_EOL;
    $output .= '   <td>'.$row['id_pdo'].'</td>' . PHP_EOL;
    $output .= '   <td>'.$row['snb_pdo'].'</td>' . PHP_EOL;
    $output .= '   <td>'.$row['cos_pdo'].'</td>' . PHP_EOL;
    $output .= '   <td>'.$row['dat_pdo'].'</td>' . PHP_EOL;
    $output .= '   <td>'.$row['starttim_pdo'].'</td>' . PHP_EOL;
    $output .= '   <td>'.$row['endtim_pdo'].'</td>' . PHP_EOL;
    $output .= '   <td>'.$row['ven_pdo'].'</td>' . PHP_EOL;
    $output .= '   <td>'.$row['dur_pdo'].'</td>' . PHP_EOL;
    // Add more here if appropriate .... . PHP_EOL;
    $output .= ' </tr>' . PHP_EOL;
}
$output .="      </tbody>
    </table>    
</body>
</html>
";

$results->free();
mysqli_close($mysqli);

echo $output;

?>

关于php - 如何从 mysql 中检索行到 php/html 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34860968/

相关文章:

mysql - 使用存储过程(mysql)内查询中的值进行操作

php 脚本和模具报告

mysql - 关系数据库中的链修改

html - 如何为每组内联 block 开始一个新行?

html - 尝试导入本地字体,但不起作用

php - mkdir 的权限将不起作用

php - 每 5 秒用 AJAX 刷新我的 php 页面

html - 当有人在网页的当前行中时,如何在我的导航栏中显示/保持悬停效果

php - 使用 laravel scout 和 tntsearch 调用未定义的方法 Illuminate\Database\Eloquent\Builder::search()

php - 如何在 Ubuntu 环境中为 XAMPP 启用 Redis 扩展。