php in html

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
duranike
Forum Newbie
Posts: 17
Joined: Tue Nov 04, 2003 8:40 pm

php in html

Post by duranike »

how do u echo a value in the middle of a html file? what is the line of code? does the whole file need to be brackated by php files
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Code: Select all

<html>
<head>
<title>Some Title</title>
</head>
<body>
..more html here...
<b>Hello <?php echo $username ?></b>
...more html...
<i>Goodbye <?php echo $yellowbrickroad ?></i>
</body>
</html>
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

um ust also save that file as php, or it won't work :D
duranike
Forum Newbie
Posts: 17
Joined: Tue Nov 04, 2003 8:40 pm

hmm

Post by duranike »

doesnt seem to work. the field is just blank and nothing is there. how does the php value $username know to point to the sql field which stores its value. is this done in the general php login script or is it done at runtime in the php file internally?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Re: hmm

Post by markl999 »

duranike wrote:doesnt seem to work. the field is just blank and nothing is there. how does the php value $username know to point to the sql field which stores its value. is this done in the general php login script or is it done at runtime in the php file internally?
You'de generally do all you database work/whatever outside of the view/template/html. For example you might have a getuser.php file which does whatever database queries it needs to do and assigns the values, e.g $username. Then getuser.php might do an require_once 'user.php'; where user.php is of the format above...your html with php 'embedded'
duranike
Forum Newbie
Posts: 17
Joined: Tue Nov 04, 2003 8:40 pm

Post by duranike »

ok i understand so far but what actually calls or assigns the value in our html page?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

duranike wrote:ok i understand so far but what actually calls or assigns the value in our html page?
The PHP code you write assigns them.
Here's a simple example:
page1.php

Code: Select all

$username = 'Ford Prefect';
require_once 'page2.php';
page2.php

Code: Select all

echo 'Helloooooo '.$username;
How and where $username comes from is up to you, i've hard coded it, you may want to get it from a database. How you get it out of the database is out of the realms of this forum/post as it will be specific to your database setup etc..
duranike
Forum Newbie
Posts: 17
Joined: Tue Nov 04, 2003 8:40 pm

Post by duranike »

ok. i think i see where im going wrong. i guess the problem is that we have an html file that is very complicated. how can we use it as a php file? i think i have read that u can put a code like "{php} ..html... {/php}" ( { = [) and then u can work it as long as u rename it blahblah.php. any help or guidance? thanks for your time and patience. by the way, cool avatar.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Just rename it to whatever.php. As long as it has a .php extension it should, by default, be parsed ok using the standard php tags. That is <?php ... ?>
You can jump in and out of php mode whenever you want so the whole file doesn't need to be withing php tags..just the php bits.
So you can have..
lots of html here...
<?php some php bits here ?>
more html
<?php some more php ?>
etc..

{php} and {/php} sounds like template specifc tags, something like Smarty?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

[big_search]php introduction tutorial[/big_search]

Check out these links. :)
Post Reply