Archive for August, 2010

Adding multiple databases to the Application.ini in Zend Framework

2

application.ini

resources.multidb.db1.adapter        = "mysqli"
resources.multidb.db1.isDefaultTableAdapter = true
resources.multidb.db1.host           = "localhost"
resources.multidb.db1.username       = "xx"
resources.multidb.db1.password       = "xx"
resources.multidb.db1.dbname         = "zftutorial"

resources.multidb.db2.adapter        = "mysqli"
resources.multidb.db2.isDefaultTableAdapter = false
resources.multidb.db2.host           = "localhost"
resources.multidb.db2.username       = "xx"
resources.multidb.db2.password       = "xx"
resources.multidb.db2.dbname         = "bboard"

bootstrap.php


protected function _initDatabase ()
    {
        $resource = $this->getPluginResource('multidb');
        $resource->init();

        Zend_Registry::set('db1', $resource->getDb('db1'));
        Zend_Registry::set('db2', $resource->getDb('db2'));
    }

Use by calling:

$db = Zend_Registry::get('db1');
$result = $db->fetchAll ( "select * from mytable", array ());

See also:
http://forums.zend.com/viewtopic.php?f=69&t=5615

Reporting Live Status of Livestreams with PHP & jQuery

4

DEMO

References:
- Livestream Channel API 2.0
- Zend_Json
- jQuery Ajax
- Livestream

islive.php

<?php
/*
ini_set('display_errors', 1);
error_reporting(E_ALL);
*/

/*
 * http://framework.zend.com/manual/en/zend.json.html
 */
include_once 'Json.php';

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

try {
	$url = '';
	switch ($_GET['name']) {
		case 'cowclan':
			$url = 'http://xcowclanx.channel-api.livestream-api.com/2.0/livestatus';
			break;
		case 'rebootgpf':
			$url = 'http://xrebootgpfx.channel-api.livestream-api.com/2.0/livestatus';
			break;
		case 'clgaming':
			$url = 'http://xclgamex.channel-api.livestream-api.com/2.0/livestatus';
			break;
	}

	function isLive($url) {
		$data = file_get_contents($url);
		$xml = new SimpleXMLElement($data);

		$nsdata = $xml->children('http://api.channel.livestream.com/2.0');

		$list = array();
		foreach($nsdata as $c) {
			$list[$c->getName()] = (string)$c;
		}

		return $_GET['callback'].'('.Zend_Json::encode($list).')';
	}

	if (!empty($url)) {
		echo isLive($url);
	} else {
		echo $_GET['callback'].'('.Zend_Json::encode(array('error'=>'404')).')';
	}
} catch (Exception $e) {
	echo $_GET['callback'].'('.Zend_Json::encode(array('error'=>'503')).')';
}

$err = error_get_last();
if (!empty($err)) {
	echo $_GET['callback'].'('.Zend_Json::encode(array('error'=>'500')).')';
}
?>

Code Snippet from the DEMO

function updateStreamInfo(channel, element, name) {
$.ajax({
	url : "http://ingol.nl/lol/livestream/islive.php?name="+channel,
	type : "GET",
	dataType : "jsonp",
	success : function(data, textStatus, XMLHttpRequest){
		var color = {'color':'#FFFFFF'};
		if (data.isLive && data.isLive == 'true') {
			color = {'color':'#FF0000'};
		} else {
			color = {'color':'#FFFFFF'};
		}

		var viewercount = 'undefined' === typeof(data.currentViewerCount) ? '-.-' : data.currentViewerCount;
		var link = $('<a/>').attr({'href':'http://livestream.com/'+channel,"target":"livestream"}).css(color)
			.html('LIVESTREAM '+name.toUpperCase()+' ('+viewercount+')');
		element.html(link);
	},
	error : function(XMLHttpRequest, textStatus, errorThrown){
		// console.debug(XMLHttpRequest);
	}
});
}

function isLiveStatus(name,channel) {
	var minutes = 5;
 	updateStreamInfo(channel,$('#'+name), name);
	window.setInterval('updateStreamInfo("'+channel+'",$("#'+name+'"), "'+name+'")', 1000 * 60 * minutes);
}

$(function(){
 	isLiveStatus('rebootgpf','rebootgpf');
 	isLiveStatus('clgaming','clgame');
 	isLiveStatus('cowclan','cowclan');
 	isLiveStatus('Team-AON','teamaon');
 	$('#popup').click(function(){
		window.open('index.html','livestreams','width=400,height=200,menubar=no,status=no,location=no,toolbar=no,scrollbars=no');
 	});
});
Go to Top