hi all,
I was developing a blog where I used classes. but I met a problem when I try to include config.php to my classes and use global variables in the methodes. Normally without classes it works pretty much well.
here is the code
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of BlogComment
*
* @author Menuka
*/
include 'DBhandling.php';
include 'config.php';
// $dob = new Dbhandling($dbHost,$dbUser,$dbName,$dbPass);
class BlogComment {
private $postId;
private $name_cm;
private $email_cm;
private $website_cm;
private $comment_cm;
function __construct($inpostId=null, $inname_cm=null,$inemail_cm=nul,$inwebsite_cm=null,$incomment=null) {
//initialization of properties.
if(!empty ($inpostId)){
$this->postId=$inpostId;//set postid and commentid toobject properties.
}
if(!empty ($inname_cm)){
$this->name_cm=$inname_cm;
}
if(!empty ($inemail_cm)){
$this->email_cm=$inemail_cm;
}
if(!empty ($inwebsite_cm)){
$this->website_cm=$inwebsite_cm;
}
if(!empty ($incomment)){
$this->comment_cm=$incomment;
}
}
//this function is used to validate commentid
//check the postid and it's commentsid get the last commentid and increment then send it to database.
//$var, $name, $email, $website, $comment
function validateCommentIDandSave($inpostId, $inname_cm, $inemail_cm, $inwebsite_cm, $incomment){
$dob = new DBhandling('localhost','root','classdb','needsome1');
$dob->connectDB();
if(!empty ($inpostId)){
$qry="select MAX(commentId) from classdb.usercomments where postId='$inpostId'";
$sql_qry = mysql_query($qry) or die(mysql_error());
$row=mysql_fetch_array($sql_qry);
$cm_id=$row[0];
if(!$sql_qry){
$cm_id=1;
}else{
$cm_id=$cm_id+1;
$ob = new BlogComment();
$ob->saveComment($inpostId, $cm_id, $inname_cm, $inemail_cm, $inwebsite_cm, $incomment);
}
} else{
echo "invalide link. there is no post from this id";
}
$dob->closeDB();
}
function saveComment($inpostId=null,$incommentId=null, $inname_cm=null,$inemail_cm=null,$inwebsite_cm=null,$incomment=null){
$db=new DBhandling('localhost','root','classdb','needsome1');
$db->connectDB();
$qury="insert into classdb.usercomments(postid,commentId,name_cm,email_cm,website_cm,comment_cm,dateComment_cm,allow_cm) values ('$inpostId','$incommentId','$inname_cm','$inemail_cm','$inwebsite_cm','$incomment',CURRENT_TIMESTAMP , '0')";
if (!mysql_query($qury))
{
die('Error: ' . mysql_error());
}
header( 'Location: ../Blog/redirect.html' ) ;
echo "<br>";
//you can set another html page or java script show message.
$db->closeDB();
}
//put your code here
}
?>
I try to replace the constructor with parameters in order to passe values from config.php to the constuctor then it doesn't wor
so I just give the real values directly. but it is not good programming to declare password and other stuff alway here and there
so how can I solve this
$dob = new DBhandling('localhost','root','classdb','needsome1');
here is code in the config.php
global $dbHost;
global $dbUser;
global $dbName;
global $dbPass;
$dbHost='localhost';
$dbUser='root';
$dbName='classdb';
$dbPass='needsome1';
thx in advance...