i was wondering how to how seprate php from html while fetching rows from database..
.
.

<?while($row=mysql_fetch_array($result))
{?>
<td><?$row['firstname']?></td>
<td><?$row['lastname']?></td>
<?}?>

can i separate php and html in above case.>?
.
.
thank u

how do you want to seperate it??

First of all: if you want to display text through php in the page you use either print() or echo()....

The code:

<table>
<?php
while ($row = mysql_fetch_array($result)) {
?>
<tr><td><?php echo $row['name']; ?></td></tr>
<tr><td><?php echo $row['description']; ?></td></tr>
<?php
}
?>
</table>

i just want to separate php code from html..
i have seen this in many open source project.. where html is other file and php code in another..! even at the time of listing.

You do know that php is a background program the decides what should be shown in HTML and handles off forms, and does the actions that are appropiate to the given information. You can do that a html file form has a action on a php script, that it handles the form and then redirects the user to another HTML file (such as "thankyou.html")

You gotta have some php in the html, I believe that you want to minimise this?

How about - at the top of the page, above all html or in another page entirely but included in you html page:


<?php
$myoutput = '';
while ($row = mysql_fetch_array($result)) {
$myoutput .= '<tr><td>' . $row . '</td></tr>';
$myoutput .= '<tr><td>' . $row . '</td></tr>';
}
?>


Then, in your html:

<table>
<?php echo $myoutput; ?>
<\table>


That's about as separate as you're gonna get I think.

You gotta have some php in the html, I believe that you want to minimise this?

How about - at the top of the page, above all html or in another page entirely but included in you html page:


<?php
$myoutput = '';
while ($row = mysql_fetch_array($result)) {
$myoutput .= '<tr><td>' . $row . '</td></tr>';
$myoutput .= '<tr><td>' . $row . '</td></tr>';
}
?>

Then, in your html:

<table>
<?php echo $myoutput; ?>
<\table>


That's about as separate as you're gonna get I think.

hmmmm nice Idea.. u got the point :)
thanx

You need to use a template class that will allow for repeatable template sections. That is the proper way to seperate php and html.

i was wondering how to how seprate php from html while fetching rows from database..

<?while($row=mysql_fetch_array($result))
{?>
<td><?$row['firstname']?></td>
<td><?$row['lastname']?></td>
<?}?>

can i separate php and html in above case.>?
thank u

If I was coding this it would look like the following:

<?
while($row=mysql_fetch_assoc($result))
{
echo '<td>'.$row['firstname'].'</td>';
echo '<td>'.$row['lastname'].'</td>';
}
?>

Hope that helps you understand.

PHP has built-in constructs that make it look like a templating language so you don't need the bloat of another templating class. I also prefer this method as people dealing with the files don't have to know what braces mean but its obvious what endwhile means.

<?php while($row=mysql_fetch_array($result)): ?>
  <td><?php echo $row['firstname'] ?></td>
  <td><?php echo $row['lastname']?></td>
<?php endwhile ?>

i just want to separate php code from html..
i have seen this in many open source project.. where html is other file and php code in another..! even at the time of listing.

From my understanding of this question, I think you're looking for includes.

Try

<?php include("mydirectory/myfile.php"); ?>

or

<?php require_once("mydirectory/myfile.php"); ?>

This will allow you to include an external PHP file that contains code you reuse so that you don't clutter your other files.

<td><?$row['firstname']?></td>
<td><?$row['lastname']?></td>

As Graphix mentioned, you need to echo() or print() your output for it to appear. There's a shorthand way to do this, which may be what you were going for, but if so you forgot one important character, the equals (=) sign after the "<?":

<td><?= $row['firstname']; ?></td> <td><?= $row['lastname']; ?></td>[code=PHP]<td><?= $row; ?></td>
<td><?= $row; ?></td>

Member Avatar for diafol
<td><?= $row['firstname']; ?></td>
<td><?= $row['lastname']; ?></td>

I got flamed for suggesting short tags before, they do save using some 7 characters every time you want to use echo.

I got flamed for suggesting short tags before, they do save using some 7 characters every time you want to use echo.

Really? How come? Is there something wrong with them?
I'm a super lazy typer so I take any opportunity to use shorthand =P

PHP has built-in constructs that make it look like a templating language so you don't need the bloat of another templating class. I also prefer this method as people dealing with the files don't have to know what braces mean but its obvious what endwhile means.

<?php while($row=mysql_fetch_array($result)): ?>
  <td><?php echo $row['firstname'] ?></td>
  <td><?php echo $row['lastname']?></td>
<?php endwhile ?>

Sure that sometimes works ShawnCplus however you cannot have a loop inside a loop using that method. An example of invalid code is as follows:

