Milo Lamar's blog

Milo Lamar's picture

SELECT Examples

A. Use SELECT to retrieve rows and columns

This example shows three code examples. This first code example returns all rows (no WHERE clause is specified) and all columns (using the *) from theauthors table in the pubs database.

USE pubs
SELECT *
FROM authors
ORDER BY au_lname ASC, au_fname ASC

-- Alternate way.
USE pubs
SELECT authors.*
FROM customers
ORDER BY au_lname ASC, au_fname ASC

This example returns all rows (no WHERE clause is specified), and only a subset of the columns (au_lnameau_fnamephonecity, state) from the authorstable in the pubs database. In addition, column headings are added.

USE pubs
SELECT au_fname, au_lname, phone AS Telephone, city, state
FROM authors
ORDER BY au_lname ASC, au_fname ASC

This example returns only the rows for authors who live in California and do not have the last name McBadden.

USE pubs
SELECT au_fname, au_lname, phone AS Telephone
FROM authors
WHERE state = 'CA' and au_lname <> 'McBadden'
ORDER BY au_lname ASC, au_fname ASC
Milo Lamar's picture

What is the difference between jQuery functions .prop() and .attr()?

linkFrom http://jquery.com/upgrade-guide/1.9/#attr-versus-prop-

.attr() versus .prop()

jQuery 1.6 introduced the .prop() method for setting or getting properties on nodes and deprecated the use of .attr() to set properties. However, versions up to 1.9 continued to support using .attr() for specific situations. This behavior in the name of backwards compatibility causes confusion when selectors are used that distinguish between attributes and properties.

For example, boolean attributes such as checked and disabled on a checkbox are affected by this change. The correct behavior of "input[checked]" is to select checkboxes that have a checked attribute, regardless of its string value, and regardless of their current state. In contrast, "input:checked" selects checkboxes that are currently checked as reflected in their boolean (true or false) checked property, which is affected when the user clicks the box for example. Versions prior to 1.9 sometimes do not select the correct nodes with these selectors.

Here are some examples of correct and incorrect usage when setting checked on a checkbox; the same rules apply for disabled. Note that only the property consistently reflects and updates the current state of the checkbox across all browsers; rarely will you need to set the attribute.

// Correct if changing the attribute is desired
$(elem).attr("checked", "checked");
// Correct for checking the checkbox
$(elem).prop("checked", true);

// Correct if removing the attribute is desired
$(elem).removeAttr("checked");
// Correct for clearing the checkbox
$(elem).prop("checked", false);

The value property versus attribute on input elements is another example of this ambiguity. The attribute generally reflects the value that was read from the HTML markup; the property reflects the current value. Since the .val() method is the recommended jQuery way to get or set the values of form elements, this confusion usually does not affect users.

However, when a selector like "input[value=abc]" is used, it should always select by the value attribute and not any change made to theproperty by the user, for example from them typing into a text input. As of jQuery 1.9, this behaves correctly and consistently. Earlier versions of jQuery would sometimes use the property when they should have used the attribute.

The jQuery Migrate plugin restores the old attribute-vs-property rules.

 

So basically we might be interested in what the checked attribute of a checkbox was when the page loaded using

$('#checkbox').attr('checked');

or we might want to know what its current state of being checked is using

$('#checkbox').prop('checked');

but for the sake of cross-browser compatibility we will always set the state of a checkbox using

$('#checkbox').prop('checked', true);

and

$('#checkbox').prop('checked', false);
Milo Lamar's picture

Find data subsets with counts using SQL

Sometimes when programming or analyzing your data, you need to know how many rows have the same name.
This is easily done with the COUNT aggregate function and grouping the columns together in a group by clause.

Example:

SELECT Name, COUNT(*) AS NumWithName
FROM table_users
GROUP BY Name
ORDER BY NumWithName

May return a result set like:

Name NumWithName
Xavier 1
Jim 1
Tom 3
John 5

But what if you only want to know names that have more than 1?
This is what the HAVING clause is good for in a query.

Example:

SELECT Name, COUNT(*) AS NumWithName
FROM table_users
GROUP BY Name
HAVING COUNT(*) > 1
ORDER BY NumWithName

If this was queried on the same dataset from above, we could get this result:

Name NumWithName
Tom 3
John 5

Notice the aggregate function COUNT(*) goes in the HAVING clause while the column's alias NumWithName goes in the WHERE clause.

Milo Lamar's picture

The sudoku code source

Sorry this program is kind of hard to follow.
But that's what you get from a newbie programmer who goes on
a writing frenzy one night.

See it in action at http://sudoku.mdlwebsolutions.com

If I were to go modify it today, I'd definitely change the mutually recursive
functions to something more readable. The program bounces around different
bits of logic willy-nilly and becomes very hard to trace. Laying the code out in a
more linear pattern would've made me more eager to post the source. I'll put it on
the bottom of my to-do list.

I would also like to write a similar program that produces solvable puzzles.
This script isn't so much for playing as it is for cheating. I made some
integrations with different sudoku sites that would send their puzzles to this
script and this script would attempt to solve them then some javascript would
send the results back into the various sites' puzzles.

Big shout out to http://www.websudoku.com for their sudoku software that
helped me pass many slow hours.

Milo Lamar's picture

On project time estimation and cost.

I found a pretty interesting code project cost calculator at http://diana.nps.edu/~madachy/tools/COCOMOII.php

It has all sorts of options to consider when thinking about size and scope of a project and actually attempts to calculate the cost of a project. Maybe I didn't plug my variables in correctly but it looks like I don't charge nearly enough for the projects I've done thus far. Oh well, keeping the cost low for companies makes me more attractive I suppose.

Milo Lamar's picture

My ntbtlog.txt

Help me analyze my boot log and figure out what can make it go faster and what problems can be resolved regarding security or general errors.

Milo Lamar's picture

coreuninstall.log file created on upgrade from Windows XP SP2 to Windows 7

So this is the log file that was generated and has some of the relevant settings and procedures that happened during a major upgrade from Windows XP SP2 to Windows 7. Let me know if you have any questions about it.

Milo Lamar's picture

Had some server hiccups.

I stripped KDE off the old box you're pulling this story from.  I have no more intentions of using its GUI so I removed a large amount of graphical user interface applications, including the desktop environment.  The full list of packages gone will come later.  On a related note, the system froze several times in the following days.  The apache logs implicated APC. I increased APC memory from 32 to 64 and it hasn't happened since although after scanning the forums it looks like it could be a deeper issue that will eventually come back given more memory.

Milo Lamar's picture

New site in development servervariable.com

I'm putting together a D7 site http://servervariable.com discussing PHP $_SERVER. I wrote a couple custom modules to track and display user agents and IP addresses. Will add functionality to track and display http referrers and query strings and write pages discussing them and their uses.

Milo Lamar's picture

WAMP Server Update available

Hey developers, http://www.wampserver.com/en/ released a new version which features PHP 5.3.8 (link to the changelog), MySQL 5.5.16 (link to mysql 5.5.x changelog), Apache 2.2.1 (link to apache 2.2.21 changelog), PHPMyAdmin 3.4.5 (link to phpmyadmin changelog), XDebug 2.1.2 (view the changelog), and XDC 1.5 (manual).  They also gave their website a facelift.  Careful if you suffer motion sickness...

 

I also printed out the WAMP changelog in English since it comes in French from their site.  So get it and you can do your AMP programming on your windows machines too :)

Syndicate content