0

I want to get single row on page and display another when click next button.

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

For now i get result only of first row in table, i want to show next results when user click on next button. Is it possible to make without paginate?

My database:

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`);
6
  • You want to make a full reload holding the next entry or do you want to add the entry to the loaded page? Commented Jun 27, 2016 at 9:22
  • Why not output them all first and hude them using javascript or css. Then just use client scripts to control Commented Jun 27, 2016 at 9:24
  • One solution could be to add "WHERE pageNumber == '$pageNumber'" into your query, then use JQuery to increase that value for each click they make on Next Result, or decrease it for each click on Previous result. Commented Jun 27, 2016 at 9:28
  • @SuperCoolHandsomeGelBoy i do want to display all because i want to have 4k rows in one table. Commented Jun 27, 2016 at 9:30
  • I think jquery and javascript wont help here because i want to have in table 4k rows and also i want to save where user stop display rows. Commented Jun 27, 2016 at 9:36

1 Answer 1

2

Update your query by this:

$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>`

You can do the same thing using ajax/jquery also. This is only the concept. You need to implement validation also.

Sign up to request clarification or add additional context in comments.

4 Comments

It works great but could you explain how its done? :) Or send me any link to read how it works.
I have just pass the $start which control the query so the it fetch single data will start fetch on the basis of $start value.
Is there any way to hide $_REQUEST id from url? For now its looks like domain.com/?ID=1
You can encrypt it using salt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.