<?php $i=0
while ($i<3):
    $i++;
    while($row=mysql_fetch_array($result)): ?>
      <td><?php echo $row['firstname'] ?></td>
      <td><?php echo $row['lastname']?></td>
    <?php endwhile
endwhile ?>

The above code will never work because you cannot have a while loop inside a while loop when not using brackets. So best practise is to use the brackets.

Sure that sometimes works ShawnCplus however you cannot have a loop inside a loop using that method. An example of invalid code is as follows:

<?php $i=0
while ($i<3):
    $i++;
    while($row=mysql_fetch_array($result)): ?>
      <td><?php echo $row['firstname'] ?></td>
      <td><?php echo $row['lastname']?></td>
    <?php endwhile
endwhile ?>

The above code will never work because you cannot have a while loop inside a while loop when not using brackets. So best practise is to use the brackets.

You most certainly can use nested whiles with that syntax

This worked perfectly fine for me

<?php $i = $k = 0 ?>
<?php while ($i++ < 10): ?>
  Hello
  <?php while ($k++ < 3): ?>
    World
  <?php endwhile; $k = 0; ?>
<?php endwhile ?>
Member Avatar for diafol
<?php $i = $k = 0 ?>
<?php while ($i++ < 10): ?>
  Hello
  <?php while ($k++ < 3): ?>
    World
  <?php endwhile; $k = 0; ?>
<?php endwhile ?>

Sorry to be picky, but this looks a lot more complicated than braces.

Sorry to be picky, but this looks a lot more complicated than braces.

More verbose, yes. More complicated, no. If you go to a designer who has never used PHP and has never needed to balance braces and you ask them what a brace is or how to align braces they'll look at you dumbfounded. If you ask them what while/endwhile means if they have two braincells in their head they'll say endwhile ends a while.

To use the example from above

Short Tags

<?php $i = 0 ?>
<?php while ($i++ < 3): ?>
    <?php while($row=mysql_fetch_array($result)): ?>
      <td><?php echo $row['firstname'] ?></td>
      <td><?php echo $row['lastname']?></td>
    <?php endwhile ?>
<?php endwhile ?>

Braces

<?php $i = 0;
while ($++ < 3) {
    while ($row = mysql_fetch_array($result)) {
?>
    <td><?php echo $row['firstname'] ?></td>
    <td><?php echo $row['lastname']?></td>
<?php
    }
}
?>
Member Avatar for diafol

Each to his own I suppose, but I reckon anyone who could understand while ($i++ < 3): should be able to understand the idea of a brace.
Still, I ain't arguing the point, just not convinced.

Each to his own I suppose, but I reckon anyone who could understand while ($i++ < 3): should be able to understand the idea of a brace.
Still, I ain't arguing the point, just not convinced.

*nod* Maybe I've just met a lot of dumb designers. I think the end point we can get from this is to just use PHP instead of bloated, even uglier templating engine :)

Each to his own I suppose, but I reckon anyone who could understand while ($i++ < 3): should be able to understand the idea of a brace.
Still, I ain't arguing the point, just not convinced.

I agree but in my belief, you should only ever have one php opening tag and one php closing tag in a php file. Then all html output should be done through php functions because otherwise it gets too confusing. And it's easier to manage that way.

Member Avatar for diafol

Apologies to sam023: you asked a strightforward question and ended up having to wade through interaction from some of us old codgers! Poor beggar!

IMO: unless you use a templating system, mixing html and php will be almost inevitable if you're just starting out.

I find that I'm still learning good practice and doubtless I'll find better ways of doing the same thing in the future. I suppose it's one of the things a self-taught coder has to endure. Currently I am siding with cwarn where output is dealt entirely within php functions, although I still use 'proper html' for the DTD, head and body tags. Making use of "\n" and "\t" within php helps to beautify the eventual html output.

I agree but in my belief, you should only ever have one php opening tag and one php closing tag in a php file. Then all html output should be done through php functions because otherwise it gets too confusing. And it's easier to manage that way.

Wow, we definitely have complete opposite thoughts on it. I've dealt with some absolutely horrible spaghetti code that was created by having PHP echo all of the HTML. Not only do very few editors highlight HTML when they're inside of strings but if you're just using strings and heredoc and you still want to maintain good indentation and readability of the HTML then you might as well just exit PHP and write it directly.

<?php
  echo "<h1>Some Page Title</h1>\n";
  $some_variable = "Hello World!";
  while ($blahity_blah) {
    // some other logic we don't care about
  }
  // do some more random stuff
  echo '<h3>' . $some_variable . "</h3>\n";
  for (/* more random stuff */) {
    // blah
  }
  echo '<table>
  <tr><td>Hello!</td></tr>
  <tr>
    <td>World!</td>
    <td>Goodbye</td>
    <td>World</td>
  </tr>
</table>';
?>

Vs

