Hi community,

I'm developing my first module, is a simple weather block, thaht brings the information from yahoo rss weather channel. I create the followings function in .module:

simpleweather_help
simpleweather_perm
simpleweather_block
simpleweather_admin
simpleweather_all : is empty
simpleweather_menu.

When i installed, and assign the block to a certain region , when the page refresh start to fill with some print_r stuff, a never ending variables, and my block layout damages.

Every is back to normal when i unistall the module.

I need some help please.
Thks.

Comments

gpk’s picture

It looks like there is a print_r() statement inside simpleweather_block() ?

gpk
----
www.alexoria.co.uk

nfajardo’s picture

Hi, thks for your help, i check the code i didnt found any print_r stuff. I never see that before, i forget some issu, but in the next answer you can see it.

Thks,
i appreciated your help.

naveenpl’s picture

You have a print_r() function call and this function will throw the output(array index and values) to the browser.
I suppose you need this result and you need to display this output.

$output .= theme('table', $header, $array_content);
here $header is the heading content, if you need to list the content. $array_content is the informations stored in the array. Now your result will be displayed in the theme, will not damage your layout.

Hope this will help
Cheers

nfajardo’s picture

Thnks for your concern, i really apreciated it. here is the code: I forget something: not everything backs to normal, some of assigns block, change the region: for example, if a x block is assign to top header(this was created in template.php) region, after install the module and assign my simpleweather block, messup with the print_r never-ending stuff, unistall the module, the x block is in another region...

I hope this is helpful,
and i really apreciate yor help


/**
* Display help and module information
* @param section which section of the site we're displaying help
* @return help text for section
*/
define('SIMPLEWEATHER_LIST', 0);
define('SIMPLEWEATHER_LAN_LIST', 0);
function simpleweather_help($section='') {

  $output = '';

  switch ($section) {
    case "admin/help#simpleweather":
      $output = '<p>'.  t("Displays links to nodes created on this date"). '</p>';
      break;
  }

  return $output;
} // function onthisdate_help

/**
* Valid permissions for this module
* @return array An array of valid permissions for the onthisdate module
*/

function simpleweather_perm() {
return array('access simpleweather', 'create simpleweather', 'administer simpleweather');
} // function onthisdate_perm()

/**
* Generate HTML for the onthisdate block
* @param op the operation from the URL
* @param delta offset
* @returns block HTML
*/
function simpleweather_block($op='list', $delta=0) {
  // listing of blocks, such as on the admin/block page
    
	$path = drupal_get_path('module', 'simpleweather');
    include_once("./$path/class.weather.php");
    include_once("./$path/class.xml.parser.php");
	
  if ($op == "list") {
     $block[0]["info"] = t('Simple Weather');
     return $block;
  }
  else if ($op == 'view') {

	
	$var_locale = variable_get("simpleweather_path", "localidad");
	$var_type = variable_get("simpleweather_type", "c");
	$var_vocabulary = variable_get("simpleweather_vocabulary", "5");
	$cityCode = "COXX0004";		

	$arr = split("/",$_SERVER['REQUEST_URI']);

	for($i=0;$i<count($arr);$i++ )
	{
		 if($arr[$i]==$var_locale)
		 {
		 	$i++;
			 $city = $arr[$i];
		 	$arr2 = simpleweather_get_code($city,$var_vocabulary);
		 }
	}

	
	if(isset($arr2) && count($arr2)>0)
	$cityCode = $arr2[0]->name;
	
	$weather_chile = new weather($cityCode, 0, $var_type);
	$weather_chile->parse();	

	
	$var_lan = variable_get("simpleweather_lan", "en");
	if($var_lan=="es")
	{
		$weather_text = simpleweather_spanish_text($weather_chile->forecast['CURRENT']['CODE']);
	}
	else
	{
		$weather_text = $weather_chile->forecast['CURRENT']['TEXT'];
	}

	$block_content ="<div id='simpleweather'>";
	$block_content ="<div id='simpleweathertitle'>".variable_get("simpleweather_title", "Today Weather")."</div>";
	if($weather_chile->forecast['CURRENT']['CODE']!=""){
		$block_content .="<div id='wcode'><img src='http://us.i1.yimg.com/us.yimg.com/i/us/we/52/".$weather_chile->forecast['CURRENT']['CODE'].".gif' /></div>";
		$block_content .="<div id='wcurrent'>".$weather_chile->forecast['CURRENT']['TEMP']."&deg;".strtoupper($var_type)."</div>";  
	}
	else
	{
		$block_content .="<div id='wcurrentna'>No Disponible</div>";      

	}

	

	$block_content .="<div id='wcity'> ".$weather_chile->forecast['CITY']."</div>";
	$block_content .="<div id='wtext'>".$weather_text."</div>";
	//$block_content .="<div id='wday'>".$weather_chile->forecast['CURRENT']['DAY']."</div>";
	//$block_content .="<div id='wdate'>".$weather_chile->forecast['CURRENT']['DATE']."</div>";
	$block_content .="<div id='wlow'>Min ".$weather_chile->forecast[0]['LOW']."&deg;".strtoupper($var_type)."</div>";
	$block_content .="<div id='whigh'>Max ".$weather_chile->forecast[0]['HIGH']."&deg;".strtoupper($var_type)."</div>";


	
	$block_content .="</div>";
	
	
	
  	$block['subject'] = 'Simple Weather';
  	$block['content'] = $block_content;

    

        return $block;

  }

 
} // end function onthisdate_block

