Export Parts of MySQL Table to Excel CSV File Using PHP
This is a quick snippet of code I created to dynamically pull a user-defined query from a MySQL database.
(more…)
This is a quick snippet of code I created to dynamically pull a user-defined query from a MySQL database.
(more…)
Posted on January 28, 2010 in PHP Tutorials and has been tagged as excel, mysql, php.
Here’s a quick snippet of code that allows you to quickly generate a random string of letters and/or numbers based on the variables you feed into the function. (more…)
Posted on January 8, 2010 in PHP Tutorials and has been tagged as php.
This problem came up while working with the Allegheny County Property database. They have a field for the sale date of the property, only they’re storing the date like this – 3291982 for 03/29/1982.
Posted on September 30, 2009 in PHP Tutorials and has been tagged as dates, php.
Take an array of numbers, strip out the dollar sign and format them correctly.
This is useful when you are given prices as varchar or text with the dollar sign already included in the field. We’re going to start with an array of numbers, in this case we’ll use the array(‘$1256.45′,’$5564.75′,’$7895.33′).
Here is the foreach loop running:
Start with $1256.45
Strip the $ – 1256.45
Cleaned and formatted number with the dollar sign added back in as a character – $1,256.45
Start with $5564.75
Strip the $ – 5564.75
Cleaned and formatted number with the dollar sign added back in as a character – $5,564.75
Start with $7895.33
Strip the $ – 7895.33
Cleaned and formatted number with the dollar sign added back in as a character – $7,895.33
Here’s the code that performs the task above:
<?php $number_array = array('$1256.45','$5564.75','$7895.33'); foreach ($number_array as $number) { echo "Start with $number<br/>"; $number = str_replace('$','',$number); echo "Strip the $ - $number<br/>"; $formatted_number = number_format($number, 2, '.',','); echo "Cleaned and formatted number with the dollar sign added back in - <strong>$$formatted_number</strong><br/><br/>"; } ?>
The next thing we’ll do is take our newly formatted numbers and perform a simple calculation to get a percentage off.
We will use the price 1234.67 as our starting price and 45% off as the discount rate.
Here is the foreach loop running:
starting price is $1234.67
45% off of $1234.67 = $555.60
$1234.67 – $555.60 will equal $679.07 – the final sale price
<?php $price = 1234.67; $percent_off = .45; echo "starting price is $$price<br/>"; $money_off = $price * $percent_off; $money_off = number_format($money_off, 2, '.',','); echo "45% off of $$price = $$money_off<br/>"; $sale_price = $price - $money_off; $sale_price = number_format($sale_price, 2, '.',','); echo "$$price - $$money_off will equal $$sale_price - the final sale price"; ?>
Posted on August 7, 2009 in PHP Tutorials and has been tagged as php.
A common problem that I’ve encountered – how to control dynamic text based on how long it is. In this case I had titles ranging from 10 characters to 25 characters – this includes spaces. The goal is to make them centered regardless of how many letters and spaces are in the title.
Here is an example of one of the shorter titles I was given:

Here is an example of one of the longer titles I was given:
A series of quick if/else statements and use of the strlen function in PHP allows me to assign different classes depending on the length of the dynamic title. I figured out, based on the CSS on the H1 class, that 14 was my magic number.
Here’s the quick snippet of code I used:
$title = $post->post_title; $char_title = strlen($title); if ($char_title > 14) {$header_style = "double";} else {$header_style = "single";}
I grab the post title using common WordPress calls and make it into the $title variable. The I use the strlen function to determine the number of characters (including spaces) in the title. If it’s greater than 14, it assigns the $header_style variable the class “double” or else it assigns it the “single” class.
Here is how I used it within the WordPress loop:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> <div class="<?php echo $header_style ?>"><h2><?php the_title(); ?></h2></div> <?php endwhile; endif; ?>
Hopefully you’ve found this quick tutorial valuable. Contact me if you have any questions or would do it a different way. There’s 1,001 ways to do everything – that’s why I love coding.
Posted on July 8, 2009 in Wordpress How-To's and has been tagged as css, php, wordpress.
My name is Andy Weigel and I'm a web developer and designer in Pittsburgh, PA.
I focus on combining design with technology to build compelling, creative, easy-to-navigate web sites and custom web applications for organizations and businesses of all shapes and sizes. My specialty is WordPress. And most of all, I love what I do!