kuchar kuchar - 5 months ago 18
MySQL Question

PHP get result, row by row on next button

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

<?php
// Database configuration
$dbHost = 'localhost';
$dbName = 'multimedialny';
$dbUser = 'multimedialny';
$dbPass = 'A74Ce5RmE5QnWKHQ';
// 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`);

Answer

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.

Comments