Nov 12 2007

PHP to Excel

Tag: YadaDustin @ 5:12 pm

I was tasked with exporting report to Microsoft Excel files. There are a number of ways to do this. One of the easiest I have found it to create a tab delimited file and give it an .xls extension. You can also create a table using html and give it an .xls extension for similar results. The main issue with doing this is that it is not really an excel file so you may get an error like this: excel-error.gif

So, not wanted to re-invent the wheel, I went searching for a good php class I could use. I ended up selected this one. What I liked most about it was that I could just pass in a multi-dimensional array (which my data was already in) and it would create a Excel file from that.

Well, soon I got complaints that number fields were being stored as strings so it was back to the drawing board. I then came across this port of a popular Perl package. It had a lot more features I could use and it wrote numbers as numbers. I wrote a simple function that converted an array to an Excel file. I’ll post it here for your enjoyment.

$test = array(
array (
'Text' => 'abc',
'Number' => 123,
'Date' => '28345.345'
),
array (
'Text' => 'def',
'Number' => 456,
'Date' => '8/8/1977 7:00 AM'
),
array (
'Text' => 'ghi',
'Number' => 789,
'Date' => '1/1/2007'
)
);
echo '

'; print_r($test);
Array2Xls($test, 'test.xls', 'Report');

function Array2Xls($array, $filename, $worksheetname = 'Sheet1') {
	require_once "php_writeexcel-0.3.0/class.writeexcel_workbook.inc.php";
	require_once "php_writeexcel-0.3.0/class.writeexcel_worksheet.inc.php";

	$workbook = &new writeexcel_workbook($filename);
	$worksheet =& $workbook->addworksheet($worksheetname);
	$heading =& $workbook->addformat(array('align' => 'center', 'bold' => 1, 'bg_color' => 'black', 'color' => 'white'));
	$date_format =& $workbook->addformat(array(num_format => 'mm/dd/yyyy hh:mm:ss AM/PM'));

	$worksheet->set_column(0, 3, 15);

	// heading
	$i = 0;
	$h = array();
	foreach(array_keys($array[0]) as $val) {
		$worksheet->write(0, $i++, $val, $heading);
		array_push($h, $val);
	}

	// values
	for ($i=0; $iwrite($i+1, $j, $array[$i][$h[$j]], $date_format);
			}
			else {
				$worksheet->write($i+1, $j, $array[$i][$h[$j]]);
			}
		}
	}

	$workbook->close();
}

There is still one issue I have yet to resolve and that is concerning dates. I’ve been searching for a PHP script that can convert Unix timestamps or date strings to Excel date numbers. I gave up on it figured I will implement it when or if the request comes in to do so ;)


Nov 05 2007

My .htaccess File is Messing Up awstats on Wordpress Blogs

Tag: Programming & InternetDustin @ 5:04 pm

I use DirectAdmin for my hosting control panel. There is a nice plugin to add awstats, but I’ve found they don’t work on sites where I have WordPress set up (such as this site) because I’m using permalinks. No worries though. I found a way to fix it. Just put the bolded line below in your .htaccess:

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/awstats.*$ [NC] [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

*Note: Replace “awstats” with the name of your stats location


Oct 18 2007

SEO Cartoon Quiz

Tag: Programming & InternetDustin @ 2:00 pm

SEO Cartoon QuizI’m sure this is nothing more than link bait, but what the heck, I got 100% the first try, so I’ll bite.


Oct 18 2007

I’m an SEO Professional?

Tag: Programming & InternetDustin @ 5:14 am

I just finished the SEO quiz on seomoz.org. I got 200/255 - 79% so I guess they consider me a “professional”

SEO Professional - 79%

Are you an SEO Expert?

The problem is, I don’t feel like a pro. Many of the answers I guessed and got right, but honestly I never thought about them nor bothered to implement them. I think what I may do is take all the answers (after you complete the quiz you will get an answer sheet) and go through them one by and and apply them to one of my sites. We’ll see how well traffic increases to that site.

Maybe then after a bit of study I can go back and take the quiz again and be an expert! ;)


Oct 16 2007

Restoring MySQL InnoDB Files on Windows

Tag: Programming & InternetDustin @ 12:49 pm

Our server died at work last week. We’re a small shop without a real IT pro, just a bunch of hackers. Therefore, we had no back-up or recovery plan. I was tasked with restoring the MySQL database files. These where critical database we use on a daily basis, as well as our customers because it contain all their license information. We were able to get all the files off the computer in the mysql data folder (in C:\Program Files\MySQL\MySQL Server 4.1 & C:\Program Files\MySQL\MySQL Server 5.0). I found that all the MyISAM type tables restored just by moving the data files to the new data directory. The InnoDB type files were a more difficult task, and that is why I am writing this post. I had a hard time finding out how to do it, but I pieced together enough information to accomplish the task. Now, in an effort to give back, I’ll explain exactly what I did to get it restored.

