12 Replies - 1242 Views - Last Post: 01 February 2011 - 09:05 AM Rate Topic: -----

#1 Sutirth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-January 11

Merging a php code into A template

Posted 01 February 2011 - 06:45 AM

Guys i m using a template for creating a website .Well i m stucked at a point where i want to display user whole profile onto the website.So shd i write a php file separately or shd i write in the html template code itself.
If anybody can help me and provide some links for this type of e.g it will be very helpful indeed
Is This A Good Question/Topic? 0
  • +

Replies To: Merging a php code into A template

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: Merging a php code into A template

Posted 01 February 2011 - 06:48 AM

Always separate your PHP and HTML. Then use an include statement to output the HTML.
Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2914
  • View blog
  • Posts: 10,093
  • Joined: 08-August 08

Re: Merging a php code into A template

Posted 01 February 2011 - 06:49 AM

Write the html in a separate file, then have the php read it, modify it where necessary and output it. Mixing html and php in one file is asking for trouble.
http://www.dreaminco...ode-separation/
Was This Post Helpful? 1
  • +
  • -

#4 Creecher  Icon User is offline

  • I don't care
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,049
  • Joined: 06-March 10

Re: Merging a php code into A template

Posted 01 February 2011 - 06:53 AM

You can create your own function in a different PHP file, or in the same file you want to show the profile in.

<?php
function ShowProfile()
{
	echo'<p>Name: $name</p>
	<p>Age: $age</p>
	blah blah
	Profile picture:<img src="$image">';
}
?>


Then call it in like so:
<body>
<?php
ShowProfile(); 
?>
</body>




Homemade functions are your friend :)
Was This Post Helpful? -1
  • +
  • -

#5 Sutirth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-January 11

Re: Merging a php code into A template

Posted 01 February 2011 - 06:56 AM

K i got it its best to separate both the files agreed one hurdle gone :P.But the problem persist after writing the php file how shd i call the file i m still a bit unclear abt it :(

This post has been edited by Sutirth: 01 February 2011 - 06:57 AM

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: Merging a php code into A template

Posted 01 February 2011 - 06:57 AM

@: Really not good practice to embed the PHP in your HTML like that.

@: You can use the file_get_contents() function to read in the HTML, or an include statement if you don't need to modify the HTML.
Was This Post Helpful? 1
  • +
  • -

#7 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2914
  • View blog
  • Posts: 10,093
  • Joined: 08-August 08

Re: Merging a php code into A template

Posted 01 February 2011 - 07:08 AM

Mixing languages is almost never a good idea. I mean, sie können Sprachen alle mischen, die Sie mögen, but who's going to read it? Probably not even you after you've been away from it for a while.
Was This Post Helpful? 1
  • +
  • -

#8 Sutirth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-January 11

Re: Merging a php code into A template

Posted 01 February 2011 - 07:18 AM

Hmm so i guess when i m logging in with a member on a website using "session" and when the login is successful i can directly link the user to its user profile page by using header(userpage) in login script and while in html page of userpage i just need to include the php file and wolaa it will work ryte?? and m i correct in this thinkin process???
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10447
  • View blog
  • Posts: 38,690
  • Joined: 27-December 08

Re: Merging a php code into A template

Posted 01 February 2011 - 07:22 AM

You shouldn't be including the PHP in the HTML. Quite the opposite. Treat the PHP as the code you are working with, and the HTML as plaintext to output.
Was This Post Helpful? 1
  • +
  • -

#10 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2914
  • View blog
  • Posts: 10,093
  • Joined: 08-August 08

Re: Merging a php code into A template

Posted 01 February 2011 - 07:33 AM

This is why I think beginners should learn another language like C/C++ before starting with php. Those languages don't let you mix output with code, so they require you to do things the proper way. PHP gives you plenty of rope to hang yourself with.
Was This Post Helpful? 1
  • +
  • -

#11 Sutirth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 16-January 11

Re: Merging a php code into A template

Posted 01 February 2011 - 08:09 AM

Hmm got it i will make 2 separate files for sure but just wanted to know how to link them so as to my output will be visible on the template that i m using

@Ctphpnwb already done with C and C++ and Java wanna get a good hand on php so learning it :P
Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2914
  • View blog
  • Posts: 10,093
  • Joined: 08-August 08

Re: Merging a php code into A template

Posted 01 February 2011 - 08:33 AM

Honestly, I don't know what you mean by template. From a php perspective every html file is a template.

As for linking them, you don't, at least not in the sense that you do with C/C++. HTML is simply data to be manipulated, like any text file. Read it in, make your changes, and then send it to the browser. If you don't need to make changes to it, send it to the browser directly by using an include.

Code separation is important whether if you're using MVC or not. MVC requires it, but all code needs it.
Was This Post Helpful? 0
  • +
  • -

#13 TMKCodes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 48
  • View blog
  • Posts: 440
  • Joined: 21-March 09

Re: Merging a php code into A template

Posted 01 February 2011 - 09:05 AM

Myself i prefer something like this.

<?php
require_once("template.php");
$template = new template("template.html");
$template->set_tag("tag", "whatever i want here!"); // Replaces {tag} from the html template file.
$template->set_tag("tag2", "More stuff in another place!");
$template->replace_tags();
$template->output();
?>


This post has been edited by TMKCodes: 01 February 2011 - 09:07 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1