PHP and Top N Things Example
Here is a PHP example for the top N items. There are a lot of bonuses to using PHP as opposed to a perl script. The PHP can be embedded into your HTML easily. Plus common parts can be pulled out into separate files that you reference. In the example below I have a file that keeps all of my DB connection information. This way in the future I will only have to make a change in one file if I need to point to a different database.
To See the PostgreSQL function see yesterdays post. Here
http://www.4rca.com/cgi-bin/top_ten_views.php
I had to remove all of the \> and \< symbols so that the code would print.
?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');
?
html
head title Top Ten N Example PHP /title
/head
body
DIV style="position:absolute;left:180;top:130px;z-index:4" align="center"
?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));
$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 Users /td td Topics /td td Views /td /tr ";
for ($k=0;$k =$rows-1;$k++) {
print " tr ";
for ($j=0;$j =2;$j++) {
echo " td ",$query_info[$k][$j]," /td ";
}
print " /tr ";
}
?
/table
/DIV
/body
/html

0 Comments:
Post a Comment
Links to this post:
Create a Link
<< Home