In our back-ups we had the following files:

  • \MySQL\MySQL Server 4.1\data\ibdata1
  • \MySQL\MySQL Server 4.1\data\ib_logfile0
  • \MySQL\MySQL Server 4.1\data\ib_logfile1

Plus, in the data folder there were was a folder with the name of the database I was restoring that contained *.frm files (table_name.frm).

I did the restore on my development machine rather than the actual server because I didn’t want to screw up what was working on the server. I already had MySQL installed from an XAMPP install. (My development box is running Windows XP SP2). XAMPP installs MySQL a little differently than the regular MySQL install, so if it helps to follow what I did here, you may want to install it.

I first stopped my MySQL service using XAMPP’s control panel.

I moved the files listed above (ib* files and the folder containing the *.frm files) to the my local mysql data folder (C:\Program Files\xampp\mysql\data).

I then edited my.cnf (located in C:\Program Files\xampp\mysql\bin) and made the following changes (starting at line 66 for me):

OLD:
skip-innodb
#innodb_data_home_dir = C:/Program Files/xampp/mysql/data/
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = C:/Program Files/xampp/mysql/data/
#innodb_log_arch_dir = C:/Program Files/xampp/mysql/data/
#set-variable = innodb_buffer_pool_size=16M
#set-variable = innodb_additional_mem_pool_size=2M
#set-variable = innodb_log_file_size=5M
#set-variable = innodb_log_buffer_size=8M
#innodb_flush_log_at_trx_commit=1
#set-variable = innodb_lock_wait_timeout=50

NEW:
#skip-innodb
innodb_data_home_dir = C:/Program Files/xampp/mysql/data/
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = C:/Program Files/xampp/mysql/data/
innodb_log_arch_dir = C:/Program Files/xampp/mysql/data/
set-variable = innodb_buffer_pool_size=16M
set-variable = innodb_additional_mem_pool_size=2M
set-variable = innodb_log_file_size=170M
set-variable = innodb_log_buffer_size=8M
innodb_flush_log_at_trx_commit=1
set-variable = innodb_lock_wait_timeout=50

(I had to set innodb_log_file_size to the actual size of my log file)

I then edited the XAMPP batch file that starts the mysql service (C:\Program Files\xampp\mysql_start.bat). I added –innodb_force_recovery=6 to the end of the call to mysqld. So line 8 of that file now read:

mysql\bin\mysqld –defaults-file=mysql\bin\my.cnf –standalone –console –innodb_force_recovery=6

This did the trick! My databases were recovered on my machine. I used SQLyog to do a sql dump of the database to restore it on our production server.

If this helped at all, please leave a comment and let me know.


Sep 24 2007

Password Protecting a Zip File Using PHP

Tag: Programming & InternetDustin @ 10:47 am

I needed a way to add a password to zip files I was creating in PHP, but it seems support for that is lacking. So I resorted to using Info-ZIP. This particular site I was running PHP on a Windows IIS server. Info-ZIP is free and supported on multiple platforms. I downloaded the Windows binary file and put zip.exe in my C:\WINDOWS folder. I then had to give the Internet User Execute permission on C:\WINDOWS\System32\cmd.exe in order for me to call the system() function in php.

Then I wrote a quick test script that seemed to work:

<?php
$filename = "filename.zip";
$pdf = "document.pdf";
echo system("zip -P 1234 -j $filename \"$pdf\"");


Aug 31 2007

The 12 Reasons Satellite is Better Than Cable, Minus The 6 Reasons It is NOT

Tag: Satellite TelevisionDustin @ 6:40 am

