Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #7850

Using echo to run separate PHP script from HTML

From "P E Schoen" <paul@peschoen.com>
Newsgroups comp.lang.php
Subject Using echo to run separate PHP script from HTML
Date 2012-12-19 07:30 -0500
Organization A noiseless patient Spider
Message-ID <kasc1f$m0s$1@dont-email.me> (permalink)

Show all headers | View raw


I adapted a PHP script that presents a vertical bargraph showing a goal and 
current value and percentage. It was from:
http://www.scriptygoddess.com/archives/2008/02/23/how-to-make-a-progressgoal-thermometer-like-bar-graph-with-php/

My adaption includes a text file for the goal and current value as well as 
height and width. This "goalgraph.php" works OK:

<?php
$datafile = "goalgraphdata.txt";
if( !file_exists($datafile) ) {
    $df = fopen( $datafile, "w" ) or die( "File Error" );
    // cur, goal, height, width
    fwrite( $df, "100\r\n50\r\n200\r\n25\r\n" );
    fclose( $df );
    }

if (isset($_GET['cur']) && isset($_GET['goal']) && isset($_GET['height']) && 
isset($_GET['width']) ) {

    header("Content-type: image/png");

    //setting the values we'll use in our script pulled in from GET
    $height = trim($_GET['height']);
    $width = trim($_GET['width']);
    $goal = trim($_GET['goal']);
    $current = trim($_GET['cur']);

    $df = fopen( $datafile, "w" ) or die( "File Error" );
    // cur, goal, height, width
    fwrite( $df, "$current\r\n$goal\r\n$height\r\n$width\r\n" );
    fclose( $df );
    }

$df = fopen( $datafile, "r" ) or die( "File Error" );
$current = trim(fgets( $df ));
$goal = trim(fgets( $df ));
$height = trim(fgets( $df ));
$width = trim(fgets( $df ));
fclose( $df );

if ($current > $goal) {
        $current = 100;
        $percent= "100%!";
    } else if ($current == "" || $current == "0") {
        $percent = "0%";
    } else {
        $percent = round(($current/$goal)*100) . "%";
    }

    $im = @imagecreate($width,$height)
        or die("Cannot Initialize new GD image stream");

    $bg = ImageColorAllocate($im,200,200,200); // grey
    $fg = ImageColorAllocate($im,255,0,0); //red
    $text_color = imagecolorallocate($im, 0, 0, 0); //black
    $text_color1 = imagecolorallocate($im, 255, 255, 0255); //white


    $fill = $height-(($current/$goal)*$height);

    imagefilledrectangle($im, 0, $fill, $width, $height, $fg);

    $font = 3;
    imagestring($im, $font, 2, 0,  trim($goal), $text_color);
    imagestring($im, 5, 5, $height-$percent*$height/200-8,  $percent, 
$text_color1);
    imagestring($im, $font, 2, $height-$percent*$height/100, 
trim($current), $text_color);

    imagepng($im);

    imagedestroy($im);

?>

I am able to access this script using a simple HTML file if I do it with a 
direct <img src="goalgraph.php" />. But when I try to do so using PHP within 
the HTML, using echo, it does not work. I tried renaming the file php and 
adding the "AddType application/x-httpd-php .htm" to a .htaccess file, but 
no joy. Here is what I have:

<html>
<body>
Goalgraph
<hr />
<?php
//echo '<img src="' . "goalgraph.php" . '" />' . "\n";

$datafile = "goalgraphdata.txt";
if( file_exists($datafile) ) {
    echo "File Found\n";
    $df = fopen( $datafile, "r" ) or die( "File Error" );
    $current = trim(fgets( $df ));
    $goal = trim(fgets( $df ));
    $height = trim(fgets( $df ));
    $width = trim(fgets( $df ));
    fclose( $df );

    echo '<img src="' . htmlspecialchars("goalgraph.php?cur=" . 
urlencode($current) . "&goal=" . urlencode($goal) .
    "&width=" . urlencode($width) . "&height=" . urlencode($height) ) . '" 
alt="Help us achieve our goal"' . "  /> \n";
    }
else
    echo "File not found\n";
?>
<hr />
<img src="goalgraph.php" />
</body>

</html>

It's been a long time since I have done anything with PHP or HTML for that 
matter, so I'm quite rusty, and it may be something simple. Any help will be 
most kindly appreciated.

Thanks!

Paul 

Back to comp.lang.php | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Using echo to run separate PHP script from HTML "P E Schoen" <paul@peschoen.com> - 2012-12-19 07:30 -0500
  Re: Using echo to run separate PHP script from HTML Richard Yates <richard@yatesguitar.com> - 2012-12-19 07:02 -0800
    Re: Using echo to run separate PHP script from HTML "P E Schoen" <paul@peschoen.com> - 2012-12-19 13:13 -0500
      Re: Using echo to run separate PHP script from HTML Jerry Stuckle <jstucklex@attglobal.net> - 2012-12-19 15:05 -0500
        Re: Using echo to run separate PHP script from HTML "P E Schoen" <paul@peschoen.com> - 2012-12-19 16:59 -0500
          Re: Using echo to run separate PHP script from HTML Jerry Stuckle <jstucklex@attglobal.net> - 2012-12-19 17:50 -0500
      Re: Using echo to run separate PHP script from HTML Luuk <luuk@invalid.lan> - 2012-12-19 21:23 +0100
        Re: Using echo to run separate PHP script from HTML "P E Schoen" <paul@peschoen.com> - 2012-12-19 18:33 -0500
          Re: Using echo to run separate PHP script from HTML Richard Yates <richard@yatesguitar.com> - 2012-12-19 17:24 -0800
      Re: Using echo to run separate PHP script from HTML "Peter H. Coffin" <hellsop@ninehells.com> - 2012-12-19 14:32 -0600
        Re: Using echo to run separate PHP script from HTML Jerry Stuckle <jstucklex@attglobal.net> - 2012-12-19 17:51 -0500
  Re: Using echo to run separate PHP script from HTML The Natural Philosopher <tnp@invalid.invalid> - 2012-12-19 16:04 +0000
    Re: Using echo to run separate PHP script from HTML "P E Schoen" <paul@peschoen.com> - 2012-12-19 13:23 -0500

csiph-web