Categories
MySQL PHP Snippet

Quick and Dirty: List records in a table using PHP and MySQL

This code lists reads a dataset from the database and lists all attributes in a table. 

This code sample is deliberately quite basic, though CSS is used (through the $bgcolour variable) to create alternating background colours for the rows.

The code assumes you have already connected to a database.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
echo "<table border='0'>";
// capture the data
$dataset = mysql_query("SELECT * FROM tablename");
// now process the dataset, one row at a time
while ($row = mysql_fetch_assoc($dataset)) {
  // create a variable for each attribute in the record
  //  - variables are easier to use when building the table 
  foreach($row as $var => $value) {
    $$var = $value;
  }
  // set the alternating background colour for the row
  $bgcolour = (!isset($bgcolour) || $bgcolour == "#F0F7FF") ? "#FFFFFF" : "#F0F7FF";
  // build the rows, placing the variables in <td> elements as appropriate
  echo "\n<tr style="background-color:$bgcolour;">";
  echo "\n<td style="font-size:80%;">$attribute</td>";
  echo "\n</tr>";
} // end while
echo "</table>";
Categories
PHP

Calculating a fiscal year in PHP

This code is a straight cut and paste from a comment the php.net page on the strftime() function, credit is due to lamb dot dan at gmail dot com.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
/*
*    This function figures out what fiscal year a specified date is in.
*    $inputDate - the date you wish to find the fiscal year for. (12/4/08)
*    $fyStartDate - the month and day your fiscal year starts. (7/1)
*    $fyEndDate - the month and day your fiscal year ends. (6/30)
*    $fy - returns the correct fiscal year
*/
function calculateFiscalYearForDate($inputDate, $fyStart, $fyEnd){
    $date = strtotime($inputDate);
    $inputyear = strftime('%Y',$date);
 
    $fystartdate = strtotime($fyStart.$inputyear);
    $fyenddate = strtotime($fyEnd.$inputyear);
 
    if($date < $fyenddate){
        $fy = intval($inputyear);
    }else{
        $fy = intval(intval($inputyear) + 1);
    }
 
    return $fy;
 
}
 
// my fiscal year starts on July,1 and ends on June 30, so...
echo calculateFiscalYearForDate("5/15/08","7/1","6/30");
// returns 2008
 
echo calculateFiscalYearForDate("12/1/08","7/1","6/30");
// returns 2009
?>
Categories
WordPress

Quickly move a WordPress blog to a new Host or Domain

Moving a WordPress blog to a new domain    or web host may be easier than you think.

  1. In the blog to be moved, select “Tools” in the left sidebar of your Dashboard.
  2. Now select the “Export” option to download the contents of the blog (posts, pages, comments, custom fields, categories, and tags) in an XML export file.
  3. Prepare your new WordPress installation on the new server with the appropriate theme and any plugins.
  4. In the dashboard of the new blog go to “Tools” and select “Import”.
  5. Choose the “Wordpress” option.
  6. Browsing to and select the XMS file you exported from the original blog, then click “Upload and Import”.

That's all there is to it.  Don't forget to redirect your domain to the new server if applicable.

Categories
Joomla!

Joomla breaks PayPal Button Code

Recent versions of Joomla! have been seen to break PayPal button code.

This is because the newer versions are stripping the action attribute from the <form> tag.

You can fix this by following these steps:

  • Go to the Article Manager
  • Select Parameters
  • Scroll down to Filtering Options, Select the front end groups (Control-Click the three groups)
  • Select the Filter Type Blacklist (Default)

You may wish to select different groups.

See Also:

Categories
WordPress

Updating WordPress

Periodically WordPress  must be updated.

You can determine the current version of WordPress by looking at the source code of the a post.  (Right-Click on the page and select View Page Source.)  You'll see a line similar to the following approximately 20 lines down.

Once in the WordPress Dashboard you will see a message like "WordPress 2.8.4 is available! Please update now." across the top if an upgrade is due.

Users reasonably comfortable with MySQL and with access to the server should install the MySQL Administrator and MySQL Query Browser from the MySQL Website to assist with database management.  It's a much easier way to work with your database, and it's not all that complicated.  I find MySQL Administrator a much easier way to work with databases that a web-based tool like phpMyAdmin.

Follow the following process to do the backup.

  • If you're using phpMyAdmin, use your webhost's control panel to access  phpMyAdmin and backup your database.
  • If you use MySQL Administrator to create a backup of the database.
    • Select Backup from the upper left pane; Select the WordPress Backup from the projects list in the lower left pane, click Execute Backup Now in the bottom right corner.
    • (If a backup project is not listed, click New Project, enter the name WordPress Backup, select your WordPress database in the schemata list, click [>], click Save Project.)
  • Backup necessary WordPress Files.
    • Typically it is only necessary to make a backup of the of the /wp-content/ folder, though a full backup of the site can be made.
    • Make a copy of the /wp-content/ folder in the wwwroot share.  Rename the copy of the folder to reflect the existing version number, i.e. if the existing version (see above to find the version) is 2.8.4, the copy of /wp-content/ will become /wp-content.284.disabled/.
  • Update WordPress
    • Click the Please update now link at the top of the WordPress dashboard and follow the instructions.
    • Visit any blog post, you'll be prompted to update the database.  Proceed with the database update, despite the warning of several minutes it will only take a few seconds.