Displaying PHP in HTML - Help!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jpcoombs
Forum Newbie
Posts: 6
Joined: Wed Dec 23, 2009 12:47 am

Displaying PHP in HTML - Help!

Post by jpcoombs »

I am trying to display information from an SQL database onto an HTML page. The goal is to display the Title and Article inside of a white space on an HTML page.

I copy/paste the code into an HTML document and it doesn't seem to work. How do I get the Title and Article from the following code to display properly?

Code: Select all

<?php
require_once('includes/DbConnector.php');
$connector = new DbConnector();
$result = $connector->query('SELECT title,thearticle FROM cmsarticles WHERE ID = 1');
$row = $connector->fetchArray($result);
?>
 
<?php echo $row['title'];?>
 
<?php echo $row['thearticle'];?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Displaying PHP in HTML - Help!

Post by requinix »

jpcoombs wrote:How do I get the Title and Article from the following code to display properly?
Depends what "properly" means, and how that's different from what it's doing now.
jpcoombs
Forum Newbie
Posts: 6
Joined: Wed Dec 23, 2009 12:47 am

Re: Displaying PHP in HTML - Help!

Post by jpcoombs »

Well nothing is happening when I paste that exact code into HTML. It should be taking two data points, title and thearticle, and displaying them. But all I get is the a part of the code displayed on the HTML - as if its not reading the <?php ?>. Here is the result when its pasted verbatum on a website:

query('SELECT title,thearticle FROM cmsarticles WHERE ID = 1'); ?>

And thats it.
jpcoombs
Forum Newbie
Posts: 6
Joined: Wed Dec 23, 2009 12:47 am

Re: Displaying PHP in HTML - Help!

Post by jpcoombs »

User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Displaying PHP in HTML - Help!

Post by manohoo »

Let's take a look at the contents of $row. Please do this and post the result:

echo "<pre>";
var_dump($row);
jpcoombs
Forum Newbie
Posts: 6
Joined: Wed Dec 23, 2009 12:47 am

Re: Displaying PHP in HTML - Help!

Post by jpcoombs »

User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Displaying PHP in HTML - Help!

Post by manohoo »

I noticed a fundamental flaw in your code.

You can embed html in a php file. Example:
file: index.php

Code: Select all

 
<html>
<head>
</head>
<body>
<?php echo "hello there!"; ?>
</body>
</html>
 
the above will output:
hello there!

But, you can't embed PHP in an html file:
file: index.html

Code: Select all

 
<html>
<head>
</head>
<body>
<?php echo "hello there!"; ?>
</body>
</html>
 
The above will not output anything.
Last edited by manohoo on Wed Dec 23, 2009 5:03 pm, edited 1 time in total.
jpcoombs
Forum Newbie
Posts: 6
Joined: Wed Dec 23, 2009 12:47 am

Re: Displaying PHP in HTML - Help!

Post by jpcoombs »

Is there a solution? My goal is to embed updates into HTML so that clients can make changes to only single areas of the site through PHP - then the HTML is updated.
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Displaying PHP in HTML - Help!

Post by manohoo »

For starters you need to use only PHP files.

zip your files and send to my email (manoloDOTwebmaster@gmailDOTcom), I'll take a closer look. And please try to explain as well as you can what you are trying to accomplish.
jpcoombs
Forum Newbie
Posts: 6
Joined: Wed Dec 23, 2009 12:47 am

Re: Displaying PHP in HTML - Help!

Post by jpcoombs »

Manohoo - thanks for all your feedback. It got me thinking - im just gonna develop this whole site using PHP and build it right rather than jury rigging existing code. Good challenge anyway. But you have been a big help - thanks!
Post Reply