Mombu the Php Forum sponsored links

Go Back   Mombu the Php Forum > Php > Dynamically generated HTML
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 8th February 12:54
william wilcox
External User
 
Posts: 1
Default Dynamically generated HTML



I've stumbled around on this for the past three days, and I'm hoping
someone here can give me some insight. I put together project just to
learn a bit of php and had some success, so now I'm putting together
something quite a bit more ambitious.

What I'm trying to do: Have one main script, which depending on the
variables passed to it, generates different content in HTML. I want 3
functions, one to generate the HTML header, one to generate the body and
one to generate the footer. What's returned from all three functions
should be put into one do***ent and passed back to the browser.

The problem is the functions work by themselves, but the output isn't
being passed back to the main script or to the browser.

Here's the function code:

<?php
require_once "HTML/Template/IT.php";
require_once "DB.php";
require "db.inc";

// Logic to decide what the title of the page should be.
// Check to see if there is an article id in the page request.
$id = $_GET['id'];

if ( !empty( $id ))
{
// Open a connection to the database
$connection = DB::connect($dsn);

// Give me an error if something is wrong
if (DB::isError($connection))
ShowError($connection);

// Put together the query to get the title of the article.
$query = "SELECT artTitle FROM article WHERE artID = '$id'";

// Get the result from the database.
$result = $connection->query($query);

// Show an error message if something goes wrong.
if (DB::isError($result))
ShowError($result);

// Otherwise, set the title variable
$title=$result;

// Disconnect from the database
$connection->disconnect();

}
else
{
// for now this is set statically. Eventually it should be read
// from an xml site config file.
$title="Default Page Title";
}

// Put the header template together.
$template = new HTML_Template_IT("./templates");
$template->loadTemplatefile("header.tpl", true, true);

// Work with the title block.
$template->setCurrentBlock("TITLEVAR");

// Assign the title variable
$template->setVariable("TITLE", $title);

// Parse the block
$template->parseCurrentBlock();

// Output the HTML
$template->show();

?>

I think I may be making a mountain out of a mole hill. The reason I
want to break up the header, body and footer is that the content in each
can change quite dramatically. So I want to be able to generate just
the HTML for a particular object, and then composite all the objects
into one page. I don't want to produce a separate template for each
combination of objects. Just one template, and then each object going
into the page has a method which specifies how to display itself.

My normal google research isn't helping me, but that may be because I
don't know what I'm really searching for.

Thanks in advance.
Bill
  Reply With Quote


  sponsored links


2 8th February 12:55
william wilcox
External User
 
Posts: 1
Default Dynamically generated HTML



I think I may be stumbling in the right direction, but clarification
from someone more familiar with what I'm trying to do would be
appreciated.

Since my original post I've discovered ob_start() and ob_flush(). This
promises to do exactly what I want. My main script starts a buffer to
hold everything till all the parts are done executing, and then flush
the whole bit back to the browser.

I still have a problem in that the code generated from my function isn't
being passed back to the main program, and therefore it's not in the
buffer to be flushed.

I put the function into it's own file and call it from the main program
like this:

//START
ob_start();
require('./site/header.php');
ob_flush();
//STOP

Nothing is passed to the browser. If I execute the function directly
from the browser it sends the code I expect. This isn't the whole code,
but what I'm working with at the moment to find the solution to my
problem.

It seems I still don't understand how I can get the function to pass the
resulting html header text back to the main program.

If I put the function into a class as a method, and then create an
object to access the method from the body of my main program
(remembering to include the class file), would that then make the output
of the method available to the main program?

I've tried this as well using the following code in the body of my main
program:

// START
require "./site/test.php"; //Where the header class exists
require "HTML/Template/IT.php";
require "DB.php";
require "db.inc";

// Try to buffer the output.
ob_start();

// Make an instance of the header class
$head = new Header();

// Execute the method which should create the header
$head->displayHeader();

// Now flush the buffer contents
ob_flush();
//STOP

I have the same problem here. The page returned to the browser is
completely blank with no HTML code at all. I'm still missing some bit
of php wisedom...well I'm missing a lot of wisedom actually, but that's
another topic. ;-)

Just for kicks and grins, here's what I've been reading about output
buffering.
http://www.devshed.com/c/a/PHP/Output-Buffering-With-PHP/1/
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=1076&
lngWId=8
http://us2.php.net/manual/en/function.ob-start.php
  Reply With Quote
3 10th February 03:09
piet meins
External User
 
Posts: 1
Default Dynamically generated HTML


William Wilcox schrieb:


Hi William,

I'm not quite shure if You got the same problem like I do. I tried to
simplify the whole thing:

$i = 0;
while ($i++ < 10)
{
// Output to a new page!?!
print("<html>");
print("<head>");
print("<title>Titel</title>");
print("</head> \n");
print("<body> \n"); print("Test $i \n");
print("</body> \n");
print("</html> \n");
flush();
ob_flush();
sleep(2);
}

In spite of opening a new page in every walkthrough and showing Testx on
new pages, the code shows a long row of Test1 Test2 etc. on o n e page.

Is that Your problem?

I'm not unexperienced in programming but new to php and web-server
programming. Most probably this is a very simple thing and we don't see
the forest because of all those trees!

I would be highly appreciated, if You tell me as soon as You'll solve the problem.
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666