Issues with dynamic website

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.

Here, you are treating $page as if it were an array.
But earlier you cast $page as an integer, taken from $_GET['page']
What do you really want page to equal in that link?

1 Like

I want the text in the bodytext and menuheader to appear in the tags when the link in the menuheader is clicked.

Have made some changes (best guesses). I have the menu text (Home, about, contact), but not the corresponding text when menu item is clicked.

The error message is: Warning : Trying to access array offset on value of type bool in C:\xampp\htdocs\Testpage\index.php on line 47

Here is what I did - again, best guess.

<?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']) ? ($_GET['page']) : 'index.php';

?>

<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='?page=index.php'>" . $row['menuheader'] . "</a>" . "</td>"; 
                    }
                 
                ?>
                   
           </tr>
       </table>
        
        <!-- Time lime menu -->
       
        
        <!-- body -->
        <?php echo $row['bodytext']; ?>
        
        
        <!-- footer -->
    </body>
</html>

Thank you for the help so far.

That was what I guessed, that you meant to use $row, not $page.

That sounds as if $row has a value of false, which points to the query not working as expected.

Now I look again, that’s a different line giving the error. It’s the part for the “bodytext”.
There you are outside the scope of the while loop, so $row is false.

The query that makes the menu selects all rows, you don’t pick a specific one for the page.
But for the body text, you want only the text for the specific page that matches the page ID…

1 Like

Thanks for the information. I know you don’t have to, but I was wondering if you can help me to show text when link is clicked, or if you know any examples using PHP OOP are that do what I am aiming to achieve.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.