JavaScript Marquee v3

Demo 7: Server-Side Scripting

Previous Demo - Marquee Page - Marquee FAQ

Back Forward
<head>
[...]
<?php
// Construct JavaScript Message Array from MySQL result //

	// Connect to MySQL server //
	$db_server = 'localhost';
	$db_name = 'weblog';
	$db_user = 'username';
	$db_password = 'password';
	$db_link = mysql_connect($db_server, $db_user, $db_password) or die ('Cannot connect to the database because: ' . mysql_error());
	mysql_select_db($db_name);

	// Get Last 10 LazyLog Entries //
	$result = mysql_query('SELECT post_id,event,description FROM journal ORDER BY datetime DESC LIMIT 0,10;');

	// Build array of HTML messages //
	$message_array = array();
	while ($row = mysql_fetch_array($result, MYSQL_NUM)) {

		// Remove existing HTML (but keep spacing) //
		$row[2] = preg_replace('/<(?:.*?)>/', ' ', $row[2]);

		// Use only first words of post in message //
		$short_body = '';
		foreach (explode(' ',$row[2]) as $word) {
			$short_body .= "$word ";
			if (strlen($short_body) >= 70) {break 1;} 
		}
		$short_body = rtrim($short_body);
		$short_body .= '...';

		// Don't forget to escape those apostrophes! //
		$short_body = addslashes($short_body);
		$row[1] = addslashes($row[1]);

		// Format HTML message //
		$message_array[] = '<a href="http://www.ruinsofmorning.net/?p=entry&eid=' . $row[0] . '"><big>' . $row[1]. '</big></a><br />' . $short_body;
	}

	array_unshift($message_array, '<big><b>Latest LazyLog Posts...</b></big>');

	// Reconstruct the array in JavaScript syntax and output to browser:
	echo '<script type="text/javascript">';
	echo "var msgarray=new Array ('";
	echo join("','",$message_array);
	echo "')";
	echo '</script>';
?>

<!-- Now the external JavaScript with the 'msgarray' definition stripped out -->
<script type="text/javascript" src="demo7.js"></script>
[...]
</head>

ruinsofmoring.net