Bcountdown Extenssion Not Working In Foreach Loop

Hi guys,

I’m new here. I have been facing a problem related to bcountdown since morning.

I’m using this extension http://www.yiiframework.com/extension/ to show countdown in front of every record under foreach loop but it displays only once in the first record and rest of the countdowns remaining blank.

Could anyone please help me on this. Thanks in advance.

Sorry for my english :P

nobody has got magic wand, paste your code and fix the link to the extetion

Alright mate. Here’s my code :




foreach ($model as $data) {

    $date = explode('-', $data->date);

    $time = explode(':', $data->time);

    $this->widget('application.extensions.bcountdown.BCountdown', array(

        'title' => false, // Title

        'message' => 'Upcoming Event', // message for user

        'isDark' => false, 

        'year' => $date[0],

        'month' => $date[1],

        'day' => $date[2],

        'hour' => $time[0],

        'min' => $time[1],

        'sec' => $time[2],

    ));

}



and extension is http://www.yiiframework.com/extension/bcountdown/

Not need to use foreach simply get data like this -

eg.


$model = new YourModel;

$data = $model->example;

Thanks Rohit, But my question is that i want to display countdown timer in foreach loop by using this extension. Like if i have more than 10 records than it shows only first countdown rest of all are getting blank. Why is this happening?

Dude bcountdown is based on lwtCountdown js plugins. so if you will use in the loop it will impact on js ids, functions and variables. so you can use single time in a page.

if you want to use two or more times, you need to cal and modify countDown function every time.

eg.


$('#example1').countDown();

$('#example2').countDown();

you are doing it right the jquery plugin page says you have to create multiple instance for multiple timers

I have patched the ext here replace your BCountdown.php




<?php

/**

 * BCountdown class file.

 *

 * PHP Version 5.1

 * 

 * @package  Widget

 * @author   FBurhan <sefburhan@gmail.com>

 * @license  http://www.opensource.org/licenses/mit-license.php MIT

 * @link     http://www.yiiframework.com/user/62626/

 */

 

class BCountdown extends CWidget{

	

	/**

	 * @var html element	

	 */

	public $el = "yii-count-down";


	/**

	 * @var array the options for  BCountdown Widget

	 */

	public $options = array();


	/**

	 * @var string the title of the Countdown. if NOT assigned , the default value("UNDER CONSTRUCTION") will be shown.

	 */

	public $title; 

	

	/**

	 * @var string the message of the Countdown as to show the reason for site down. if NOT assigned , the default value("Stay tuned for news and updates") will be shown.

	 */

	public $message;      

	

	/**

	 * @var string the isDark of the Countdown . the default is Light Gray.

	 */

	public $isDark=false;   

		 

	// Timestamp

	/**

	 * @var string the year of the Countdown . if NOT assigned ,the default is 0.

	 */

	public $year='0';

	

	/**

	 * @var string the month of the Countdown . if NOT assigned ,the default is 0.

	 */

	public $month='0';


	/**

	 * @var string the day of the Countdown . if NOT assigned ,the default is 0.

	 */

		public $day='0';

	/**

	 * @var string the hour of the Countdown . if NOT assigned ,the default is 0.

	 */

	public $hour='0';

	

	/**

	 * @var string the min of the Countdown . if NOT assigned ,the default is 0.

	 */

	public $min='0';

	

	/**

	 * @var string the sec of the Countdown . if NOT assigned ,the default is 0.

	 */

	public $sec='0';

	

	/**

	 * @var string the cssFile of the Countdown for Css file.

	 */

	public $cssFile;


	/**

	 * @var string the jsFile of the Countdown for Javascript file.

	 */

	public $jsFile;

		

	public function init()

	{

		// Put togehther options for plugin

		$path = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('ext.bcountdown.src', -1, false));

		

		$this->jsFile = $path . '/js/jquery.lwtCountdown-1.0.js';

		if ($this->isDark==false)

			$this->cssFile = $path . '/style/main.css';

		else

			$this->cssFile = $path . '/style/dark.css'; 


		$cs = Yii::app()->clientScript;

		$cs->registerScriptFile($this->jsFile);

		$cs->registerCssFile($this->cssFile);

	   

		$script = '$("#'.$this->el.'").countDown({

			targetDate: {

				"year":"'.$this->year.'",

				"month":"'.$this->month.'",

				"day":"'.$this->day.'",

				"hour":"'.$this->hour.'",

				"min":"'.$this->min.'",

				"sec":"'.$this->sec.'"

			}

		});'; 

		$cs->registerScript($this->el, $script);

	}

		

	

	/**

	 * Run this widget.

	 * This method registers necessary javascript and renders the needed HTML code.

	 */

	public function run()

	{

		 

		if ($this->title == "")

			$this->title = "UNDER CONSTRUCTION";

		

		if ($this->message == "")

			$this->message = "Stay tuned for news and updates";

		 

		echo '<div id="container">

			<h1>'.$this->title.'</h1>

			<h2 class="subtitle">'.$this->message.'</h2>

			<div id="'.$this->el.'">

				<div class="dash weeks_dash">

					<span class="dash_title">weeks</span>

					<div class="digit">0</div>

					<div class="digit">0</div>

				</div>


				<div class="dash days_dash">

					<span class="dash_title">days</span>

					<div class="digit">0</div>

					<div class="digit">0</div>

				</div>


				<div class="dash hours_dash">

					<span class="dash_title">hours</span>

					<div class="digit">0</div>

					<div class="digit">0</div>

				</div>


				<div class="dash minutes_dash">

					<span class="dash_title">minutes</span>

					<div class="digit">0</div>

					<div class="digit">0</div>

				</div>


				<div class="dash seconds_dash">

					<span class="dash_title">seconds</span>

					<div class="digit">0</div>

					<div class="digit">0</div>

				</div>

			</div>

		</div>';

	}


}

and then you can do your loop here




<?php

foreach ($model as $data) {

    $date = explode('-', $data->date);

    $time = explode(':', $data->time);

    $this->widget('application.extensions.bcountdown.BCountdown', array(

        'el'=> 'count-down'.$data->id,

        'title' => false, // Title

        'message' => 'Upcoming Event', // message for user

        'isDark' => false, 

        'year' => $date[0],

        'month' => $date[1],

        'day' => $date[2],

        'hour' => $time[0],

        'min' => $time[1],

        'sec' => $time[2],

    ));

}



You nailed it mate… Thank you :)