Using databases in a class
Posted: Thu Jan 24, 2008 5:52 am
I'm comparatively new to writing my own classes. I've never come across this problem before and needed some guidance.
I'm writing a class to replace certain keywords on an article page with internal links to the rest of the site. Here's what I have so far which works.
Currently I'm using Lorem Ipsum text in place of an actual article hence the keyword 'Phasellus'. What I want to do now is take a database of keywords and links and use that to populate the $keyword and $link variables (throwing the whole thing in to a loop). My question is; what is the best way of connecting to a database from a function within a class?
I'm writing a class to replace certain keywords on an article page with internal links to the rest of the site. Here's what I have so far which works.
Code: Select all
<?php
class replace {
var $text = "";
function ReplaceString($text) {
$keyword = "Phasellus";
$link = "home.php";
$replace = "<a href=\"$link\">$keyword</a>";
$text = str_replace($keyword, $replace, $text);
echo $text;
}
}
?>