Saturday, February 24, 2007

Embedding PHP query results into your web page

In an earlier post I gave an example of a PostgreSQL function and a php script that would output the information in a sample web page. Now what am I going to do with this script? I really would like to embed the information within another web page. I also have other little snippets of information that I would like to update in a similar manner, just straight queries instead of a function.

I quickly learned that you can not just embed you php code into a normal html file and expect it to display the information you want. Searching google with the phrase embed php into HTML does not bring back the results I was looking for. Well one result hinted at what I wanted. SSI (Server Side Includes) Searching on this phrase got me the following link on how to enable it on your apache web server.

Here is some example code.

DIV style="position:absolute;left:500;top:130px;width:300px;height:160px;z-index:1" align="center"
FONT style="font-size:24px" color="#0000FF" face="Arial" B Most Viewed Topics /B
PRE
!--#exec cgi="/cgi-bin/4rca/top_ten_views.php"--
/PRE
/DIV

This is the code that you embed into your page. I took a shortcut on my setups of SSI. I’m having it parse all HTML pages. Eventually I’m going to embed all of the pages with small snippets of code. So I feel comfortable with this decision.

DIV helps to place this inserted result exactly where you want it to display on the page.

!--#exec cgi="/cgi-bin/4rca/top_ten_views.php"--

This line makes the call to the script that is pulling back the information that you want to embed into your page.

Next is the php program


?php
// This defines some base paths to locate other files needed in by the demo
define ('IN_ROOT', true);
$root_path= './';
include($root_path . 'phpbb_db_connect.php');
// Connect and get Top N results
$connection = pg_connect("host=$host dbname=$db user=$user password=$pass");
if (!$connection)
{
die ("Could not open connection to database server");
}

$query = "select * from get_top_posts() AS (users varchar , topic varchar, views int )";

$res = pg_query($connection, $query) or die("Error in query: $query." . pg_last_error($connection));

$count_row = pg_fetch_row( $res, 0 );

$rows = pg_num_rows($res);

$query_info = array(null);

for ( $counter = 0; $counter = $rows-1; $counter += 1)
{ $query_info[$counter] = pg_fetch_row( $res, $counter );
}

?
table
?php
print " tr td FONT style=\"font-size:14px\" color=\"#000000\" face=\"Arial\" Users /FONT /td td FONT style=\"font-size:14px\" color=\"#000000\" face=\"Arial\" Topics /FONT /td td FONT style=\"font-size:14px\" color=\"#000000\" face=\"Arial\" Views /FONT /td /tr \n";

for ($k=0;$k =$rows-1;$k++) {
print " tr ";
for ($j=0;$j =2;$j++) {
echo " td FONT style=\"font-size:12px\" color=\"#000000\" face=\"Arial\" ",$query_info[$k][$j]," /FONT /td ";
}
print " /tr \n";
}
?
/table

This program is a lot shorter than the original. I’ve been able to pull out all of the extra HTML formatting that was included in the original. Now that the SSI process returns the results I just have to encapsulate the results with the proper HTML format and not worry about the Page setups. Everything between the table tags is what will be displayed on the embedded page.

You can go here to see a demo of the embedded code results. I’ll include future code results here as well.

http://www.4rca.com/index_test.html

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home