SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP include not working with isset

    Hi All,
    I am trying to learn PHP and am going through David Powers' PHP Solutions. I am getting the hang of things and finally beginning to understand after going through Lynda.com and a bunch of online resources. Now I am tackling this book and I am confused about why something doesn't work. It is from pg 82-83 of his book and involves the use of an include to set the page title. All the other includes from this chapter work fine and so does this one - but only if a suggested isset statement is used. What I am having trouble with is why this is needed to make it work. The include code says to change the title to the filename minus the extension and has a condition that says if the title pages is index set $title = home. Unless you use the isset statement (<?php if (isset($title)) {echo "—{$title}";} ?> it will not work. Why is that? If there was a problem with the include it wouldn't work at all, yet when the isset statement isn't there, it doesn't work. Any ideas?

    Thanks ahead.

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,130
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Can you show us the code where you are defining the value of $title? You made mention of $title = home, but that isn't valid PHP, so I'd like to see exactly how you are setting the value of $title.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the content of title.inc.php
    <?php
    $title = basename($_SERVER['SCRIPT_FILENAME'], '.php');
    $title = str_replace('_', ' ', $title);
    if ($title == 'index') {
    $title = 'home';
    }
    $title = ucwords($title);

    Below is the webpage I am trying to display - it works correctly with the isset statement. Remove it and it doesn't. Sorry it is kinda long for a forum.

    <?php include('./includes/title.inc.php'); ?>
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset=utf-8">
    <title>Japan Journey
    <?php if (isset($title)) {echo "—{$title}";} ?>
    </title>
    <link href="styles/journey.css" rel="stylesheet" type="text/css" media="screen">
    </head>

    <body>
    <div id="header">
    <h1>Japan Journey</h1>
    </div>
    <div id="wrapper">
    <?php include('./includes/menu.inc.php'); ?>
    <div id="maincontent">
    <h2>A Journey through Japan with PHP</h2>
    <p>One of the benefits of using PHP is that you can extract common page elements such as the menu and footer, and turn them into server-side includes. Using PHP functions and conditional logic, you can automatically change the page title and indicate the current page in the menu. Generating a random image on the front page also adds interest to the site.</p>
    <div id="pictureWrapper"><img src="images/basin.jpg" alt="Water basin at Ryoanji temple" width="350" height="237" class="picBorder"></div>
    <p>Ut enim ad minim veniam, quis nostrud exercitation consectetur adipisicing elit. Velit esse cillum dolore ullamco laboris nisi in reprehenderit in voluptate. Mollit anim id est laborum. Sunt in culpa duis aute irure dolor excepteur sint occaecat.</p>
    <p>Eu fugiat nulla pariatur. Ut labore et dolore magna aliqua. Cupidatat non proident, quis nostrud exercitation ut enim ad minim veniam.</p>
    <p>Quis nostrud exercitation eu fugiat nulla pariatur. Ut labore et dolore magna aliqua. Sed do eiusmod tempor incididunt velit esse cillum dolore ullamco laboris nisi.</p>
    <p>Sed do eiusmod tempor incididunt ullamco laboris nisi consectetur adipisicing elit. Ut aliquip ex ea commodo consequat. Qui officia deserunt ut labore et dolore magna aliqua. Velit esse cillum dolore eu fugiat nulla pariatur. Sed do eiusmod tempor incididunt cupidatat non proident, sunt in culpa. Sunt in culpa duis aute irure dolor excepteur sint occaecat.</p>
    <p>Quis nostrud exercitation eu fugiat nulla pariatur. Ut labore et dolore magna aliqua. Sunt in culpa duis aute irure dolor excepteur sint occaecat.</p>
    </div>
    <?php include('./includes/footer.inc.php'); ?>
    </div>
    </body>
    </html>

    Thanks ahead again for any help.

    Jim

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,130
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    hmmm.... I don't see why the isset would be required with the code that you have. I don't get any errors or warnings when I ran it locally
    PHP Code:
    <?php
    $title 
    basename($_SERVER['SCRIPT_FILENAME'], '.php');
    $title str_replace('_'' '$title);
    if (
    $title == 'index') {
        
    $title 'home';
    }
    $title ucwords($title);
    ?>
    Now if your code was
    PHP Code:
    <?php
    $page 
    basename($_SERVER['SCRIPT_FILENAME'], '.php');
    $page str_replace('_'' '$page);
    if (
    $page == 'index') {
        
    $title 'home';
    }
    //$title = ucwords($title);
    ?>
    Then you would need isset because $title isn't defined unless you are on the index page and so the variable would not be set in all situations.

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    hmmm.... I don't see why the isset would be required with the code that you have. I don't get any errors or warnings when I ran it locally
    PHP Code:
    <?php
    $title 
    basename($_SERVER['SCRIPT_FILENAME'], '.php');
    $title str_replace('_'' '$title);
    if (
    $title == 'index') {
        
    $title 'home';
    }
    $title ucwords($title);
    ?>
    Now if your code was
    PHP Code:
    <?php
    $page 
    basename($_SERVER['SCRIPT_FILENAME'], '.php');
    $page str_replace('_'' '$page);
    if (
    $page == 'index') {
        
    $title 'home';
    }
    //$title = ucwords($title);
    ?>
    Then you would need isset because $title isn't defined unless you are on the index page and so the variable would not be set in all situations.

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the cool reply. When I comment out the isset on my server using the code that I showed you it doesn't work.
    I am going to try this on a different server and see if it makes a difference (I am not experienced enough to know how much difference this will make, but I might learn something.
    I like the second set of code and understand why isset would be needed. Thanks for that, it helps.

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,130
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    When you commented it out, did you leave it as
    PHP Code:
    <?php echo "- $title"?>

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    When you commented it out, did you leave it as
    PHP Code:
    <?php echo "- $title"?>

  9. #9
    SitePoint Enthusiast
    Join Date
    May 2012
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. The dash works fine....

  10. #10
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,833
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by jskintauy View Post
    Yes. The dash works fine...
    .
    The code you supplied is missing a double-quote:

    HTML Code:
    <!--  <meta charset=utf-8"> -->
    <meta charset="utf-8">
    Please explain what you mean when you say "...something doesn't work."

    Also please show the code that is working and also exactly how you commented out the isset statement.

    Also please include your code in block php tags (without the space which follows the leading square bracket):

    [ php]
    your code
    [ /php]
    Last edited by John_Betong; Aug 22, 2012 at 17:55. Reason: spelling: not my fortay
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •