College Student needs help resolving php/database problem!

homework
php

#1

Hi, so I’m new to PHP, and I’m taking a college class on it. However the instructor is not particularly helpful with his instructions on the assignment, and tends to give vague answers on how to find the solutions to issues we have.

Here is the basic problem. I have a database, and 2 seperate php pages. One is “index.php”. The other is “home.html.php”.

My problem, is the picture that I provided. I’ve tried coding this a few different ways, but for the life of me I am hung up on how to resolve the problem I’m facing. And since the instructor isn’t being helpful, I need someone to actually give me a helping hand in resolving this problem.

index.php: Supposed to connect to my xampp database, and pull rows from a certain table in the database.

<?php
try
{
  $pdo = new PDO('mysql:host=localhost;dbname=seanropp_pht_db', 'seanropp_pht_user', 'password');
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
  $error = 'Unable to connect to the database server.';
  include 'home/error.html.php';
  exit();
}

try
{
  $sql = 'SELECT title, description, image FROM content';
  $result = $pdo->query($sql);
}
catch (PDOException $e)
{
  $error = 'Error fetching page: ' . $e->getMessage();
  include 'home/error.html.php';
  exit();
}

while ($row = $result->fetch())
{
  $content[] = array(
      'title' => $row['title'],
      'description' => $row['description'],
      'image' => $row['image'],
  );
}

include 'home/home.html.php';

home.html.php: HTML page witht he “header”, “body”, “main”, etc. elements in it. PHP coding in here is the problem.

<!DOCTYPE html>

<html lang="en">
<head>
  <title>Portland Historical Tours</title>
  <meta name="description" content="Portland Historical Tours has offered three Portland Tour options for over 33 years: the Downtown Tour, the Landmarks Tour and the Growth Tour.">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href=project.css>
  <link href='https://fonts.googleapis.com/css?family=Cinzel' rel='stylesheet' type='text/css'>
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>
<body>
  <div id="wrapper">
      
    <?php include 'includes/header.inc.php'; ?>
      
    <?php include 'includes/navigation.inc.php'; ?>
      
    <main>
        
        <?php 
        foreach ($content as $contents): ?>
            <?php
                echo "<img src=" . "$contents" . "width='850' height='389'>";
                echo "<h1>" . "$contents" . "</h1>";
                echo "<p>" . "$contents" . "</p>";      
        ?>
        
       
<!-- TEMPORARY Block out, use for referance, may need to delete later

        <a href="tours.php"><img src="images/downtown.png" alt="Portland Historical Tours" width="850" height="389"></a>
        <h1>How did we get started?</h1>
        <p>
          We have such a passion for the city we live in. For over 33 years we have offered tours for Portland visitors and residents alike through our family business. Having family in Portland since the early 1900's has allowed us to build tours out of insight over 100 years. We are confident you will love any of our Portland tours with the help of our generous guides. We offer three tours: the Downtown, the Growth, and the Landmarks tour. Our favorites are the Downtown and Landmarks but with Portland's current growth we are selling out our Growth tours.<br><br>We hope to meet you soon! Let us know if you have any questions by visiting the <a href="contact.html">Contact page.</a><br><br>Thank you!
        </p>
-->        
      <!--
        <a href="tours.html"><img src="images/downtowntour.png" alt="Portland Historical Downtown Tour" width="850" height="481"></a>
        <div id="source"><p>(City of Portland Archives)</p></div>
      <a href="tours.html"><img src="images/growthtour.png" alt="Portland Historical Growth Tour" width="850" height="481"></a>
        <div id="source"><p>(City of Portland Archives)</p></div>
      <a href="tours.html"><img src="images/landmarkstour.png" alt="Portland Historical Landmarks Tour" width="850" height="481"></a>
        <div id="source"><p>(City of Portland Archives)</p></div>
        -->
      
        <?php endforeach; ?>
        
    </main>
      
    <?php include 'includes/footer.inc.php'; ?>
      
  </div>
