1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Inserting php into html

Discussion in 'HTML & Website Design' started by lazerorca, Oct 23, 2014.

  1. #1
    so I have a separate php file that echoes html code that I would like to insert into an html file. when I try to insert it using <?php include pathtofile.php ?> or <?php require pathtofile.php ?>, the code doesn't show and also the rest of the html document gets chopped off from that point forward.

    also when I try to load the said file, pathtofile.php alone in the browser... I get nothing. I am not completely sure what is going on. I have what I want to echo in double quotes, so i suspect it is something with "escaping characters", but I am not sure.
     
    lazerorca, Oct 23, 2014 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    Your webserver is setup so that files with a .htm or .html will be sent straight to the browser with no server side processing. You can change that but it's simpler and safer to just change the file name to .php instead.

    I got a sneak peak at the code you posted and here are some thoughts
    for ($i=1, $i>=$gridCount, $i++) {
    PHP:
    This will only run when $i is greater than 20, ie never. Change to
    for ($i=1, $i<=$gridCount, $i++) {
    PHP:
    do you have an image called 20x20 - are you sure it's not 20x20.jpg or 20x20.png?

    when you echo out a string the double quote " is different to the single quote '

    echo '<div id="deal-$i" class="deal-grid">';
    PHP:
    This snippet uses the single quote so it will echo out exactly what is between the quote marks with no further processing.
    echo "<div id='deal-$i' class='deal-grid'>";
    PHP:
    In this one I've swapped the single and double quotes around. Double quotes tell the server to do more processing and look for variables so the output will replace $i with the actual value - which is what you want
    echo "<div id='deal-{$i}' class='deal-grid'>";
    PHP:
    You can play it really safe by putting curly brackets {} around the variable which makes sure the server knows exactly what to do.
     
    sarahk, Oct 23, 2014 IP
  3. pthomasgarcia

    pthomasgarcia Greenhorn

    Messages:
    13
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    23
    #3
    Try this. If you don't have an .htaccess file, then create one. Else, edit that file and add the following line to it:

    AddType application/x-httpd-php .php .html
     
    pthomasgarcia, Oct 23, 2014 IP
  4. lazerorca

    lazerorca Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    1
    #4
    sarahk

    I tried all of those things you suggested... still not working :-(

    The tutorials I have read indicate that this should work... I should be able to insert it as follows correct???

    <div>
    <?php include=pathtofile.php>
    </div>

    So everything that is echoed in the php file should echo in the div correct?

    pthomasgarcia

    I also created a .htaccess file and I put in what you posted and saved it. it wanted to save it as a hidden file. I chose the option to allow me to see hidden files on my ftp server and it was in fact there. does this file need to be in the "www" or "public_html" folder or in the parent directory to the "www" or "public_html" folder
     
    Last edited: Oct 23, 2014
    lazerorca, Oct 23, 2014 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #5
    it goes in the public_html folder however because of your inexperience I'm concerned that you may run into other problems.

    to include a file you do this
    <div>
    <?php include 'myfile.php'; ?>
    </div>
    PHP:
    how about you post the relevant parts of your html and php code here, remember to wrap in bbcode like [ code] and then [ /code]
     
    sarahk, Oct 23, 2014 IP
  6. lazerorca

    lazerorca Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    1
    #6
    ok I have a really stupid question to ask... is there a special declaration that I have to make if it is a php file for !DOCTYPE?

    BTW thank you so much for taking time to help me
     
    lazerorca, Oct 23, 2014 IP
  7. lazerorca

    lazerorca Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    1
    #7
    here is the html file

    
    <!doctype html>
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="css/normalize.css">
    <link rel="stylesheet" type="text/css" href="css/editor.css">
    <meta charset="UTF-8">
    <title>test</title>
    </head>
    
    <body>
    <header>
    <div id="top-head">
       <div id="lt-top-head"test test test test</div>
      <div id="ctr-top-head">
         <table id="wrapper">
           <tr>
           <td><img src="images/header-logo.jpg" alt="" /></td>
           </tr>
           </table>
      </div>
      <div id="rt-top-head">test</div>
      <br style="clear:left;" />
    </div>
    </header>
    
    <nav>
    <div id="top-nav">
       <a href="#">test1</a> |
         <a href="#">test2</a> |
         <a href="#">test3</a> |
         <a href="#">test4</a> |
    </div>
    <div id="btm-nav">
       <a href="#">test1</a> |
         <a href="#">test2</a> |
         <a href="#">test3</a> |
         <a href="#">test4</a> |
    </div>
    </nav>
    
    <div id="wrapper">
       <div id="lt-sidebar">
         Text 1
      </div>
     
       <div id="rt-sidebar">
         Text 2
      </div>
     
       <div id="main">
         <div id="main-grid"> 
      <?php
      include "grid.php";
      ?>
         </div>
      </div>
       <div class="clear"></div>
    </div>
    
    </body>
    </html>
    Code (markup):
    here is the php file

    
    <?php
    $gridCount=20
    for ($i=1, $i<=$gridCount, $i++) {
    echo "<div id='deal-{$i}' class='deal-grid'>
      <div id='deal-{$i}-image' class='deal-image'>
      <img src='http://placehold.it/100x100' id='wrapper' alt='' />
      </div>
      <div id='deal-{$i}-desc' class='deal-desc'>
      <p>thingamajig</p>
      </div>
      <div id='deal-{$i}-vote' class='deal-vote'>
      <img src='http://placehold.it/20x20' alt='' />
      <img src='http://placehold.it/20x20' alt='' /> 
      </div>
      </div>";
    }
    ?>
    
    Code (markup):
    This is the code that was working for me, so you can see what I am trying to do

    
    <!doctype html>
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="css/normalize.css">
    <link rel="stylesheet" type="text/css" href="css/editor.css">
    <meta charset="UTF-8">
    <title>Untitled Document</title>
    </head>
    
    <body>
    <header>
    <div id="top-head">
       <div id="lt-top-head">dasfdasfs</div>
      <div id="ctr-top-head">
         <table id="wrapper">
           <tr>
           <td><img src="images/header-logo.jpg" alt="" /></td>
           </tr>
           </table>
      </div>
      <div id="rt-top-head">dsafdsafdsa</div>
      <br style="clear:left;" />
    </div>
    </header>
    
    <nav>
    <div id="top-nav">
       <a href="#">test1</a> |
         <a href="#">test2</a> |
         <a href="#">test3</a> |
         <a href="#">test4</a> |
    </div>
    <div id="btm-nav">
       <a href="#">test1</a> |
         <a href="#">test2</a> |
         <a href="#">test3</a> |
         <a href="#">test4</a> |
    </div>
    </nav>
    
    <div id="wrapper">
       <div id="lt-sidebar">
         Text 1
      </div>
      
       <div id="rt-sidebar">
         Text 2
      </div>
      
       <div id="main">
         <div id="main-grid">  
      <div id="deal-1" class="deal-grid">
      <div id="deal-1-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-1-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-1-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
      <div id="deal-2" class="deal-grid">
      <div id="deal-2-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-2-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-2-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
      <div id="deal-3" class="deal-grid">
      <div id="deal-3-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-3-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-3-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
      <div id="deal-4" class="deal-grid">
      <div id="deal-4-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-4-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-4-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
      <div id="deal-5" class="deal-grid">
      <div id="deal-5-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-5-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-5-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
      <div id="deal-6" class="deal-grid">
      <div id="deal-6-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-6-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-6-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
      <div id="deal-7" class="deal-grid">
      <div id="deal-7-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-7-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-7-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
      <div id="deal-8" class="deal-grid">
      <div id="deal-8-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-8-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-8-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
      <div id="deal-9" class="deal-grid">
      <div id="deal-9-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-9-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-9-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
      <div id="deal-10" class="deal-grid">
      <div id="deal-10-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-10-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-10-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
      <div id="deal-11" class="deal-grid">
      <div id="deal-11-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-11-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-11-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
      <div id="deal-12" class="deal-grid">
      <div id="deal-12-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-12-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-12-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
      <div id="deal-13" class="deal-grid">
      <div id="deal-13-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-13-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-13-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
      <div id="deal-14" class="deal-grid">
      <div id="deal-14-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-14-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-14-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
      <div id="deal-15" class="deal-grid">
      <div id="deal-15-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-15-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-15-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
      <div id="deal-16" class="deal-grid">
      <div id="deal-16-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-16-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-16-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
      <div id="deal-17" class="deal-grid">
      <div id="deal-17-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-17-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-17-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
      <div id="deal-18" class="deal-grid">
      <div id="deal-18-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-18-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-18-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
      <div id="deal-19" class="deal-grid">
      <div id="deal-19-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-19-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-19-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
      <div id="deal-20" class="deal-grid">
      <div id="deal-20-image" class="deal-image">
      <img src="http://placehold.it/100x100" id="wrapper" alt="" />
      </div>
      <div id="deal-20-desc" class="deal-desc">
      <p>$15 thingamajig</p>
      </div>
      <div id="deal-20-vote" class="deal-vote">
      <img src="http://placehold.it/20x20" alt="" />
      <img src="http://placehold.it/20x20" alt="" />  
      </div>
      </div>
         </div>
      </div>
       <div class="clear"></div>
    </div>
    
    </body>
    </html>
    
    Code (markup):
    Do I need to echo each line individually??? or can I just echo the whole block?
     
    Last edited: Oct 23, 2014
    lazerorca, Oct 23, 2014 IP
  8. pthomasgarcia

    pthomasgarcia Greenhorn

    Messages:
    13
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    23
    #8
    Do you have error reporting enabled in PHP? If so, what are the errors you are receiving? If not, enable error reporting, it will make your debugging life a whole deal easier.

    There's no special declaration you need to use in your .html files in order to use PHP, except to use PHP tags and your server be configured for PHP. Also, you can echo the entire block, no need to echo each line.

    I noticed in the grid.php file, you missed a semi-colon at the end of line 3, and on line 4, you used commas instead of semi-colons.

    Try this:

    
    <?php
    $gridCount=20;
    
    for ($i=1; $i<=$gridCount; $i++) {
    	echo "<div id='deal-{$i}' class='deal-grid'>
    	<div id='deal-{$i}-image' class='deal-image'>
    	<img src='http://placehold.it/100x100' id='wrapper' alt='' />
    	</div>
    	<div id='deal-{$i}-desc' class='deal-desc'>
    	<p>thingamajig</p>
    	</div>
    	<div id='deal-{$i}-vote' class='deal-vote'>
    	<img src='http://placehold.it/20x20' alt='' />
    	<img src='http://placehold.it/20x20' alt='' />
    	</div>
    	</div>";
    }
    ?>
    
    PHP:
     
    pthomasgarcia, Oct 23, 2014 IP
  9. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #9
    echo'ing the block is fine but you have to change the name of the html file to .php

    When someone makes a request to a domain and doesn't specify the filename the server will try to find an index file and work through .html, .htm, .php and probably some other stuff.
     
    sarahk, Oct 23, 2014 IP
  10. lazerorca

    lazerorca Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    1
    #10
    I am using godaddy as my host, so I am not sure how to turn on error reporting. let me do some googling and find out.

    So I connected to the ftp server and couldn't find php.ini... I used to file manager in cpanel to also look around. I couldn't find it either. there is an option in the cpanel called php configuration. I clicked that, but I didn't see any options for error reporting. isn't there a way I can code for it to echo out errors in an "if" statement?

    man this is super frustrating lol. this is my first attempt at doing coding, so i am struggling with the syntax. up until now, I just used to code excel spreadsheets in college, which i was somewhat good at. I like to think that I am somewhat computer savvy, but yeah evidently not lol. this is all new stuff to me.

    UPDATE: AWESOME IT IS WORKING! THANK YOU GUYS! I am going to feel like a dummy for admitting this, but when I was saving the file in BBEdit to the ftp server with the things you were showing me, it wasn't saving it in the www, I was saving it in the root folder. I have my other ftp server set up to connect directly to the www directory, so I had gotten in the habit of just saving it stuff without changing directories! It is satisfying to get things to actually work! I think that is why I have always had an interest in it. I think that is going to keep my interest too!

    However, I would like to find out how to be able to get the errors reported to me on godaddy. I get errors reported when I do things on my localhost server on my computer, but not on godaddy for some reason
     
    Last edited: Oct 24, 2014
    lazerorca, Oct 24, 2014 IP
  11. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #11
    well done!

    I use filezilla and it has "synchronised browsing" which helps prevent uploads to the wrong folder. Check it out.

    you won't find a control panel setting for error reporting - and you won't have access to php.ini if you are on shared hosting like most of us.

    this is what you need to use: http://php.net/manual/en/function.error-reporting.php
     
    sarahk, Oct 24, 2014 IP
  12. COBOLdinosaur

    COBOLdinosaur Active Member

    Messages:
    515
    Likes Received:
    123
    Best Answers:
    11
    Trophy Points:
    95
    #12
    The easy way to display errors in a limited environment is to put this at the beginning of php while you are developing:
    error_reporting(E_ALL ^ E_NOTICE);
    ini_set('display_errors', 1);
    When you are ready to make it live comment out the line as it would be a security hole to display the in a production environment.

    You cal also use var_dump during development to examine the value of variables.
     
    COBOLdinosaur, Oct 24, 2014 IP