0

Update: Found a solution! Using chrome developer tools, I've found that the problem is hardcoding src files in /Applications/blahblah. It's looking for the files in localhost/Applications/blahblah. For now, I've copied the .js files it needed in a subdirectory to /Library/WebServer/Documents/ (where localhost seems to start from on my machine) and coded a path to there. Thanks for the help!


Newbie to PHP here. Can't find my answer with some thorough googling and lots of trying to debug myself.

I'm doing everything locally on my own machine.

I have a PHP class which builds an string that makes up an html page in its entirety, then returns it. My index.php script creates an instance of this class and calls the function that returns the html, and echos it the return. When I execute this class, the page comes up blank (using chrome as a browser). When I "view source" on the blank page, I see exactly the html script I had intended to view.

If I copy and paste that html script into a new file, blahblah.html and load blahblah.html directly with chrome, it works just fine.

Possible subtlety: the html string includes a javascript function which pulls from a hard-coded src directory on my machine.

Thanks for the help! Let me know what more information I might provide that could help.

EDIT: Here's the code:

index.php:

<?php

function __autoload($class_name) 
{
  $source = '/Path/To/src/' . $class_name . '.php';
  if (file_exists($source)) require $source; 
  else throw new Exception('class "' . $class_name . '" source file not found. Failed to autoload...');
}

$myweb=new GenHCSplineIrregTime();

echo $myweb->genWeb();

?>

GenHCSplineIrregTime.php:

<?php

class GenHCSplineIrregTime
{
    public function __construct()
    {
        ;
    }

    public function __destruct()
    {
        ;
    }

    public function genWeb()
    {
        return $this->genEntireHTMLFile();
    }

    private function genEntireHTMLFile()
    {        
        $cont="";
        // $cont = $cont . "<!DOCTYPE HTML>\n";
        $cont = $cont . "<HTML>\n<head>\n";
        $cont = $cont . "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n";
        $cont = $cont . "<title>This is my title</title>\n";
        $cont = $cont . "<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js\"></script>\n";
        $cont = $cont . "<style type=\"text/css\">\n\${demo.css}\n</style>\n";
        $cont = $cont . "<script type=\"text/javascript\">\n";
        $cont = $cont . "\$(function () {\n\n$('#container').highcharts({\nchart: {\ntype: 'spline'\n},\n title: {\ntext: 'BATTERY SHIT'\n},\nsubtitle: {\ntext: 'Irregular time data in Highcharts JS'\n},\n";
        $cont= $cont . "xAxis: {\ntype: 'datetime',\ndateTimeLabelFormats: {\nmonth: '%e. %b',\nyear: '%b'\n},\ntitle: {\ntext: 'Date'\n}\n},\n";
        $cont= $cont . "yAxis: {\ntitle: {\ntext: 'Snow depth (m)'\n},\nmin: 0\n},\ntooltip: {\nheaderFormat: '<b>{series.name}</b><br>',\npointFormat: '{point.x:%e. %b}: {point.y:.2f} m'\n},\n";
        //data starts here
        $cont= $cont . "series: [{\nname: 'Winter 2007-2008',\ndata: [\n";
        $cont= $cont . "[Date.UTC(1970,  9, 27), 0   ],\n";
        $cont= $cont . "[Date.UTC(1970, 10, 10), 0.6 ],\n";
        $cont= $cont . "[Date.UTC(1970, 10, 18), 0.7 ]\n]\n}]\n";
        $cont= $cont . "});\n});\n";

        $cont= $cont . "</script>\n</head>\n<body>\n";
        $cont= $cont . "<script src=\"/Applications/BasicSoftware/Highcharts-4.0.3/js/highcharts.js\"></script>\n";
        $cont= $cont . "<script src=\"/Applications/BasicSoftware/Highcharts-4.0.3/js/modules/exporting.js\"></script>\n";
        $cont= $cont . "<div id=\"container\" style=\"min-width: 310px; height: 400px; margin: 0 auto\"></div>\n";
        $cont= $cont . "</body>\n</html>\n";

        return $cont;
    }