The top 12 reasons satellite television (i.e. DIRECTV & Dish Network) kick cable’s (i.e. Comcast) Butt:

  1. More channels – Satellite providers are available across the country and out of the country even. More customers means more customer demands and hence more channels and packages available. Let’s say a particular channel is popular and available back east and you want to watch it on the west coast. You are much more likely to have this channel included in you satellite package that you would in your cable package. For example, I like watching Braves games and I live in Utah. Many of the games are played on SportsSouth. That channel is available on both DIRECTV & Dish Network, but not available through my local cable provider.
  2. Cheaper – Thanks to the two major players in the market, the costs are quite reasonable. You will find by comparing packages that you can get much more for your money than you can with cable.
  3. No Activation Fees – Order from either satellite provider and they will be out within a couple days and get you all set up, for free! Cable always tends to tack on a $30-$100 setup fee.
  4. Better Equipment – Enjoy top of the line gadgets from the satellite dealers – again, in most cases it is free.
  5. Free DVR! – Even with the cheapest satellite plans, you can get multiple DVR’s for free. Once you’ve had a DVR it’s hard to imaging not owning one. Cable’s DVR’s seem a bit out of reach in my mind for the average consumer.
  6. All Digital – All channels are digital on the satellite. This is not the case with “digital cable”.
  7. More sports – Looking back at reason number 1, you will see why this is the case. Plus, DIRECTV has more sports than any other provider. If you are a true sports fan, DIRECTV is your only options really.
  8. And Music – DIRECTV has XM satellite and Dish Network has Sirius. What does Cable have?
  9. Customer Satisfaction – OK, everyone will have their complaints and when there are problems with TV service I’ve found that people bark loud! But overall, based on consumer surveys, Dish Network is #1, and DIRECTV is #2. Oddly, is seams customers are more happy when they pay less and get more… go figure.
  10. More HDTV – Satellite has led the way when it comes to HD. Soon DIRECTV will air 150 channels in HD including local channels.
  11. Availability – This obviously will not apply to MOST people, but if you live in Rural area that doesn’t have a cable company, or a new development where the utilities have not yet come to you, you basically have no other choice.
  12. Portability – My mom is weird but, I know whenever they take their camper out, they take along their extra satellite dish and receiver and have satellite TV with them while their camping (if you can call it camping). Have you ever heard of anyone taking their cable with them?

… And the 6 reasons why cable has its strong points:

  1. Reliability – I went 5 years without having a problem with my satellite including wind, rain & snow, then one day it went fuzzy. That’s when I switched to the other provider and problem was solved. There may be occasions when snow or some other environmental variable may cause interference with your satellite. You just don’t have this problem with cable. That’s not to say cable is perfect. It seems my cable high speed internet has more outages than my satellite, but that’s a different beast.
  2. Local packages – Cable companies are generally local and they know what locals want. The biggest example I can think of is right here in Utah. BYU and University of Utah Football is HUGE in the fall. The mountain west games are now being played on 3 different channels: 1. The Mountain (MTN); 2. Versus; 3. CSTV. Comcast has all three channels in their basic package. Neither DIRECTV nor Dish Network carry the Mountain. It is a new station and they claim they don’t have enough demand yet. I personally thing the real issue is because Comcast owns the Mountain. Damn them all!
  3. Package Deals – Sticking with the local them, you can often get your phone and internet through your cable company, which can save you some money.
  4. On Demand – Comcast loves to tote their On Demand stuff. This basically lets you choose from a library of shows and movies that you can watch anytime – much like YouTube ;)
  5. No Roof Décor – Hey, you can always put the satellite on the side of your house if you don’t like it on the roof ;)
  6. No Contract – Often, if you’re going to take advantage of the free equipment and installation with the satellite providers it will require a one or two year contract. With cable there is generally no contract so you can cancel at any time.

Aug 21 2007

Over a Year and No Mountain

Tag: Satellite Television, Sports & StuffDustin @ 10:23 am

The MountainIt’s that time of year again. My contract is up on DIRECTV, so I’m preparing to make the switch to Dish Network for some new DVR’s and more channels for the money.

I’m a bit hesitant though. Even though I’m a big fan of satellite television, I’m actually considering Comcast. Why? Well, it’s been over a year, and still the Mountain (mtn) is only available on Comcast. Now, was this some ploy by Comcast to get more subscribers? So what’s a guy to do, be stubborn and say no to the monopoly, or enjoy some BYU & Utah Football?

Decisions… Decisions…

I guess for now, I can do my part by signing online petitions and making my voice heard. I will be sure to tell DIRECTV that the reason I am leaving is because they won’t offer mtn. ;)


Aug 21 2007

The Cost To Fix a Computer

Tag: ElectronicsDustin @ 8:21 am

I recently built my own computer and I know from experience it can be a pain in the butt finding and fixing issues. (In my case it was the bios not giving my RAM enough voltage.)

I am the family tech support guy. It makes sense that every family has one of these, otherwise, you may be left to the wolves trying to find an honest computer repair store…


Jul 13 2007

How To Get “Free” DIRECTV Upgrades

Tag: Satellite TelevisionDustin @ 6:05 am

I was browsing my DIRECTV account online this morning and noticed that I was eligible for various free upgrades. My first though was, “man, I could have had another DVR all this time… for free?”

But then I read the fine print

Offer for existing residential customers only and requires a two-year programming commitment to any DIRECTV base package or DIRECTV Spanish-language® package, and activation of DIRECTV® DVR service.

So I would have to renew my contract for two years, but I would also have to activate DVR service. What if it is already activated?

In any case, after comparing apples to apples the pricing and channels available of various packages, I think I’ll be switching to Dish Network next month anyway.


« Previous PageNext Page »


ss_blog_claim=4973c51f5e7bec57951f995fed1b85f3 ss_blog_claim=4973c51f5e7bec57951f995fed1b85f3