Page 1 of 1

open file, passing variable data, and insert into a db

Posted: Sat Jun 26, 2010 9:03 am
by lauthiamkok
Hi,
I think this is the most common method to pass variable data into html tags and then insert this html into the database, below is an abstract of the method in a class (I'm using OOP),

Code: Select all

$content = '
<table>
<tr><td>
<p>Dear '.$this -> database -> real_escape_string($params['first_name']).',</p>
<p>Thank you for your order....</p>
<td>		
</tr>
</table>';

$content = "'".$this -> database -> real_escape_string($content)."'";

$sql = " INSERT INTO invoices (content) VALUES($content)"

$result = $this -> database -> query($sql);
The issue is, I have very long html page needs to be passed it into the database, and I don't want to put the long html tags into my class's method, bcos this html file is just a template which will be changed from time to time. so ideally I will put the html code above into an external - template.php so that I will need to update this template only without messing around too much in my class method. and the most important thing in this template is, I want to pass some variable data into it, such as above.

I tried include() like this,

Code: Select all

$content = include 'template.php';
but it shows the value of '1' in my database which is very strange.

So I tried file_get_contents(),

Code: Select all

$content = file_get_contents('template.php');
it then won't pass any variables into it. it will shows this in the database - $this -> database -> real_escape_string($params['first_name']),instead of the information.

it would be great if you have any ideas.

Many thanks,
Lau

Re: open file, passing variable data, and insert into a db

Posted: Sat Jun 26, 2010 2:04 pm
by requinix
If you don't want to put it into the database then why are you trying to?

Hint: use output buffering while you include the file.

Re: open file, passing variable data, and insert into a db

Posted: Sat Jun 26, 2010 8:56 pm
by lauthiamkok
tasairis wrote:If you don't want to put it into the database then why are you trying to?

Hint: use output buffering while you include the file.
thanks! i got it sorted with this function,

Code: Select all

public function get_include_contents($filename,$params) 
	{
		if (is_file($filename)) 
		{
			ob_start();
			include $filename;
			$contents = ob_get_contents();
			ob_end_clean();
			return $contents;
		}
		return false;
	}
I modified it from http://uk3.php.net/manual/en/function.include.php Example #6 Using output buffering to include a PHP file into a string

but I dont understand it at all! I have read the documentation from php.net about ob_start() and buffering, but still don't understand it.
ob_start — Turn on output buffering
- what does it mean by buffering??

Re: open file, passing variable data, and insert into a db

Posted: Sat Jun 26, 2010 9:15 pm
by requinix
It lets you put output into a buffer instead of sending it directly to the client.

Re: open file, passing variable data, and insert into a db

Posted: Sun Jun 27, 2010 7:33 am
by lauthiamkok
tasairis wrote:It lets you put output into a buffer instead of sending it directly to the client.
now I understand! thank you so much! :D