Jump to content

No content on index page


Steve_Berry

Recommended Posts

Hello.  I am attempting to display content from a database to a web page.  I have SELECT * etc to do this, plus the menu that should be displayed has links to pages.  This would display text to the page depending on the link, for this to work I use the id of the page ($Get['id']).  Following advice (not code), I have attempted the following:

 

<!DOCTYPE html>
<html>

  <?php include 'includes/headsection.php'; ?>

  <body>

    <?php

      // query database
      if (!empty($_GET['id']) && (intval($_GET['id']) == $_GET['id'])) {
      	$pdo_statement = $conn->prepare("SELECT * FROM pages WHERE id=:id ORDER BY id ASC");
      	$pdo_statement->execute(['id' => $_GET['id']]);
      	$result = $pdo_statement->fetchAll();
      }

    ?>

    <!-- topMenu -->
    <table id="topMenu">

      <tr>
        <td><h1 class="siteName">Site name</h1></td>

        <td>
          <?php
          if(!empty($result)) {
            foreach($result as $row) {
          ?>
          <a href="index.php?id=<?php echo $row['id']; ?>"><?php echo $row['menuheader'];  ?></a>

          <?php }
          }
        ?>

        </td>

      </tr>

    </table>

This issue is that the top menu (home, etc), does not appear, and there are no errors to suggest what I have done wrong.  Any help will be appreciated. I include a picture of what I mean.1600064702_emptymenu.thumb.jpg.f75cb752f6624649df6c47c6ef7a7ff3.jpg

There should be Home | other | etc...

 

Link to comment
Share on other sites

Hello, yes and the link is missing:

1716027376_missingcode.jpg.94ece03367d4d09ca1f3451658902b6d.jpg

I don't see anything in the CSS that would cause this to happen.  As a test I added HTML code to see the link.

addedcode.jpg.f02b285bf5edc0c8b4bed8f4ce5c7d96.jpg

No idea why I don't see the php version [<a href="index.php?id=<?php echo $row['id']; ?>"><?php echo $row['menuheader']; ?></a>].

Link to comment
Share on other sites

Hi,  Assuming I placed the code you provided in the correct place, I still do not see anything either in the source code - blank space where the link should be, or on the page itself.

Just to make sure, is the following correct?

  	<td class="navItem">
          <?php
          if(!empty($result)) {
            foreach($result as $row) {

              echo "<pre>" . print_r($result, 1) . "</pre>";
              die();
          ?>
          <a href="index.php?id=<?php echo $row['id']; ?>"><?php echo $row['menuheader'];  ?>echo</a>

          <?php }
          }
        ?>

     </td>

 

Link to comment
Share on other sites

  <td class="navItem">
          <?php
          
          echo "<pre>" . print_r($result, 1) . "</pre>";

          if(!empty($result)) {
            foreach($result as $row) {
          ?>
          <a href="index.php?id=<?php echo $row['id']; ?>"><?php echo $row['menuheader']; ?>echo</a>

          <?php }
          }
        ?>

        </td>

This now gives me an error: Notice: Undefined variable: result in C:\xampp\htdocs\testsite\index.php on line 33.  I assume this means is not set.  How do I remove this error?

Link to comment
Share on other sites

I assume you mean the following:

try {
  $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
 
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  //echo "Connected successfully";
} catch(PDOException $e) {
  echo "Connection failed: " . $e->getMessage();
}

As a point of interest I changed -

echo "<pre>" . print_r($result, 1) . "</pre>";

To -

echo "<pre>" . print_r($conn, 1) . "</pre>";

To which I got the following:

pdo.jpg.4ab07b3ef048891b5055fa580ef8e87c.jpg

This is - or would be the menu for links to Home page etc.

Not sure if this is helpful.

 

Link to comment
Share on other sites

the sql query you are showing and the output you are (trying) to produce from that query make no sense. if this was working, you are querying for the row of data WHERE the id column matches the $_GET['id'] value and looping to produce a (one) link with ?id=$row['id'] in it, i.e. a link containing the same id that was in $_GET['id'].

you need to step back and come up with a stateable definition of what your code needs to do. you are doing two things, 1) producing navigation links, and 2) when a link has been clicked, you are displaying the content that corresponds to the id in the clicked link.

to do item #1, you would query to get ALL the page ids, and page names/titles, which i assume are in the menuheader column (if you list out the columns you are selecting in a query, rather than using *, it helps make your code/query self-documenting.) you would then test and loop over the result from this query to produce navigation links. Note: almost every if() conditional  test needs an else so that code does something when the main condition fails. for navigation link data, if there is no data, rather than outputting nothing, you should output a 'sorry, nothing to display' or similar message.

to do item #2, you would test for a $_GET['id'] value and use it to query to get the matching row of data, fetch that single row of data (no loop needed), and if there was a matching row of data, output the content for the page. if there was no matching  row of data, you would instead output a 'nothing to display' or similar message.

Edited by mac_gyver
  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.