Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

James Barrett
James Barrett
13,253 Points

How to display <h3> dynamically using PHP?

Hi there,

I am working on a project of my own and I wish to insert a h3 below my h1 depending on what page the user is on. I understand this can be accomplished using the $_GET variable but I do not know where to start. For example I want a H3 to be displayed if the user is on the index.php page blog.php page. Any help would be great!

(inc/header.php):

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="Portfolio Website For Jackson Jacob. Software Engineer, Designer & Developer, Creative Management & Software.">
    <meta name="author" content="Jackson Jacob">
    <title><?php echo $pageTitle ?></title>
    <!-- Bootstrap Core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom CSS -->
    <link href="css/landing-page.css" rel="stylesheet">
    <!-- Custom Fonts -->
    <link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="http://fonts.googleapis.com/css?family=Lato:300,400,700,300italic,400italic,700italic" rel="stylesheet" type="text/css">
</head>

<body>
    <!-- Navigation -->
    <nav class="navbar navbar-default navbar-fixed-top topnav">
        <div class="container topnav">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand topnav" href="index.php">INSERT JACKSON PIC HERE</a>
            </div>
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="about.php">About</a>
                    </li>
                    <li>
                        <a href="work.php">Work</a>
                    </li>
                    <li>
                        <a href="blog.php">Blog</a>
                    </li>
                    <li>
                        <a href="contactpage.php">Contact</a>
                    </li>
                </ul>
            </div>
            <!-- /.navbar-collapse -->
        </div>
        <!-- /.container -->
    </nav>
    <!-- Header -->
    <div class="contact-test-intro-header">
        <div class="container">
            <div class="row">
                <div class="col-lg-12">
                    <div class="intro-message">
                        <h1><?php echo $pageHeading ?></h1>
                        <!-- Where I want the H3 subheading to be displayed! -->
                        <hr class="intro-divider">
                        <ul class="list-inline intro-social-buttons">
                            <li>
                                <a href="https://instagram.com/jackson3195" class="btn btn-default btn-lg"><i class="fa fa-instagram fa-fw"></i> <span class="network-name">Instagram</span></a>
                            </li>
                            <li>
                                <a href="https://github.com/jackson3195" class="btn btn-default btn-lg"><i class="fa fa-github fa-fw"></i> <span class="network-name">Github</span></a>
                            </li>
                            <li>
                                <a href="https://www.linkedin.com/profile/view?id=AAkAABtkhjsBFhFLyos_ON-Zp8chbhRd7omS92g&amp;authType=NAME_SEARCH&amp;authToken=mSum&amp;locale=en_US&amp;trk=tyah&amp;trkInfo=clickedVertical%3Amynetwork%2CclickedEntityId%3A459572795%2CauthType%3ANAME_SEARCH%2Cidx%3A1-1-1%2CtarId%3A1447347287723%2Ctas%3Ajackson" class="btn btn-default btn-lg"><i class="fa fa-linkedin fa-fw"></i> <span class="network-name">Linkedin</span></a>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
        <!-- /.container -->
    </div>
    <!-- /.intro-header -->

(inc/index.php):

<?php
$pageTitle = "Jackson Jacob | Java Developer, Web Design & Development";
$pageHeading = "Jackson Jacob";
include("inc/header.php");
?>

Thanks! James.

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey James,

I think you might be over-complicating it. Why not just use a variable as your are with $pageHeading inside the <h1>. The variable can be placed on each page you want the <h3>.

Wouldn't this work?

:)

James Barrett
James Barrett
13,253 Points

I tend to over-complicate things sometimes... Thanks for your help

James Barrett
James Barrett
13,253 Points

Jason, I need to store the list items under the

<h1><?php echo $pageHeading ?></h1>

in an array so I can display different values across different pages, I am unsure on where to start, would you be able to help me?

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey James Barrett,

Sorry I didn't get back sooner. I was away from my computer for most of the day.

For the array, you could make a file called data.php and put the array in there. Each page that needs / uses the items in the array would need the include() method to include (and thus be able to access) the array.

Depending on how many items you need to store and what kind of data you are storing will determine how you want to construct your array.

If it's a simple array with only a few items the best way would be

$array_name = ("item number 1", "item number 2", "item number 3", "more items to come");

Alena covers this way of assigning and using an array in this video and how to add items in this one

If it is a more complex array that you need, I would use an Associative Array. You start by declaring an empty array:

$array_name = [];

and then add the information need:

$array_name[101] = [  // number could be anything that is relevant (i.e. a UPC or item number)
  "key1" => "Value for the first key",
  "key2" => "Value for the second key",
  "key3" => "Keep going for all the data"
];

Alena coves these in this video and show how to display the items in this one.

I hope all this helps you. Anything else ... just let me know. Happy Coding! :)