PHP block examples

bertboerland@ww... - February 8, 2005 - 20:20

In this thread one can post working PHP code to be put in a block. You can copy the code in a new custom block on your Drupal site. Note that this code is "AS IS" and might not work on all versions/platvorms and isn't up to the quality of the Drupal core code itself.

show load and uptime

bertboerland@ww... - February 8, 2005 - 20:21

<?php

$uptime
= shell_exec("cut -d. -f1 /proc/uptime");
$loadavg_array = explode(" ", exec("cat /proc/loadavg"));
$loadavg = $loadavg_array[2];
$days = floor($uptime/60/60/24);
$hours = $uptime/60/60%24;
$mins = $uptime/60%60;
$secs = $uptime%60;
echo
"This server has been up $days day(s) $hours hour(s) $mins minute(s) and $secs second(s)";
echo
"<p><br>";
print(
"[ Current server load: " . $loadavg . " ]");
?>

--
groets
bertb

footer

nsk - February 9, 2005 - 06:27

I need to display this info on the footer, but it seems php cannot be executed there. What should I do?

--
NSK, Admin of Drupal-based site http://www.wikinerds.org

If you are using phptemplate

gordon - February 9, 2005 - 07:21

If you are using phptemplate you can add this code here not a problem.
--
Gordon Heydon
Heydon Consulting

OT: loadavg numbers?

BrianH - February 9, 2005 - 07:19

I'm not a linux guru. I did a search online but didn't come up with much. What does the loadavg number really mean? That is, what does it signify? What is regarded as "high" load and what would be "low" and "normal" etc?

I tried the block on my site and it works fine. I seem to be getting an average load hovering around 1.0 but I don't know if this is good or bad.

It needs a key or legend or measure in relation to something known to make it understandable and useful to the average user ... like me.

Brian.

Read

Aran Deltac - February 9, 2005 - 16:32

Read this:

http://www.ussg.iu.edu/hypermail/linux/kernel/9802.2/0756.html

1.0 means your cpu speed is perfect for you cpu usage. I have about 0.17 on my server which means there is lots of room to grow.

my server loadavg (2) is

nsk - February 10, 2005 - 09:26

my server loadavg (2) is usually 3-4 but sometimes also 8. is this bad?

--
NSK, Admin of Drupal-based site http://www.wikinerds.org

Paypal Overview

Aran Deltac - February 8, 2005 - 22:52

Here's a snipets that I have in my site (www.electroniclife.org). I it to display how much and who has made donations. Requires the paypal_framework module.

<?php
  $result
= db_query("
    SELECT SUM(payment_gross-payment_fee) as total FROM paypal_log
  "
);
 
$pp = db_fetch_object($result);
  print
sprintf( "%01.2f", $pp->total+0 );
 
$result = db_query("
    SELECT l.payment_gross, l.payment_fee, SUBSTRING(p.first_name,1,1) as f_name, p.last_name
    FROM paypal_log as l, paypal_payer as p
    WHERE p.payer_id = l.payer_id
    ORDER BY l.payment_date DESC LIMIT 10
  "
);
 
$count = 0;
  while(
$donor = db_fetch_object($result) ){
    print
'<li>'. $donor->f_name .'. '. $donor->last_name .' ($';
    print
sprintf( "%01.2f", $donor->payment_gross + 0 );
    print
")</li>";
   
$count = $count + 1;
  }
  if(
$count==0 ){
    print
"<li><i>none</i></li>";
  }
?>

--
http://www.electroniclife.org/

The best forum thread

xmacinfo - February 9, 2005 - 16:47

This thread is an excellent idea. I hope that a lot of people will share their code and give us ideas.

Christmas countdown timer

sepeck - February 9, 2005 - 17:00

http://www.auburn.edu/oit/support/students/webpages/ht_countdown_date.ph...

For my sister-in-law's business, I put a christmas countdown timer on her site for her. I modified the path to files to be ./files/images, changed some of the text displayed and colors to match the site theme.

