Author Topic: php inline with html working oddly  (Read 637 times)

andymcd

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
php inline with html working oddly
« on: August 19, 2010, 12:14:56 PM »
i support a website for a bunch of us jazz musicians. i have some php that works great to select a random image to display, but when i use it inline it give some odd output.  the code below works from the command line, but when i try to use it inline it prints '=0 echo....'  as though the "$rand >= 0" wasn't working as a comparison operator. is there something i'm missing about using php inline?  any thoughts appreciated. --andy


<?php
$dir=opendir("../httpdocs/img/spinners/");
$i=0;
while($imgfile=readdir($dir))
{
     if ($imgfile != "." && $imgfile!="..")
         {
        $imgarray[$i]=$imgfile;
        $i++;
        }
}

closedir($dir);

$rand=rand(0,count($imgarray)-1);

    if ($rand >= 0)
    {
    echo '<img src="../httpdocs/img/spinners/'.$imgarray[$rand].'" width="170" height="137">';
    }
?>

phphelp

  • Administrator
  • Senior Member
  • *****
  • Posts: 777
  • Karma: +3/-0
    • View Profile
    • PHP Help forum
Re: php inline with html working oddly
« Reply #1 on: August 19, 2010, 12:38:29 PM »
Do you have this code in the .php file or in .html? It looks like this page (and php code) is interpreted as html. So, everything between opening <?php ... and $rand > is interpreted as html tag (thus not displaying), while content start with = 0

If this code is within .php file, you need to make sure php is installed and correct mime type is configured for .php files. If this code is within .html file, you can either rename it to .php or add handler for .html pages within this directory using .htaccess

.htaccess:
Code: [Select]
AddHandler application/x-httpd-php .html
Are you experienced in PHP programming and willing to share your knowledge?
Join us at php help forum!

andymcd

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: php inline with html working oddly
« Reply #2 on: August 19, 2010, 01:37:53 PM »
ahh... i see.. yes it is an html file... i expected anything within <?  and ?>  to be interpreted as PHP.  i did try changing extension to php, but some other code got confused... i'm going to do the job with external php for the time being, and learn some more about how PHP gets handled.   thanks so much for your prompt reply. it was driving me nuts. :-)  it does make sense. appreciate your help.

andymcd

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: php inline with html working oddly
« Reply #3 on: August 19, 2010, 01:55:51 PM »
forgot to mention how much i like PHP and happy to be part of the community.   hope i can help someone one day.  thanks again.

phphelp

  • Administrator
  • Senior Member
  • *****
  • Posts: 777
  • Karma: +3/-0
    • View Profile
    • PHP Help forum
Re: php inline with html working oddly
« Reply #4 on: August 19, 2010, 01:59:38 PM »
You're welcome! We hope to see you here often  ;)
Are you experienced in PHP programming and willing to share your knowledge?
Join us at php help forum!