HOME BLOG iPhone SCRIPTS MUSIC

Archive for February, 2009

Building a Bar Graph in PHP

Tuesday, February 17th, 2009

Can I be honest? I hate making graphs. I think they are over used and serve as filler on droll reports more often than not. In spite of that, I know they have their place. When used correctly can be a GREAT way to communicate a large amount of information in a very orderly way.

I needed a graph the other day to present some data, so I sat down and creating one from scratch. I’d like to share the experience for those of you who are trying to learn PHP. I am sure there are better ways to do this from a programming perspective, but I think it’s pretty easy for the rest of us to understand.

First, we create our script and for illustration purposes we simulate a data source. This can be static as I have done, or can be a query array return or just about any other source.

<?php
$tw
=‘90%’;
$chart =
array(

rand(0,100),rand(0,100),rand(0,100),rand(0,100),rand(0,100));

$label = array(‘Frogs’,‘Lizards’,‘Gophers’,‘Snakes’,‘Turtles);

$cname=‘Likelihood of Creatures Being Eaten’;
//$label=array with labels
//$chart=array with percentages
//$tw=table width in percent or px
//$cname=table name
//barc($label,$chart,$tw,$cname);

function barc($label,$chart,$cname,$tw=‘95%’){
echo ‘<table style=”border-collapse: collapse; border-right: 5px #c0c0c0 solid;border-left: 5px #c0c0c0 solid;border-bottom: 5px #c0c0c0 solid;border-top: 0px #c0c0c0 solid; width: ‘.$tw.‘; margin: 0px auto;”>

<tr><td style=”background-color: #c0c0c0; width: 10%;”>’;
echo ‘<table style=”border-collapse: collapse;width: 100%;”>’;
foreach ($label as $c){
$lab=$c;

echo ‘<tr><td style=”font-weight: bold; vertical-align: bottom; background-color: #c0c0c0;

height: 20px;padding: 0px;margin: 0px;border-top: 3px #c0c0c0 solid;

font-size: 16px;”>’.$lab.‘</td>
</tr>
;
}
echo ‘</table>’;
echo
‘</td><td style=”background-color: #c0c0c0; width: 90%;”>’;

echo ‘<table style=”border-collapse: collapse;width: 100%;”><tr>’;
$w=0;
while($w<100){
echo ‘<td style=”width: 1%;”></td>’;
$w++;
}
echo ‘</tr>’;
$x=1;
foreach ($chart as $c){
$bar=$c;
$fill=100-$bar;
if($x==7){$x=1;}
echo ‘<tr><td class=”bar” colspan=”‘.$bar.‘”

style=”background : red url(bar’.$x.‘.jpg) repeat-x top center;

background-color: red; height: 20px; width: 1%; padding: 0px; margin: 0px;

border-top: 3px #c0c0c0 solid; font-size: 16px;”>

<span style=”color: white; margin-left: 2px;”>’.$bar.‘%</span></td>

<td colspan=”‘.$fill.‘” style=”background-color: #c0c0c0;border-top: 3px #c0c0c0 solid;”></td>
</tr>’
;
$x++;
}
echo ‘</table>’;

echo ‘</td></tr><tr><td colspan=”3″ style=”background-color: #c0c0c0; font-size: 24px; text-align: center;”>’.$cname.‘</td></tr></table>’;
}
barc($label,$chart,$cname);
?>

Monkey Business

Sunday, February 15th, 2009

Once upon a time a man appeared in a village and announced to the villagers that he would buy monkeys for $10 each.

The villagers, knowing there were many monkeys, went to the forest and started catching them. The man bought thousands at $10 and, as supply started to diminish, the villagers stopped their effort.

He then announced that he would buy monkeys at $20 each. This renewed the villager’s efforts and they started catching monkeys again.

Soon the supply diminished even further and people started going back to their farms. The offer increased to $25 each and the supply of monkeys became so scarce it was hard to even find a monkey, let alone catch it!

The man now announced that he would buy monkeys at $50 each! However, since he had to go to the city on some business, his assistant would buy on his behalf. In the absence of the man, the assistant told the villagers: “Look at all these monkeys in the big cage that the man has already collected. I will sell them to you at $35 each and when the man returns from the city, you can sell them to him for $50 each.”

The villagers rounded up all their savings and bought all the monkeys for 825 billion dollars.

They never saw the man or his assistant again, only lots and lots of monkeys!

Now you have a better understanding of how the Economic Stimulus Package will work.

Linux and Wireless

Wednesday, February 4th, 2009

I have on many occasion mentioned just what a great OS Linux is. Today, I’d like to point out an area that really need some work: Wireless.

Wireless networking in Linux can be very painless, or it can be as enjoyable as a date with the Marquis de Sade. It all depends on what you own and what you are trying to do.

Just this weekend, I decided to convert my new Acer Aspire One to a Linux Dual Boot system. Learning from past experience, I booted the the laptop off a live-CD to make sure the hardware would all work. In this case, I chose OpenSuSE 11.1-Gnome. The system booted up without error, and the sound worked. Good. Now for the next test: Wireless. I have found in the past that Sound and Wireless are the most troublesome pieces of hardware when using Linux on a laptop. I pointed Network Manager to my SSID, and within a few seconds, Google.com was up. Ok, we’ll go ahead and install to disk.

The install went well, and Grub setup the system for dual-boot as needed without any issues. After the reboot and the final few questions, I was in. The startup music was indeed music to my ears. Now for wireless. Huh? Nothing. Dead. It was there, but the radio was off. The soft button was useless. I could find no way to turn the radio on. Now what? No problem, I’ll boot to windows and see if I can find a clue there. Uh-oh, same issue there. The soft button would turn the radio on for a second, but the light would go out right away. What in the world?

After downloading and installing the drivers fresh from Acer’s website, I still had the same issue. I was getting a little scared. I know sometimes firmware gets loaded at boot-up. Did it break it?

I tried everything I could think of, but both Linux AND Windows wireless wouldn’t work. Then I had one last idea… When was the last time it worked? When I had the live CD… so back to that. And you know what? It worked! Then I rebooted to windows… It worked too! And you know what? Linux started working as well. Weird.

Well, my theory, which dovetails to my point, is that Linux tried to switch wireless into an invalid mode, thus “breaking” it. Because Windows drivers don’t mess with that, it couldn’t fix it either (MS assumes they are the on relevant thing out there, so lazy programmers don’t consider other OS’s). Anyways, my theory is the Live-CD returned the wireless to the correct mode, and that fixed it.

In any event, that’s the problem with Linux. Sometimes, it’s still a little TOO smart. That’s fine in the hands of a “Geek”, but what about the typical PC user? Being geekish myself, I was able to make it work, but what about someone else? What about the neighborhood PC shop (Who knows NOTHING about Linux)?

Well, not knowing anything about Linux, they’d say that Linux broke their PC. (Not true, Linux just told the PC to do what someone told it to do… ) Then they would say the wireless card was broke, and needed replacing. So, 2 hours of labor @ $65/hr and $100 for the card, and you’re out $230 for something that isn’t broke. That’s bad. And enough to scare someone into NEVER trying Linux again.

Pretty grim picture.

The community is working very hard, and things are getting better all the time, but this kind of stuff happens a lot still. On my Toshiba laptop, it just worked. I never had to do anything.

Support your local DEV and try to help the newbie’s all you can.

CSS article

Sunday, February 1st, 2009

This may be one of the most useful CSS articles I’ve ever read. Good job!

http://www.search-this.com/2007/03/12/no-margin-for-error/


© 2009 BLMServices.com
Currently: Sunny: 45F