function simpleweather_admin() {
      $form['simpleweather_title'] = array(
    '#type' => 'textfield',
    '#title' => t('This the title of the block'),
    '#default_value' => variable_get('simpleweather_title', 'Weather'),
    '#size' => 50,
    '#maxlength' => 255,
    '#description' => t("Title of the block")
  );
        $form['simpleweather_vocabulary'] = array(
    '#type' => 'textfield',
    '#title' => t('Vocabulary of the cities'),
    '#default_value' => variable_get('simpleweather_vocabulary', ''),
    '#size' => 50,
    '#maxlength' => 255,
    '#description' => t("Vocabulary")
  );
      $view_types = array(
                        'c' => t('Celsius'),
                        'f' => t('Farehenit'),
                        );
    $form['simpleweather_type'] = array(
    '#type' => 'radios',
    '#title' => t('Please select the weather type (C or F)'),
    '#default_value' => variable_get('simpleweather_type', SIMPLEWEATHER_LIST),
	'#options' => $view_types,
    '#description' => t("Url of the web service.")
  );
      $form['simpleweather_defaultcode'] = array(
    '#type' => 'textfield',
    '#title' => t('Default Code'),
    '#default_value' => variable_get('simpleweather_defaultcode', 'COXX0004'),
    '#size' => 50,
    '#maxlength' => 255,
    '#description' => t("Codigo predeterminado")
  );
      $view_lan = array(
                        'en' => t('English'),
                        'es' => t('Spanish'),
                        );
    $form['simpleweather_lan'] = array(
    '#type' => 'radios',
    '#title' => t('Please select the languaje'),
    '#default_value' => variable_get('simpleweather_lan', SIMPLEWEATHER_LAN_LIST),
	'#options' => $view_lan,
    '#description' => t("Languaje of the service")
  );
      $form['simpleweather_path'] = array(
    '#type' => 'textfield',
    '#title' => t('This is for view porpouses'),
    '#default_value' => variable_get('simpleweather_path', 'localidad'),
    '#size' => 50,
    '#maxlength' => 255,
    '#description' => t("Name of the path variable.")
  );


  return system_settings_form($form);
}
function simpleweather_all() {


}
function simpleweather_menu() {

  $items = array();

  //this was created earlier in tutorial 7.
  $items[] = array(
    'path' => 'admin/settings/simpleweather',
    'title' => t('Simple Weather Settings'),
    'callback' => 'drupal_get_form',
    'callback arguments' => 'simpleweather_admin',
    'access' => user_access('access administration pages'),
    'type' => MENU_NORMAL_ITEM,
   );

  //this is added for this current tutorial.
  $items[] = array(
    'path' => 'simpleweather',
    'title' => t('Simple Weather'),
    'callback' => 'simpleweather_all',
    'access' => user_access('access simpleweather content'),
    'type' => MENU_CALLBACK
  );

  return $items;
}
function simpleweather_get_code($name,$vid)
{
 $db_result = db_query("SELECT ts.name as name FROM {term_data} t, {term_synonym} ts, {vocabulary} v WHERE t.tid=ts.tid AND v.vid=t.vid AND t.name LIKE '".$name."'  AND v.vid='".$vid."'");
  while ($term = db_fetch_object($db_result)) {
    $result[] = $term;
  }
return($result);

}
function simpleweather_spanish_text($valor)
{
	$arr = array("0"=>"tornado","1"=>"tormenta tropical","2"=>"huracan","3"=>"tormenta electica severa","4"=>"tormenta electrica","5"=>"lluvia y nieve","6"=>"lluvia y aguanieve","7"=>"nieve y aguanieve","8"=>"llovizna helada","9"=>"llovizna","10"=>"lluvia helada","11"=>"duchas","12"=>"duchas","13"=>"rafagas de nieve","14"=>"Duchas de nieve suave","15"=>"nieve","16"=>"nieve","17"=>"granizo","18"=>"llovizna","19"=>"polvo","20"=>"Neblina","21"=>"calina","22"=>"ahumado","23"=>"blustery","24"=>"viento","25"=>"frio","26"=>"nublado","27"=>"nublado (noche)","28"=>"nublado (dia)","29"=>"parte nublado (noche)","30"=>"parte nublado (dia)","31"=>"despejado (noche)","32"=>"soleado","33"=>"normal (noche)","34"=>"normal (dia)","35"=>"lluvia y granizo (noche)","36"=>"Caliente","37"=>"lluvias aisladas","38"=>"lluvias dispersas","39"=>"lluvias dispersas","40"=>"lluvias dispersas","41"=>"nieve pesada","42"=>"nieve dispersa","43"=>"nieve pesada","44"=>"parte nublado","41"=>"duchas electricas","41"=>"duchas de nieve","41"=>"duchas electricas aisladas","3200"=>"No disponible");

return($arr[$valor]);

}
gpk’s picture

