Steve_Berry Posted April 27, 2021 Share Posted April 27, 2021 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. There should be Home | other | etc... Quote Link to comment https://forums.phpfreaks.com/topic/312549-no-content-on-index-page/ Share on other sites More sharing options...
requinix Posted April 27, 2021 Share Posted April 27, 2021 Have you looked at the HTML source of the page? To confirm that the links really are not being outputted, as opposed to some CSS issue that's hiding them from you? 1 Quote Link to comment https://forums.phpfreaks.com/topic/312549-no-content-on-index-page/#findComment-1586181 Share on other sites More sharing options...
Steve_Berry Posted April 27, 2021 Author Share Posted April 27, 2021 Hello, yes and the link is missing: 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. No idea why I don't see the php version [<a href="index.php?id=<?php echo $row['id']; ?>"><?php echo $row['menuheader']; ?></a>]. Quote Link to comment https://forums.phpfreaks.com/topic/312549-no-content-on-index-page/#findComment-1586192 Share on other sites More sharing options...
Strider64 Posted April 27, 2021 Share Posted April 27, 2021 Do a echo "<pre>" . print_r($result, 1) . "</pre>"; die(); to see if you are getting any results. 1 Quote Link to comment https://forums.phpfreaks.com/topic/312549-no-content-on-index-page/#findComment-1586195 Share on other sites More sharing options...
Steve_Berry Posted April 28, 2021 Author Share Posted April 28, 2021 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> Quote Link to comment https://forums.phpfreaks.com/topic/312549-no-content-on-index-page/#findComment-1586206 Share on other sites More sharing options...
Barand Posted April 28, 2021 Share Posted April 28, 2021 1 Quote Link to comment https://forums.phpfreaks.com/topic/312549-no-content-on-index-page/#findComment-1586209 Share on other sites More sharing options...
Steve_Berry Posted April 28, 2021 Author Share Posted April 28, 2021 <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? Quote Link to comment https://forums.phpfreaks.com/topic/312549-no-content-on-index-page/#findComment-1586210 Share on other sites More sharing options...
Barand Posted April 28, 2021 Share Posted April 28, 2021 Do you still get that notice if you enter a valid id? Quote Link to comment https://forums.phpfreaks.com/topic/312549-no-content-on-index-page/#findComment-1586215 Share on other sites More sharing options...
Steve_Berry Posted April 28, 2021 Author Share Posted April 28, 2021 Yes. Quote Link to comment https://forums.phpfreaks.com/topic/312549-no-content-on-index-page/#findComment-1586220 Share on other sites More sharing options...
Barand Posted April 28, 2021 Share Posted April 28, 2021 Have you got PDO and PHP error reporting options set? Quote Link to comment https://forums.phpfreaks.com/topic/312549-no-content-on-index-page/#findComment-1586221 Share on other sites More sharing options...
Steve_Berry Posted April 28, 2021 Author Share Posted April 28, 2021 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: This is - or would be the menu for links to Home page etc. Not sure if this is helpful. Quote Link to comment https://forums.phpfreaks.com/topic/312549-no-content-on-index-page/#findComment-1586222 Share on other sites More sharing options...
mac_gyver Posted April 28, 2021 Share Posted April 28, 2021 (edited) 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 April 28, 2021 by mac_gyver 1 Quote Link to comment https://forums.phpfreaks.com/topic/312549-no-content-on-index-page/#findComment-1586227 Share on other sites More sharing options...
Barand Posted April 28, 2021 Share Posted April 28, 2021 I ran your code and it functions OK. The only way I can see that $result is not defined is if $_GET['id'] is empty or not an integer. But you say that you are inputting a valid id value so I'm out of guesses. Good luck. 1 Quote Link to comment https://forums.phpfreaks.com/topic/312549-no-content-on-index-page/#findComment-1586228 Share on other sites More sharing options...
Steve_Berry Posted April 29, 2021 Author Share Posted April 29, 2021 Thanks everyone. Will look into PDO further for more help. Quote Link to comment https://forums.phpfreaks.com/topic/312549-no-content-on-index-page/#findComment-1586232 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.