homesupportload from a local database via php + mysql [tutorial]

loading data from your database

two different example scripts have been provided to help you load news feed data from your database these are all designed based on the database created inadvanced database structure. the two files newslist.php and article.php work hand in hand and are linked together using two variables in each file called $individualarticlepage and $listpage respectively.

[newslist.php ]

the first one callednewslist.phpis used to produce a list of news items and outputs each item in a html table with pre-configured class names for css styling again this can be tailored to your own needs:




include_once '../classes/db.php';

$link = mysql_connect(db::gethost(), db::getuser(), db::getpassword());
@mysql_select_db(db::getdatabasename()) or die("cannot find database");

$limit = 10; //number of news items to display
$individualarticlepage = "article.php";

$query = "select * from feed ".getquerycriteria()."order by publisheddate desc limit 0,".$limit;
if(!$result = mysql_query($query))echo "invalid query: " . $query;

$flip=false;
while($row = mysql_fetch_assoc($result)){
$tableclass=" class='alternatingtable'";
if($flip)$tableclass="";
echo "\n";
echo "" . $row['headline'] . "\n";
echo "" . $row['extract'] . "\n";
echo "" . $row['publisheddate'] . "\n";
echo "\n";
$flip = !$flip;
}

mysql_close($link);

function getquerycriteria(){
$categoryid = $_get['categoryid'];
$wherequery = "";
if(isset($categoryid)){
$wherequery = "where id in (select feedid from feedcategories where categoryid=" . $categoryid . ") ";
}

return $wherequery;
}
?>

[jump to top]

[article.php ]

the second one calledarticle.phpis used to produce a full news item using a html table for structuring:

include_once '../classes/db.php';

$link = mysql_connect(db::gethost(), db::getuser(), db::getpassword());
@mysql_select_db(db::getdatabasename()) or die("cannot find database");

$id = $_get['id'];
$listpage = "newslist.php";

//feed query
$query = "select * from feed where id =" . $id;
$result = mysql_query($query);
$feedrow = mysql_fetch_assoc($result);

//photo query
$subquery = "(select photoid from feedphotos
where feedid =". $id .")";
$query = "select * from photos where id in " . $subquery;
$result = mysql_query($query);
$imagerow = mysql_fetch_assoc($result);

//category query
$subquery = "(select categoryid from feedcategories
where feedid =". $id .")";
$query = "select * from categories where id in " . $subquery;
$categoryresult = mysql_query($query);

//comments query
$subquery = "(select commentid from feedcomments
where feedid =". $id .")";
$query = "select * from comments where id in " . $subquery;
$commentresult = mysql_query($query);

echo "\n";
echo "

" . $feedrow['headline'] . "

\n";
echo getimagehtml() . $feedrow['text'] . "\n";
echo $feedrow['publisheddate'] . "\n";
echo "

" . $feedrow['byline'] . "

\n";
echo getcategorieshtml() . "\n";
echo "

" . getcommentshtml() . "

\n";
echo "\n";

mysql_close($link);

function getimagehtml(){
global $imagerow;
$imageurl = $imagerow['largeurl'];
$imagealt = htmlspecialchars($imagerow['htmlalt'], ent_quotes);
return "".$imagealt.""; } function getcategorieshtml(){ global $categoryresult, $listpage; $categorieslist = ""; while($row = mysql_fetch_assoc($categoryresult)){ if(!empty($categorieslist))$categorieslist = $categorieslist . ", " . $row['name'] . "\n"; else $categorieslist = "" . $row['name'] . "\n"; } return $categorieslist; } function getcommentshtml(){ global $commentresult; $commentslist = "\n"; while($row = mysql_fetch_assoc($commentresult)){ $commentslist = $commentslist . "\n". "" . $row['name'] . "\n". "" . $row['text'] . "\n". "" . $row['location'] . "\n". "" . $row['postdate'] . "\n". "\n"; } $commentslist = $commentslist . "\n"; return $commentslist;
} ?>

[jump to top]