Not being a php coder, one day it occured to me that surely there was example code on the net. This was the third one I found and the easiest to understand. If I need to, I can now have it countdown to any date I need by modifying the text and color.

Just to see what would happen, I put the traditional phpinfo function into a php page. The output was the traditonal phpinfo we have come to expect. http://us3.php.net/phpinfo

-sp
---------
Test site...always start with a test site.

stopped smoking

bertboerland@ww... - February 10, 2005 - 12:01

I did something simular some time ago on my site but far less code:

<?php
$day
= 12;
$month = 12;
$year = 2003;
$cleandays= ((int)((mktime (0,0,0,$month,$day,$year) - time(void))/86400) * -1 );
print (
"I smoked half my life; the last 10 years cigars. Now however - related to <a href=\"http://brecht.boerland.com\">brecht.boerland.com</a> - I stopped. And I havent smoked for <b>" . $cleandays . " </b>days now. Thereby saving <b>" . $cleandays * 2.60 . "</b> euros.");
?>

--
groets
bertb

This one had fancy graphics

sepeck - February 10, 2005 - 16:38

This one had fancy graphics for numbers :).

Also, I mentioned it as an approach method for those less then familiar with php. It was a chance comment in the threads that really reminded me the power of blocks that you could just put in php code.

Thanks for the example tho, I may have a need for something simpler in the near future and yours may fit the bill.

-sp
---------
Test site...always start with a test site.

Hits by Month

kbahey - February 9, 2005 - 17:39

Excellent idea Bert! Kudos to you for starting this thread.

This is MySQL specific. I am sure it can be adapted to other databases.

Put this in a block, and it will show the number of hits per month, by month
The number of months shown will depend on what you configured (how long to keep statistics).

The first and last months will be incomplete, because of the log purging.

You can restrict the path of the block to admin to make this visible only to you (the site administrator).

<?php
$header
= array ('Month', 'Hits');

$rows = array();

$q = "SELECT DATE_FORMAT(FROM_UNIXTIME(timestamp), '%Y-%m') AS month, COUNT(*) AS hits FROM {accesslog} GROUP BY month ORDER BY month DESC";

$result = db_query ($q);

while (
$row = db_fetch_object ( $result ) ) {
 
$rows[] = array ( 'data' => array ( $row->month, $row->hits )) ;
}

if (!
$rows) {
 
$rows[] = array(array('data' => t('No log data available.'), 'colspan' => 2));
}

print
theme('table', $header, $rows);
?>

There's already a section in

Boris Mann - February 10, 2005 - 15:36

There's already a section in the handbook for Custom PHP blocks. Please put all working code there.

done

bertboerland@ww... - February 10, 2005 - 19:54

I copied them to there. New posters, please submit your blocks here

--
groets
bertb

list nodes associated to taxonomy

omar - February 10, 2005 - 20:03

This is a copy/paste of a node in a similar thread I tried to launch a while ago. There are a few other bits of code over there.

Here is the PHP code for a block to list the last five nodes associated to a taxonomy, in this case tid=17.

<?php
$taxo_id
= 17;
$sql = "SELECT node.title, node.nid FROM node INNER JOIN term_node ON node.nid = term_node.nid WHERE term_node.tid = $taxo_id LIMIT 5";

 
$output .= "<ul>";
$result = db_query($sql);
while (
$anode = db_fetch_object($result)) {
 
$output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";
}
 
$output .= "</ul>";
return
$output;

?>

Clearly, you can modify the HTML used here as well as add ID and/or CLASS tags for use with CSS styles.

Thanks go out to CyberSteph for his help with this.

Cusom blocks repository

Bèr Kessels - February 11, 2005 - 10:06

Please do not start a new threadm, but add your custom blocks *as child pages* to http://drupal.org/node/17170

And if this solved you problem, would you be so kind to report back that it helped? This will help others whom are looking for the same solution.

[Ber | Drupal Services webschuur.com]

 
 

Drupal is a registered trademark of Dries Buytaert.