OK this all very odd!

Firstly, your code above clearly doesn't include print_r(). Nor does Drupal core. Some contrib modules do, e.g. devel, but I think you would know if you were using devel's print_r(). Is it in a custom block?

Secondly, if you look at your HTML page source, you should be able to work out where the print_r() is being called e.g. if it is in a block you should see a div with revealing id information.

Thirdly, I did once have problems with blocks not appearing where I expected. I never got to the bottom of it; partly it was just a matter of having different block positions in two almost identical themes, but there was also something else funny going on. A quick look at the {blocks} table may be revealing. You may even be able to delete stuff from that to have it regenerated (but please backup first!!!!).

Also if you are viewing the site in one browser as an Anonymous user and have caching on then block changes may not show up immediately (I'm not sure if changing block settings forces a cache clear - maybe it does, I don't know).

gpk
----
www.alexoria.co.uk

nfajardo’s picture

Ok, thanks for your advice, the site has devel module, and the backtracing option is on.

I going to revise the devel and the block cache module, perhaps the problem is there...if i found something i will tell you

thks again!!!

gpk’s picture

Ah, block cache, that sounds highly relevant ..!
Good luck,

gpk
----
www.alexoria.co.uk

nfajardo’s picture

Sorry, but it was my fault after all. The never ending print_r stuff was causing by devel node access. When he found an error i think it backtracking all, the error was caused by the other two files that i include in my module. They have ONE BLANK SPACE AND ONE ENTER. That error cause the famous error:

warning: Cannot modify header information - headers already sent by (output started at /home/www/mysite/sites/all/modules/simpleweather/class.weather.php:2) in /home/www/mysite/includes/common.inc on line 309.

When i unistall devel and devel node access, drupal show me the error.

I learn my lesson, take a look the code, even if is mine.

Thanks a lot.

gpk’s picture

Glad you found it!

It's generally suggested not to include the closing ?> if possible, to avoid this problem.

gpk
----
www.alexoria.co.uk