Delicious!….hmmmm, yes!!!

This video explains what the title means. Anyway, one of the things I hate about PHP is how redundant the MySQL statements are. Each one is the same thing, but different only by a little bit. Experiencing the same thing? Well, this function I just wrote will help lighten the redundancy!

<?php

function add_mysql_entry($info)
{
if(is_array($info) and sizeof($info) > 0)
{
// Python: mysql_values,mysql_tables = {},{}
$mysql_values = array();
$mysql_tables = array();
if(is_array($info['post']))
{
foreach($info['post'] as $post)
{
// You need single quotes, or else it’s a suntax error.
$mysql_values[] = “‘”.htmlspecialchars($_POST[$post[0]]).”‘”;
$mysql_tables[] = $post[1];
}
}
if(is_array($info['background'])) {
foreach($info['background'] as $background_info)
{
$mysql_values[] = “‘”.$background_info['value'].”‘”;
$mysql_tables[] = $background_info['column'];
}
}
$mysql_values = implode(’,', $mysql_values);
$mysql_tables = implode(’,', $mysql_tables);
$query = mysql_query(”
INSERT
INTO “.$info['table'].”(”.$mysql_tables.”)
VALUES(”.$mysql_values.”)”) or die(mysql_error());
}
}

?>

This function is basically a simple MySQL INSERT statement. It makes it so that you don’t have to declare each $_POST variable, preform all precautions, execute the query, and then make sure that the query did what you wanted it to do. Here’s an example:

<?php

$info = array(
'table' => 'shoes_tb',
'post' => array(
array('add_shoes_name', 'shoe_name'),
array('add_shoes_desc', 'shoe_desc'),
array('add_shoes_price', 'shoe_price')
),
'background' => array(
array('value' => date("d/m/Y"), 'column' => 'shoe_date_added')
)
);
$add_shoe = add_mysql_entry($info);

?>

In this example, there are three $_POST variables that the programmer wants to use: add_shoes_name, add_shoes_desc, and add_shoes_price. He wants insert them in the columns shoe_name, shoe_desc, and shoe_price in the table shoes_tb. Of course, getting something such as the date and not letting the user see it can’t be done through a $_POST variable (other than using the hidden input type). This is where background comes in. The syntax of the $info array is:

<?php

$info = array(
'table' => the MySQL table's name,
'post' => array(
array(input_field_1, column_name_1),
array(input_field_2, column_name_2),
...
array(input_field_n, column_name_n)
),
'background' => array(
array('value' => value_1, 'column' => column_name_1),
array('value' => value_2, 'column' => column_name_2),
...
array('value' => value_n, 'column' => column_name_n)
)
);

?>

All in all, this is function is delicious.

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

September 1st, 2008 | 2 Comments

Changing printers in Windows Vista

Today, I had to actually change printers - that usually happens around every time one’s own printer type is outdated by a new type of printer. The reason why I had to change printers was because I had needed to have a ASCII reference table. You can get one here.

The process of changing printers is very simple. The most obvious pars would be plugging the printer into an electrical socket and then establishing a connection between your computer and the printer (wireless or wired, it will usually be wired (USB wire)). Yes, but for some reason Windows Vista doesn’t update the printers. I mean, it’ll know when you disconnect a speaker, mouse, keyboard or screen, but for some stupid reason it won’t know when the printer is disconnected - either that, or doesn’t care. First of all, once it’s physically connected, open the Control Panel (Start -> Control Panel). Now, I’m going to write this in list form to make it easier to read:

  1. Once you are in the Control Panel, click Hardware and Sound. It should be the picture of a printer.
  2. After you are in Hardware and Sound, click Printers and a window with a list of printers will appear.
  3. Now, do you know the name of your printer? My new printer is the HP Officejet 5610 series; therefore, I would double-click on the text that says HP Officejet 5600 series. The name of your printer and one displayed on your screen may not match up, like mine. I’m not sure why this, but Windows doesn’t seem to care. What you basically need to do is, double-click on the shortcut that has the name of the printer you want to use. If there’s more than one of those shortcuts, try each one until you get the one that works.

To test, print something. If it comes out of the printer that you want it to come out of, then mission accomplished. If not, then… post a comment.

Here are pictures if you are a visual learner:

September 1st, 2008 | 1 Comment

Powered by WordPress | Blue Weed by Blog Oh! Blog | Entries (RSS) and Comments (RSS).