    private function fetchData()
    {
        $data="";

        return $data;
    }

}
?>
5
  • It's probably because the PHP file isn't hosted. Commented Jul 29, 2014 at 0:53
  • Are you echoing at all? Could be one of endless reasons. "Let me know what more information I might provide that could help." - Code. Commented Jul 29, 2014 at 0:56
  • Can you post any of the code? @Shahar if the HTML code is outputted to a page, any web browser will process it. EDIT: Unless there's a mime-type issue with the server or lack of server... Commented Jul 29, 2014 at 0:56
  • @Fred-ii- My guess is that he doesn't have Wampserver/xampp. I had the same issue when I started with PHP. Or maybe it was because I didn't have PHP installed (do macs come with PHP?). Commented Jul 29, 2014 at 0:56
  • @Shahar You stand at being correct. Or PHP is not properly configured. Could be anything. OP needs to show some server info. Commented Jul 29, 2014 at 0:57

4 Answers 4

2

This happens because PHP is a server-sided scripting language, not a client-sided one like HTML. It needs a server to run on. To have a web server on your computer, you'll need software like WampServer or XAMPP. Once you have these installed, you can use PHP.

UPDATE: Your code outputs nothing because the GenHCSplineIrregTime class is not imported correctly. You will need to use the require or include (or require_once, include_once, depends on what you need) statements to add GenHCSplineIrregTime.php, but not the way you did it. You did what some old manuals show:

//foo.php
<?php
class foo {
    public function __construct() {
        echo "hi";
    }
}
?>

//index.php
<?php
function __autoload($classname) {
    $filename = "./". $classname .".php";
    include_once($filename);
}

$obj = new foo();
?>

Problem is, what's $classname? The code is incomplete; it will just load nothing, really, or some irrelevant file. That's why, instead, you'll almost always see:

//index.php
<?php
require 'foo.php';
$obj = new foo();
?>

UPDATE 2: I am glad that your problem was resolved but for next time, pay special attention to the title of the page, which was changed (because everything but the Javascript part of the HTML worked). Hence, it wasn't a completely blank page as you implied (i.e. no HTML compiled).

Sign up to request clarification or add additional context in comments.

2 Comments

Hrmm.. I think I have a server running? I'm using a mac and I've got apache running. For example, if I try to open localhost index.php that contains the simple "<?php phpinfo(); ?>", this works fine. Does that mean I have a server running?
@n3utrino Yes it does. Give me a second to look at the code.
1

if you have your html code in a string, let say $str, all you need is to print the content of the string just like this :

$str "<h1> Hello world ! </h1>";
echo $str;

If your html code is inside another page , all you need to do is to load the content of your page inside a string and print it :

   $str = file_get_contents("path_to/blahblah.html");
   echo $str;

Comments

0

PHP only runs on a server.

Download XAMPP and run apache server.

After you downloaded and installed it, go to the xampp folder, and to htdocs folder and put your php file in there.

And navigate to where your php file is located, for example: localhost:/index.php.

Then your php code should run properly.

4 Comments

I'm running on a mac and I have apache server running. If I have a simple <?php phpinfo(); ?> index.php script, then localhost:/index.php opens up fine... is there more I have to do?
Hmm, did you make sure you named your file .php at the end? Make sure of that, if the extension of the file is .html it only runs html and javascript and css code, it does not run php code.
Yup the file is php. What it boils down to is that it's a .php file with an echo inside it of a longass string of html (with a javascript function inside of it that). If I take that echo and save it separately as a .html file, the browser will load that file fine. Bah!
Kk I edited the original post to show the code. The generating of the html is a bit messy... if it helps, I can run the script, view source to obtain the nicer looking html, and post it too.
0

Well if you're viewing the HTML in the source then you know that the HTML string is being printed. So maybe you're doing something with headers? Possibly Chrome doesn't expect to receive HTML content when you print it from PHP, but when you take the same content and put it into a file with .html extension it's a no-brainer, so it renders it automatically. Try opening up the Chrome's dev tools, reload the page and look under 'network' to see what headers you're receiving.

If you are in fact printing the html string, as you've verified in the 'source' received by the client (chrome) then you know your php code was evaluated. Given that as true, and without trying to guess at what is going on with the js script, that is the only thing I can think of.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.