PHP在下一个按钮上逐行获取结果

时间:2016-06-27 09:20:18

标签: php mysql

我想在页面上获得单行,并在单击下一步按钮时显示另一行。

<?php
        // Database configuration
    $dbHost = 'localhost';
    $dbName = 'multimedialny';
    $dbUser = 'root';
    $dbPass = '';
        // Database connection
    $dbConnect = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
        // Check database connection
    if ($dbConnect -> connect_errno) {
      echo "MySQL Connection error: " . $dbConnect -> connect_errno;
      exit();
    }
        // Database Query
    $dbQuery = "SELECT * FROM course ORDER by ID";
    $result = $dbConnect -> query($dbQuery);
    $row = $result -> fetch_array(MYSQLI_ASSOC);
        // Count number of rows in table
    $rowCount = $result -> num_rows;
?>

<html>
    <head>
        <title>Title Page</title>
    </head>
    <body>
        <?php echo $row["ID"]; ?>
        <br>
        <?php echo $row["CourseName"]; ?>
        <br>
        <?php echo $row["ModuleName"]; ?>
        <br>
        <?php echo $row["Contents"]; ?>
        <br><br>
        <a href="#">Next Result</a>
        <a href="#">Previous Result</a>
    </body>
</html>

现在我只获得表格中第一行的结果,我想在用户点击下一个按钮时显示下一个结果。是否可以在没有分页的情况下制作?

我的数据库:

CREATE TABLE `course` (
  `ID` int(11) NOT NULL,
  `CourseName` text NOT NULL COMMENT 'Kategoria kursu',
  `ModuleName` text NOT NULL COMMENT 'Nazwa modułu',
  `Contents` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `course` (`ID`, `CourseName`, `ModuleName`, `Contents`) VALUES
(1, 'Prawo jazdy kat. B', 'Zagadnienia podstawowe', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eaque illum repellat harum reiciendis ea officia numquam iusto excepturi consequatur libero sit, nemo, iste obcaecati distinctio soluta eum molestias, eos voluptates. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odio eligendi ullam voluptatem vero soluta reiciendis, consectetur quidem aperiam consequatur, facilis et. Quasi omnis eos vitae voluptatibus. Quis accusantium, debitis esse! '),
(2, 'Prawo jazdy kat. B', 'Zagadnienia podstawowe', 'Odio eligendi ullam voluptatem vero soluta reiciendis, consectetur quidem aperiam consequatur, facilis et. Quasi omnis eos vitae voluptatibus. Quis accusantium, debitis esse! ');

ALTER TABLE `course`
  ADD PRIMARY KEY (`ID`);

1 个答案:

答案 0 :(得分:1)

通过以下方式更新您的查询:

$start = !empty($_REQUEST['start']) ? $_REQUEST['start'] : 0 ;
$dbQuery = "SELECT * FROM course ORDER by ID limit $start, 1 ";`

<a href="<yourpg_name>?start=<?php echo ($start == 0 ) ? 0 : $start-1 ?>">Previous Result</a>
    $start++; 
<a href="<yourpg_name>?start=<?php echo $start?>">Next Result</a>`

你也可以使用ajax / jquery做同样的事情。 这只是概念。 您还需要实现验证。