Comment CMS Script Troubles: [OOP Style]

Hello again from MrMaverick,

I am having trouble diagnosing and fixing this script that I modified from another person’s comment C.M.S. tutorial.

The purpose (hopefully) is for this script to connect to a database, retrieve all of the comment info stored, store them into an associative array (I guess…), then, using a while loop, print out each comment until there are no more rows to print. At least, that is the idea I got from looking at it. But unfortunately it’s not working out so well.

Here is probably some helpful information for you experts out there.

PHP Error Information:

Notice: Undefined index: title in ~\cmtCMS.php on line 33
Notice: Undefined index: cmt_sum in ~\cmtCMS.php on line 35
Notice: Undefined variable: cmt_entry_display in ~\cmtCMS.php on line 53

CODE: cmtCMS.php
[php]

<?php class cmtCMS { var $host; var $user; var $pass; var $database; //This Function will run the code to display all comments on the website submitted by users. public function display_public() { //Formulating the MySQL Loading Query $query = "SELECT * FROM `comment_table` ORDER BY `cmt_date` DESC LIMIT 6"; //Query the Database $result = mysql_query($query); // This shows the query sent to MySQL, and the error. Comment out when done debugging. if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } //If we have rows to fetch... if ( $result == true && mysql_num_rows($result) > 0 ) { // If there ARE rows to fetch, as long as we have results from the queries, keep inserting them into the array while ( $array = mysql_fetch_assoc($result) ) { /* Insert Database Information into variables from the Associative Array We are assuming that the data being pulled from the database is clean of any malicious code */ $cmt_title = $array['title']; $cmt_raw_date = $array['cmt_date']; $cmt_subject = $array['cmt_sum']; $cmt_data = $array['cmt_data']; $username = $array['username']; $rating = $array['cmt_rating']; //Format the MySQL Date into the correct format we desire for each post $cmt_date_proc = date('F jS, Y \a\t g:i A', strtotime( $cmt_raw_date )); //Display the Post Data $cmt_entry_display .= <<<CMT_ENTRY_DISPLAY

$cmt_title

Posted By: $username on $cmt_date_proc [$cmt_subject]

$cmt_data

| Average Ratings Go Here: ($rating) |

CMT_ENTRY_DISPLAY; } } else{ $cmt_entry_display .= <<<CMT_ENTRY_DISPLAY

No Comments To Load

Comment Info Appears Here

This is a hardcoded comment. Either someone didn't post anything, or someone managed to hack the database server to DROP ALL THINGS! Ah well, programmers live and learn (:P).

| Average Ratings Would Go Here: (#.##) |

CMT_ENTRY_DISPLAY; } //END display_public } //This Function will instruct PHP to connect to the database where the comments are stored. public function connect() { mysql_connect($this->host,$this->user,$this->pass) or die("Connection Error! Reason: " . mysql_error()); mysql_select_db($this->database) or die("Database Error! Reason: " . mysql_error()); } } ?>

[/php]

While this is where the errors are being reported by PHP, this may not be the source. So I have included the PHP code on the page that accesses this script.

CODE: dd-cmt.php [Partial]
[php]

<?php echo "Comments are unavailable to view because I am an idiot. That is all."; //Include The Comment Content Management System ONLY ONCE include_once('content/cmtCMS.php'); //Initialize the new class of variables to use in the Comment CMS $cmt_obj = new cmtCMS(); //These are used as arguments for the connection function inside $cmt_obj->host = 'localhost'; $cmt_obj->user = 'username'; $cmt_obj->pass = 'password'; $cmt_obj->database = 'database'; $cmt_obj->connect(); //Now just say to users that this is where actual comments will start $cmt_obj->display_public(); ?>
		<?php include "footer/footer.php" ?>

[/php]

I got my code working like I want it to. If you still have suggestions though as to how it can be improved or streamlined for performance or stability, I beg you to post it.

The key portions of code I changed were inside the display_public() function.

[php]
//If we have rows to fetch…
if ( $result == true && mysql_num_rows($result) > 0 ) {
// If there ARE rows to fetch, as long as we have results from the queries, keep inserting them into the array
while ( $row = mysql_fetch_assoc($result) ) {

		/* Insert Database Information into variables from the Associateive Array
		We are assuming that the data being pulled from the database is clean of any malicious code */
		$cmt_title = $row['cmt_title'];
		$cmt_raw_date = $row['cmt_date'];
		$cmt_subject = $row['cmt_sub'];
		$cmt_data = $row['cmt_data'];
		$username = $row['username'];
		$rating = $row['cmt_rating'];
		
		//Format the MySQL Date into the correct format we desire for each post
		$cmt_date_proc = date('F jS, Y \a\t g:i A', strtotime( $cmt_raw_date ));
		
		//Display the Post Data
	
		print <<<POST
		
		<div class="cmt">
			<h4 class="cmtlabel">$cmt_title</h4>
			<p class="cmtinfo">Posted By: <b>$username</b> on <b>$cmt_date_proc [$cmt_subject]</p>
			<p class="cmtpost">$cmt_data</p>
			<p class="cmtlabel">| Average Ratings Go Here: ($rating) |</p>
		</div>

POST;

	}
	
	//Close the Connection to the Database
	mysql_free_result($result);
}
	else{
	print <<<POST
	
	<div class="cmt">
		<h4 class="cmtlabel">No Comments To Load</h4>
		<p class="cmtinfo">Comment Info Appears Here</p>
		<p class="cmtpost">This is a hardcoded comment. Either someone didn't post anything, or someone managed to hack the database server to DROP ALL THINGS! Ah well, programmers live and learn (:P). </p>
		<p class="cmtlabel">| Average Ratings Would Go Here: (#.##) |</p>
	</div>

POST;

		}
	//END display_public
	}

[/php]

It seems that I was actually storing the output all into one variable, but it was never being printed. So instead, I used the print() function (I think it’s a function) to write the output to the newly generated HTML file.

Oh, don’t worry about the tiny changes in some of the database columns. Remember that anyone looking here GUARANTEED will have a setup different than mine, so those variables in the associative array could be completely different.

Sponsor our Newsletter | Privacy Policy | Terms of Service