Including PHP in HTML

Discussion in 'Programming' started by Dormage, Jun 19, 2009.

  1. #1
    Hello,
    I'm trying to include a php code into my simple HTML and i'm not having any luck.
    Its a simple table of two cell's the first cell is a simple html string,
    the second one is the problematic one, i need it to be an int value that increses by 1 evry second.
    I know php has some tags that help controll the time flow like "int sleep(int)"
    but i'm too new to php to even know how to include a simple php code that would do this and print the value in the second cell evry time i refresh my page.
    the HTML part looks somthing like this.
    <body>
    <table width="400">
    <td>String</td>
    <td>Here is where the int value should display</td>
    </body>
    
    HTML:
    And my PHP code is somthing like this but it most likely isn't working at all cause i never done this be4!
    
    <?php
    $a=1;
    while(true){
    sleep(1);
    $a=$a+1;
    echo $a;
    }
    ?>
    
    PHP:
    Now i dont know wether im suposed to put this code directly into the HTML tag that declares my cell or i should somehow let HTML compiler know that he has to compile some PHP code as well as HTML:confused:

    Many of u mighit find many of my words stupid and for that i apologise but that is the reason im asking for help :)
    I thank evry1 in advance.:p
     
    Dormage, Jun 19, 2009 IP
  2. fdfandrade

    fdfandrade Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hello,

    Just put your php code inside your TD tag.

    
    <body>
    <table width="400">
    <td>String</td>
    <td><?php $a=1;
    while(true){
    sleep(1);
    $a=$a+1;
    echo $a; } ?></td>
    </body>
    
    HTML:
    This should work fine :)

    ps: your main file must have the extension .php

    Best Regards,
    Francisco
     
    fdfandrade, Jun 19, 2009 IP
  3. Dormage

    Dormage Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well i did what u suggested but it didnt work.
    My main Index.html is composed out of 2 html documents each in its own frame.
    so what i did is edit that html page that is suposed to show the table to what you suggested and i changed the extention as well.
    What i got when i opened the page is a msgbox asking me if i want do download the php , and the table html was not displayed at all.
    if i leave the extention .html it simply dosent work.
    If i change Index.html to index.php it asks does the same msgbox that asks me to download the .php doc

    additional question, is the php code i wrote actualy doing what it suposed to do?

    ty very much for the help so far
     
    Dormage, Jun 19, 2009 IP
  4. Louis11

    Louis11 Active Member

    Messages:
    783
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    70
    #4
    Make sure you're saving your files with the .php extension instead of the .html extension (i.e. Index.php, not index.html). Otherwise your PHP will just appear as text. You can put HTML into a PHP file, but you can't put PHP into an HTML file :)
     
    Louis11, Jun 19, 2009 IP
  5. Dormage

    Dormage Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    So what ur saying is that all my web page files should be with extention .php? and in them the code is a bit HTML and a bit PHP?
     
    Dormage, Jun 19, 2009 IP
  6. nishanair

    nishanair Active Member

    Messages:
    188
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #6
    it was bad to mix PHP and HTML in the same file.

     
    nishanair, Jun 19, 2009 IP
  7. Dormage

    Dormage Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Tnx for the help i got it to work now :)
    But it isnt doing what i want it to do..
    When i refresh my page the page is white and dosent appear until the php code is finished calculating my variable $a after a few seconds it prints an enormus number and an error that probably means i passed well ovee the line of an normal integer number..
    So i figured what it does is keeps adding 1 to varable $a until it brakes cause the number gets to big...

    Now what i realy wanted it to do is to add 1 to varable $a every second..
    but when i refresh my page i want it to show me the current $a...
    I mighit have to use a database for this and i got it installed but again i never had anything done in SQL so i froze one again !

    If there is a chance i can do that with no database and its fairly simple please do ur best to help me :)

    Otherwise i will get on reading guides obout SQL righit away !
    info:
    Im running an apacahe server on Windows the client running is WAMPSERVER

    Cheers and thanks to all of you for helping so far :D
     
    Dormage, Jun 19, 2009 IP
  8. nishanair

    nishanair Active Member

    Messages:
    188
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #8
    You can do it with time difference.
     
    nishanair, Jun 19, 2009 IP
  9. Cloudberries

    Cloudberries Peon

    Messages:
    74
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #9
    That's because you've got yourself a big ol' infinite loop there my friend :)

    
    while(true){
    sleep(1);
    $a=$a+1;
    echo '<td>',$a,'</td>';
    }
    
    Code (markup):
    Basically, what you're saying there is "While this statement is true, keep adding a+1"

    Unfortunately, your statement is always going to be true. And there's no need whatsoever for "sleep"

    Try

    
    while($a < 30){
    $a=$a+1;
    echo '<td>',$a,'</td>';
    }
    
    Code (markup):
    Meaning - "While the variable a contains a number less than 30, keep going. Otherwise, stop"
     
    Cloudberries, Jun 20, 2009 IP
  10. Cloudberries

    Cloudberries Peon

    Messages:
    74
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Oh, and also -

    There's nothing wrong with mixing HTML and PHP in the same file - there's no penalty incurred in the code execution
     
    Cloudberries, Jun 20, 2009 IP
  11. nishanair

    nishanair Active Member

    Messages:
    188
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #11
    no penalty, but code work slow.
     
    nishanair, Jun 21, 2009 IP
  12. SA101

    SA101 Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    try using php include function
     
    SA101, Jun 21, 2009 IP