Jump to content

Insert php table code in tpl file


LexHammer

Recommended Posts

Hello,

i have this php code. Its for sms insertion of links in to table

<table border="1">
<?PHP

$db_user = 'user';
$db_pass = 'pass';
$db_name = 'name';
$db_host = 'localhost';
if(mysql_connect($db_host,$db_user,$db_pass)) {
mysql_select_db($db_name);
mysql_query("CREATE TABLE IF NOT EXISTS smsads(id bigint unsigned primary key auto_increment, link varchar(255), fromnum varchar(60))");
$res = mysql_query("SELECT * FROM smsads ORDER BY id DESC LIMIT 10");
while($row = mysql_fetch_object($res)) {
	$http_link = $row->link;
	if(strstr($http_link, 'http') === FALSE) $http_link = 'http://'.$http_link;
	echo "<tr><td><a href=\"{$http_link}\" target=\"_blank\">{$row->link}</a></td></tr>";
}
}
?>
</table>

 

The problem is that i use tpl files for the visual part and all my other tables are in tpl. How can i insert the table above in my tpl file, whitout loosing its functions?

Link to comment
Share on other sites

load the tpl file with file() or fopen().

 

in your tpl file there should be a string that you can "replace" eg: <!--//REPLACEME//-->

 

then once you fopen/file() the tpl you would use str_replace("<!--//REPLACEME//-->", $table_result, $tpl_file);

Then echo the result of the str_replace().

 

You should only really echo Once in your code, ever. Everything else should be in a variable up until that point, then you would not have this problem as you would already being doing what i just said with all your tpl files :P.

 

So you will have to change that echo to a variable decleration:

 

<table border="1">
<?PHP

$db_user = 'user';
$db_pass = 'pass';
$db_name = 'name';
$db_host = 'localhost';
if(mysql_connect($db_host,$db_user,$db_pass)) {
mysql_select_db($db_name);
mysql_query("CREATE TABLE IF NOT EXISTS smsads(id bigint unsigned primary key auto_increment, link varchar(255), fromnum varchar(60))");
$res = mysql_query("SELECT * FROM smsads ORDER BY id DESC LIMIT 10");

$table_content = null;

while($row = mysql_fetch_object($res)) {
	$http_link = $row->link;
	if(strstr($http_link, 'http') === FALSE) $http_link = 'http://'.$http_link;
	$table_content .= "<tr><td><a href=\"{$http_link}\" target=\"_blank\">{$row->link}</a></td></tr>";
}
}

echo $table_content;
?>
</table>

 

hope this helps

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.