Home>

I'm sorry for the question.

I'm currently studying php in a local environment and trying to create pagination.
Pagination does not work well.
When the next page is pressed, the same contents as the first page are displayed.

The error is displayed as Notice: Undefined index. It seems that the process is not separated by the first access and the second and subsequent accesses
I don't know how to solve it.

Thanks for your professor if you know how to solve it.

This is a postscript.
A little code was fixed.
Changed the link setting code of "Go to previous page" and "Go to next page" from echo to print.
Then, page feed worked fine, but the error display does not disappear.

New code.

<? php
require ('dbconnect.php');
$page = $_REQUEST ['page'];
    if ($page == '') {
    $page = 1;
}
$page = max ($page, 1);
// get the last page
$cnt = $mysqli->query ('SELECT COUNT (*) AS cnt FROM menu');
$table = mysqli_fetch_assoc ($cnt);
$maxPage = ceil ($table ['cnt']/5);
$page = min ($page, $maxPage);
$start = ($page-1) * 5;
?>

<div>

<table>
    <tr>
    <th>Product Management ID</ht>
    <th>Category</ht>
    <th>Product Name</ht>
    <th>Price</ht>
    <th>Remarks</ht>
    <th>Edit</ht>
    <th>Delete</ht>
    </tr>
<? php
$recodeSet = $mysqli->query ('SELECT * FROM menu ORDER BY id LIMIT'. $start. ', 5');
while ($table = mysqli_fetch_assoc ($recodeSet)) {
?>
<tr>
    <td><? php echo (htmlspecialchars ($table ['id']));?></td>
    <td><? php echo (htmlspecialchars ($table ['kind']));?></td>
    <td><? php echo (htmlspecialchars ($table ['name']));?></td>
    <td><? php echo (htmlspecialchars ($table ['price']));?></td>
    <td><? php echo (htmlspecialchars ($table ['remarks']));?></td>
    <td><a href = "update.php? id =<? php echo (htmlspecialchars ($table ['id']));?>">Edit</a></td&gt ;
    <td><a href = "delete.php? id =<? php echo (htmlspecialchars ($table ['id']));?>" onclick = "return confirmation ();">Delete</a></td>
</tr>

<? php
    }
?>
</table>
<ul>
<? php
    if ($page>1) {
?>
<li><a href = "index.php? page =<? php print ($page-1);?>">previous page</a></li>
<? php
    } else {
?>
<? php
}
?>
<? php
    if ($page<$maxPage) {
?>
<li><a href = "index.php? page =<? php print ($page + 1);?>">next page</a></li>
<? php
    } else {?>
<? php
    }
?>
</ul>
<a href = "http: //localhost/menu/input.php">Product Registration</a>
<a href = "http: //localhost/menu/menu-retrieval.php">Category List</a>
</div>

↓↓↓ is the code before changing from echo to print.

<? php
require ('dbconnect.php');
$page = $_REQUEST ['page'];
    if ($page == '') {
    $page = 1;
}
$page = max ($page, 1);
// get the last page
$cnt = $mysqli->query ('SELECT COUNT (*) AS cnt FROM menu');
$table = mysqli_fetch_assoc ($cnt);
$maxPage = ceil ($table ['cnt']/5);
$page = min ($page, $maxPage);
$start = ($page-1) * 5;
?>
<div>
  <table>
    <tr>
    <th>Product Management ID</ht>
    <th>Category</ht>
    <th>Product Name</ht>
    <th>Price</ht>
    <th>Remarks</ht>
    <th>Edit</ht>
    <th>Delete</ht>
    </tr>
<? php
$recodeSet = $mysqli->query ('SELECT * FROM menu ORDER BY id LIMIT'. $start. ', 5');
while ($table = mysqli_fetch_assoc ($recodeSet)) {
?>
<tr>
    <td><? php echo (htmlspecialchars ($table ['id']));?></td>
    <td><? php echo (htmlspecialchars ($table ['kind']));?></td>
    <td><? php echo (htmlspecialchars ($table ['name']));?></td>
    <td><? php echo (htmlspecialchars ($table ['price']));?></td>
    <td><? php echo (htmlspecialchars ($table ['remarks']));?></td>
    <td><a href = "update.php? id =<? php echo (htmlspecialchars ($table ['id']));?>">Edit</a></td&gt ;
    <td><a href = "delete.php? id =<? php echo (htmlspecialchars ($table ['id']));?>" onclick = "return confirmation ();">Delete</a></td>
</tr>
<? php
    }
?>
  </table>
<ul>
<? php
    if ($page>1) {
?>
<li><a href = "index.php? page =<? php echo '$page-1';?>">previous page</a></li>
<? php
    } else {
?>
<? php
}
?>
<? php
    if ($page<$maxPage) {
?>
<li><a href = "index.php? page =<? php echo '$page + 1';?>">Next page</a></li>
<? php
    } else {
?>
<? php
    }
?>
</ul>
</div>
  • Answer # 1

      

    When the next page is pressed, the same contents as the first page are displayed.

    Is the first page and the second page executed with the same query?
    I didn't move the presented code (I can't move it), so I debug it visually.

    The first question is here.

    $recodeSet = $mysqli->query ('SELECT * FROM menu ORDER BY id LIMIT'. $start. ', 5');

    When$startis the first page, is it0? Well, it ’s okay here. surely.
    Do you understand that on the second page,$startmust be5?

    Let's debug it.

    Because this code is difficult to debug, change it as follows.

    $statement = 'SELECT * FROM menu ORDER BY id LIMIT'. $start. ', 5';
    var_dump ($statement);// check the value here (comment out when not debugging)
    $recodeSet = $mysqli->query ($statement);

    Let's check each one like this.
    I know where to fix it, but if you can't debug it yourself, it's awkward so look here.

    Continue later.