<h1>Some Page Title</h1>
<?php
  $some_variable = "Hello World!";
  while ($blahity_blah) {
    // some other logic we don't care about
  }
  // do some more random stuff
?>
<h3><?php echo $some_variable ?></h3>

<?php
  for (/* more random stuff */) {
    // blah
  }
?>
<table>
  <tr><td>Hello!</td></tr>
  <tr>
    <td>World!</td>
    <td>Goodbye</td>
    <td>World</td>
  </tr>
</table>

The whole point of it is to separate display logic (HTML) from control logic (PHP). In my opinion you should almost never ie., only when absolutely positively necessary have PHP output HTML.

Wow, we definitely have complete opposite thoughts on it. I've dealt with some absolutely horrible spaghetti code that was created by having PHP echo all of the HTML. Not only do very few editors highlight HTML when they're inside of strings but if you're just using strings and heredoc and you still want to maintain good indentation and readability of the HTML then you might as well just exit PHP and write it directly.

<?php
  echo "<h1>Some Page Title</h1>\n";
  $some_variable = "Hello World!";
  while ($blahity_blah) {
    // some other logic we don't care about
  }
  // do some more random stuff
  echo '<h3>' . $some_variable . "</h3>\n";
  for (/* more random stuff */) {
    // blah
  }
  echo '<table>
  <tr><td>Hello!</td></tr>
  <tr>
    <td>World!</td>
    <td>Goodbye</td>
    <td>World</td>
  </tr>
</table>';
?>

Vs

<h1>Some Page Title</h1>
<?php
  $some_variable = "Hello World!";
  while ($blahity_blah) {
    // some other logic we don't care about
  }
  // do some more random stuff
?>
<h3><?php echo $some_variable ?></h3>

<?php
  for (/* more random stuff */) {
    // blah
  }
?>
<table>
  <tr><td>Hello!</td></tr>
  <tr>
    <td>World!</td>
    <td>Goodbye</td>
    <td>World</td>
  </tr>
</table>

The whole point of it is to separate display logic (HTML) from control logic (PHP). In my opinion you should almost never ie., only when absolutely positively necessary have PHP output HTML.

If you want perfect html and perfect php using my method then I would use something like the following:

<?php
//pre define tab indents
$t1=' ';
$t2='  ';
$t3='   ';
$t4='    ';
$t5='     ';
$t6='      ';
$t7='       ';
$t8='        ';
$t9='         ';
$t10='          ';
$t11='           ';
$t12='            ';
$t13='             ';
$t14='              ';
$t15='               ';
$t16='                ';
$nl="\n"; //new line variable

//now for the code

echo '<h1>Some Page Title</h1>'.$nl;
$some_variable = 'Hello World!';
while ($blahity_blah) {
  // some other logic we don't care about
}
// do some more random stuff
echo '<h3>' . $some_variable . '</h3>'.$nl;
for (/* more random stuff */) {
  // blah
}
echo '<table>'.$nl;
echo $t4'<tr>'.$nl;
echo $t8.'<td>Hello!</td>'.$nl;
echo $t4.'</tr>'.$nl;
echo $t4.'<tr>'.$nl;
echo $t8.'<td>World!</td>'.$nl;
echo $t8.'<td>Goodbye</td>'.$nl;
echo $t8.'<td>World</td>'.$nl;
echo $t4.'</tr>'.$nl;
</table>';
?>
Member Avatar for diafol

Urgh, I don't think I like that. '\t' will do for me.

I like that idea of sticking all the new line and tab stuff into variables, I wish I'd thought of that ages ago - I like to make my html output easily readable so my code is littered with "\n" and "\t" which makes my PHP code a bit messier than I would like. This will tidy it up a treat, I shall be using it for sure.

If you want perfect html and perfect php using my method then I would use something like the following:

<?php
//pre define tab indents
$t1=' ';
$t2='  ';
$t3='   ';
$t4='    ';
$t5='     ';
$t6='      ';
$t7='       ';
$t8='        ';
$t9='         ';
$t10='          ';
$t11='           ';
$t12='            ';
$t13='             ';
$t14='              ';
$t15='               ';
$t16='                ';
$nl="\n"; //new line variable

I don't even know how to respond to that.... just... wow.

I don't even know how to respond to that.... just... wow.

I was thinking the same thing.

As for the question, I feel that none of these responses really answer the question "how to separate php with html". To truly do it, you need a template engine. This does not mean go and download smarty or anything, just a simple class the replaces variables in html and maybe allows for repeatable sections.

After my experience of working with multiple designers, I found that its best to keep them as far away from my code as possible. They always find a way to break it. This is the reason I adopted the template system.

In the end, its a matter of preference really. If you don't mind having some html in the middle of your php, then use it as its not worth the overhead of SOME template engines.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.