I have a top menu with links. What I am trying to do, is that when a link is clicked, data for that page is displayed on the index page. So clicking home will have ‘home’ data.
The problem is I get error messages. I am using PHP OOP and being new to this, have no idea how to solve the issues.
The errors are:
Trying to access array offset on value of type int in C:\xampp\htdocs\Testpage\index.php on line 35
Warning : Trying to access array offset on value of type bool in C:\xampp\htdocs\Testpage\index.php on line 47
This is the php code:
<?php
include 'newFunct.php';
// Connect to MySQL database
$pdo = pdo_connect_mysql();
// Get the page via GET request (URL param: page), if non exists default the page to 1
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
?>
<html>
<head>
<meta charset="UTF-8">
<title>test page</title>
<!-- site styles -->
<link rel="stylesheet" type="text/css" media="screen" href="css/site-styles.css">
</head>
<body>
<!-- top menu -->
<table id="topMenu">
<tr>
<td>
<h1 class="siteName">test</h1>
</td>
<?php
$sql = "SELECT * FROM testtable01";
$result = $pdo->query($sql);
while($row = $result->fetch()) {
echo "<td class='navItem'>" . "<a href='index.php?page={$page['menuheader']}'>" . $row['menuheader'] . "</a>" . "</td>";
}
?>
</tr>
</table>
<!-- Time lime menu -->
<!-- body -->
<?php echo $row['bodytext']; ?>
<!-- footer -->
</body>
</html>```
I also include the database connection code:
<?php
function pdo_connect_mysql() {
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = '';
$DATABASE_NAME = 'mysite';
try {
return new PDO('mysql:host=' . $DATABASE_HOST . ';dbname=' . $DATABASE_NAME . ';charset=utf8', $DATABASE_USER, $DATABASE_PASS);
} catch (PDOException $exception) {
// If there is an error with the connection, stop the script and display the error.
exit('Failed to connect to database!');
}
}
?>
I’m guessing I’m missing something. Any help? Please not, as mentioned, I am new to OOP so please be kind to me.