Page 1 of 1

php in html

Posted: Wed Nov 05, 2003 12:24 am
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

Posted: Wed Nov 05, 2003 12:28 am
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>

Posted: Wed Nov 05, 2003 12:37 am
by vigge89
um ust also save that file as php, or it won't work :D

hmm

Posted: Wed Nov 05, 2003 1:13 am
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?

Re: hmm

Posted: Wed Nov 05, 2003 1:17 am
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'

Posted: Wed Nov 05, 2003 1:23 am
by duranike
ok i understand so far but what actually calls or assigns the value in our html page?

Posted: Wed Nov 05, 2003 1:34 am
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..

Posted: Wed Nov 05, 2003 1:40 am
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.

Posted: Wed Nov 05, 2003 1:48 am
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?

Posted: Wed Nov 05, 2003 1:54 am
by m3mn0n
[big_search]php introduction tutorial[/big_search]

Check out these links. :)