Jacques Posted October 4, 2018 Share Posted October 4, 2018 Hi All. I want to update my page titles dynamically with the applicable title in the selected language (Chapter 17 - Forum). I have searched the web without success. My code below shows the default "Website Name". Any help will be much appreciated. Regards. header.html snippet <!-- Page title --> <title><?php // Use the default page title if one was not provided: if (isset($page_title)) { echo $page_title; } else { echo 'Website Name'; } ?></title> signup.php snippet // Include the page title: $page_title = $words['signup']; Link to comment Share on other sites More sharing options...
Larry Posted October 5, 2018 Share Posted October 5, 2018 You need to ensure that every page that includes header.html sets a $page_title variable. Link to comment Share on other sites More sharing options...
Jacques Posted October 5, 2018 Author Share Posted October 5, 2018 (edited) Hi Larry, Thank you for your response. I have included the $page_title variable in all the applicable php scripts. However the code below gives an "Undefined index: signup" error. // Include the page title: $page_title = $words['signup']; Edited October 5, 2018 by Jacques Link to comment Share on other sites More sharing options...
Larry Posted October 5, 2018 Share Posted October 5, 2018 Ah, okay, so that would mean the $words array, which presumably comes from the database query, has no 'signup' element, which means there's no such column in the database. Link to comment Share on other sites More sharing options...
Jacques Posted October 6, 2018 Author Share Posted October 6, 2018 Hi Larry, Sorry for the oversight, I have added the words to the database. But there still seems to be an issue as the code now displays the default page title. It doesn't seem to pick up the $words variable. Any further suggestions would be much appreciated. Thank you. Link to comment Share on other sites More sharing options...
Larry Posted October 6, 2018 Share Posted October 6, 2018 Do you make sure that the $words variable is populated before the header file is included? Link to comment Share on other sites More sharing options...
Jacques Posted October 6, 2018 Author Share Posted October 6, 2018 Hi Larry, I have tested the $words variable before and after including the header.html file. The error I referred to previously occurred with the $words variable being populated after including the header.html file. When populating the $words variable before including the header.html file, I get the following error: "Undefined variable: words". I include the code snipped below. Regards. <?php // Require the configuration before any PHP code as the configuration controls error reporting: require('includes/config.inc.php'); // The config file also starts the session. // Require the database connection: require(MYSQL); // Include the page title: $page_title = $words['signin']; // Include the HTML header file: include('templates/header.html'); // Array for storing sign up errors: $reg_errors = array(); // Check for a form submission: if ($_SERVER['REQUEST_METHOD'] === 'POST') { ... Link to comment Share on other sites More sharing options...
Larry Posted October 7, 2018 Share Posted October 7, 2018 I think we're not quite following each other here. Let's look at this logically... 1. header.html uses if (isset($page_title)) { so $page_title MUST be set in the file that includes the header and before the header is included. 2. The code you posted above does exactly that, which is great. $page_title = $words['signin']; 3. That code only works if $words is defined. There's nothing in the code you've posted that defines $words. Does your config.inc.php or MYSQL script do that? Again, this is a pretty basic logic issue and you may find that if you talk it out you might realize where the problem is. Link to comment Share on other sites More sharing options...
Jacques Posted October 12, 2018 Author Share Posted October 12, 2018 (edited) Hi Larry, The code below defining the $words variable has been included in my header.html script above the <!DOCTYPE html> declaration (as per Chapter 17 - Forum). The language functionality works perfectly fine across my web app except for in the page title where it gives the "Undefined index" error. Regards. // Look for a language id: // Then store the language ID in the session: if (isset($_GET['lid']) && filter_var($_GET['lid'], FILTER_VALIDATE_INT, array('min_range' => 1)) ) { $_SESSION['lid'] = $_GET['lid']; } elseif (!isset($_SESSION['lid'])) { $_SESSION['lid'] = 1; // Default. } // Get the words for selected language: $q = "SELECT * FROM words WHERE lang_id = {$_SESSION['lid']}"; $r = mysqli_query($dbc, $q); if (mysqli_num_rows($r) == 0) { // Invalid language ID. // Use the default language: $_SESSION['lid'] = 1; // Default. $q = "SELECT * FROM words WHERE lang_id = {$_SESSION['lid']}"; $r = mysqli_query($dbc, $q); } // Fetch the results into a variable: $words = mysqli_fetch_array($r, MYSQLI_ASSOC); // Free the results: mysqli_free_result($r); Edited October 12, 2018 by Jacques Link to comment Share on other sites More sharing options...
Larry Posted October 12, 2018 Share Posted October 12, 2018 Okay, that's good. Except if your header file defines the $words variable, then you can't set $page_title to $words['anything'] before the header b/c $words doesn't exist yet. Link to comment Share on other sites More sharing options...
Jacques Posted October 15, 2018 Author Share Posted October 15, 2018 Hi Larry, It took me a while but I finally got it!:) I moved the code defining the $words variable from the header.html script to the mysql.inc.php script and the $page_title variable now updates perfectly! Thank you so much for your time and patience and for getting me to finally understand the logic! Regards. Link to comment Share on other sites More sharing options...
Larry Posted October 20, 2018 Share Posted October 20, 2018 No problem. Glad you've got it working! Link to comment Share on other sites More sharing options...
Recommended Posts