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

时间:2016-01-18 17:48:43

标签: php html mysql

This is the php/html table output

监考列是我希望mysql db中的行填满但输出不断重复相同名称的地方。 我想要的是输出应该是这样的 先生A. B先生 C先生 D先生 先生A. B先生 C先生 D先生 如果我们在数据库中只有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里面。

    class Data {

    // 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);
    // }


    ?>

1 个答案:

答案 0 :(得分:2)

这可能会有所帮助......

Unscheduled