</body>
</html>


#2

Can you navigate to the includes folder and verify the names of the files. I never used .php on my includes (in the past), only .inc. That’s why I ask. It’s not to object. So long as those files exist, we’re good.

The other thing would be the paths. Remember that a PHP page opens in its current directory. I would suggest moving home.html.php to the same directory as index.html, else modify the URI paths to the include files.

../includes/ ...

#3

Hi, so the paths to the include files all are correct. Most of the page loads correctly when I check my work, and that’s the portions from the include files. Unfortunately I have to keep the “home.html.php” file in the folder it’s in for the assignment.

I’m not sure if there’s something wrong with the index.php page and if its pulling from the database correctly, or if there is something wrong with the php code in the home.html.php, namely the portion that deals with a foreach loop and is supposed to echo the contents I want displayed, namely an image, h1 heading, and paragraph.

I think that the real problem is the :
<?php foreach ($content as $contents): ?>

I don’t think it’s actually pulling from the index page’s SELECT query. I’ve also tried to use echo htmlspecialchars($content, ENT_QUOTES, "UTF-8"); to try and get things to display, but have had no success there either.


#4

Not to get off track here, but… So I must assume that the includes directory is in the home folder?

When does $contents get defined?


#5

Yes, that would be correct. I have a home folder, and an includes folder. The includes folder contains php files like: header.inc.html.php, and navigation.inc.html.php.


#6

Back in the day I would have a templates folder that contained raw PHP pages or page fragments (such as yours). They were used site wide, not just for one page.

If you have never taken a course in HTML and CSS then you are hogtied at this point. PHP is first off, text, but second off, HTML, and third, declarative script. Without an understanding of HTML you have no mooring, just a dead weight to the bottom.

PHP stands for `Pre-Processed HTML. That means the content of the response page is dynamic and generated with each request from accumulated data server-side. The data can be a database, or text files or CSV, or HTML snippets. PHP doesn’t care.

Disconnect from the database for the time and ensure that the core HTML is being sent as a response. Design that content to be W3C valid (so generate it and validate at validator.w3.org).

When you get there, ping this topic. Then we will know we’re going in the same direction and I’m not just tossing turds.


#7

No worries, I do have an understanding of HTML & CSS. Alot of the files we’re using for this assignment were pre-provided by the instructor.

I’m pretty sure that the core HTML & CSS are in good order. The blocked out section of HTML in the home.html.php is what would normally be displayed for the page using just HTML & CSS. These blocked out sections are the ones that are in the database that the index.php is trying to pull the table from.

Maybe this would help, if I post the instructions he gave us.

  • Add a foreach loop php island below the opening <main> tag. This loop will display the 1 record generated by the SELECT query. That record contains 3 values, one for the image, one for the title <h1> and one for the paragraph <p>.

  • Using the record values generated by the SELECT query, replace the image src, h1 text and all the <p> text with the results generated by the while loop in the root index.php page.


#8

Validate, then be sure, otherwise fix it so it does validate. Structure is everything and order is the key with PHP. There are no mulligans, only white screens (response code 500).

If JavaScript doesn’t run, almost nobody knows it. If PHP doesn’t run, everybody knows.

Start by generating a valid page, then bring the db into play.


#9

I checked the html with: validator.w3.org. No errors were detected with it. I checked the include files and there were no errors with the html as well.


#10

So the response with no db is valid?

Please post the HTML that you validated.


#11
while ($row = $result->fetch())
{
  $content[] = array(
      'title' => $row['title'],
      'description' => $row['description'],
      'image' => $row['image'],
  );
}

$content[] ==> $content


Then you’re gonna want to include index.php within your home.html.php file.


This will retrieve data from the database. You’ll find that this won’t give you a perfect result, though.


Please ask your teacher about structure / separation of concerns / MVC or similar (eventually).