Thursday, May 30, 2019

How to implement select all function for multiple sets of checkbox items on the same webpage using simple JavaScript

When designing a webpage with list of checkbox items, it is common and useful to implement a Select All/None function so that the user can easily select/unselect all items with a single mouse click.

This can be done with a simple JavaScript function, even without the need of using jQuery.

Now, the challenge is, when you have multiple sets of checkbox items on the same webpage, how to implement a shared JavaScript function to handle the Select All/None action to each set of the items respectively, so that when the user Select All in one of the sets, the other sets will remain unaffected by the action?

Let's say, we have 3 sets of item as below:


You can use separate FORM for each of the sets, and use FORM ID to distinguish each of the FORM. Each FORM will need to handle their own SUBMIT action separately.

If the different sets are within the same FORM and share the same SUBMIT action, you can make use of CLASS name in their CHECKBOX elements to distinguish and separate out the sets.

Here is the JavaScript codes to implement the Select All/None function (click the image to enlarge it):



and below is the HTML BODY part of the sample webpage (click the image to enlarge it):


Set 1 is in separate FORM from Set 2 and Set 3 with a different FORM ID. Note that even though Set 1 and Set 2 are both using the same CHECKBOX CLASS name, their checkboxes will can be selected all/none separately.

Set 2 and Set 3 are within the same FORM. We can still separate them by using different CHECKBOX CLASS names.

Our simple checkAll JavaScript function accepts both the FORM ID (as 1st parameter) and CLASS NAME (as 2nd parameter), and making use of its 3rd parameter as toggle. In this way, you can use this checkAll function to handle all the sets on the same webpage.

You can create a HTML file to try it out. Since this is just an ordinary HTML with simple JavaScript, it can run even without any web server. Just create the HTML, and open it with your web browser to test it out.


Monday, May 13, 2019

How to choose a better maintenance free car battery

Car battery is a consumable product that will eventually fail over time. When we say a better car battery, the most important point is that it can last longer and degrade slower.

Below are some of the criteria to look for in selecting a better car battery.

1. Brand

The lifespan of battery is affected by high temperature. Certain brand of battery is manufactured for use in hot equatorial areas, such as Amaron, Exide, etc.

 

2. Size

The battery for normal passenger car is normally of NS40, NS60 or NS70. NS60 is more powerful than NS40, and NS70 is more powerful than NS60.

However, choosing the right size of battery that match with your car's alternator is important. If you car's alternator is generating smaller current and designed to work with NS40 type of battery, it is better for you to stick to NS40 or at most upgrade to NS60 but not beyond.

This is because if the charge supplied from alternator is not sufficient to make the battery fully charged, the battery will suffer premature death and therefore has a shorter life span.

In addition, bigger battery will cause heavier workload to the alternator, which might also reduce the life span of the alternator.

Regardless of the size of the battery, if the charging system is unstable, the battery will die fast.


3. Performance Rating

A battery number looks like this: 46B24L

The first 2 digit is its overall performance rating. The higher the better.

The alphabet after the first 2 digit is an indicator of the battery's width and height. This alphabet can be from A to H. Smaller alphabet indicates smaller width and height.

The number after the alphabet is its length in centimeter. For the battery number 46B24L, its length is 24cm.

The final alphabet indicates the position of its positive terminal. L means it is on the left, and R means it is on the right.



4. Amp Hour (AH)

Amp Hour is an indicator of how much energy is stored in a battery.

It is the energy a 12 volts battery can deliver continuously for 20 hours (C20) at 80°F (26.7°C) without falling below 10.5 volts.

The state when the battery's energy fall below 1.75 volts per cell (or 10.5 volts in a 6-cells 12 volts battery) is considered as "fully discharged".

There are several C-rating given to the batteries in relation to its AH, by giving the battery a specific load to discharge:
  • C3 means the battery will be fully discharged over a period of 3 hours.
  • C5 means the battery will be fully discharged over a period of 5 hours.
  • C8 means the battery will be fully discharged over a period of 8 hours.
  • C10 means the battery will be fully discharged over a period of 10 hours.
  • C20 means the battery will be fully discharged over a period of 20 hours.
  • C100 means the battery will be fully discharged over a period of 100 hours.
In normal practice, C20 is used to measure the AH of vehicle batteries.

The AH value among batteries with same size varies, and you should look for the one with higher AH value.


5. Reserve Capacity (RC)

RC is a general indicator of how long a fully charged new battery can continue to operate the vehicle's essential accessories when the alternator or fan belt fails.

It identifies how many minutes the battery can deliver a constant current of 25 amps at 80°F (26.7°C) without falling below the minimum voltage of 1.75 volts per cell in order to keep the vehicle running.

The higher the RC rating, the longer your vehicle can operate in the event your alternator or fan belt is faulty.


6. Cold Cranking Amps (CCA)

CCA is a rating used to define the battery's ability to start an engine in cold temperatures. It refers to the number of amps a 12-volt battery can deliver at 0°F (-17.8°C) for 30 seconds while maintaining a voltage of at least 7.2 volts.

The higher the CCA rating, the greater the starting power of the battery.

Battery starting power deteriorates as the battery ages. Therefore, a battery with higher starting power should give you more confidence over time.

Replacing a battery with one that has a lower CCA than the original may result in poor performance.


7. Price

Normally, the branded battery that suit with your car's alternator, with high performance rating, high AH, high RC and high CCA, will also come with a high price tag.

As such, you might want to strike a balance between the battery's price and its performance. Choose the above average battery, and it can probably last for at least 2 years.



Friday, May 3, 2019

DSN-less access to Microsoft SQL Server from Raspberry Pi Raspbian Stretch

Accessing Microsoft SQL Server (including SQL Server Express and Azure SQL Database) from Linux isn't that straightforward.

Microsoft does provide free connection library for Linux to access to SQL Server, but the library will not work for Raspbian OS in Raspberry Pi due to conflicting type definition with other essential library in the system.

Below is the easiest way I found out to access SQL Server from Raspberry Pi.

You need to enable and allow TCP/IP access to the SQL Server.

The SQL client to use is FreeTDS through unixODBC.

First of all, make sure the required FreeTDS and unixODBC components are installed in the Raspberry Pi:

sudo apt install freetds-bin freetds-dev tdsodbc unixodbc unixodbc-bin unixodbc-dev

If you are using Python to access the SQL Server, also need to install the following components:

sudo apt install python-pymssql python-pyodbc python-sqlalchemy

If you are using PHP to access the SQL Server, then need to install the following components:

sudo apt install php7.0-odbc

Since we are going to use the DSN-less method to access to the SQL Server, it is not necessary to configure the DSN settings. However, there is still minimal configuration needs to be made.

sudo nano /etc/odbc.ini

[FreeTDS]
Description = SQL Server
Driver = FreeTDS
Trace = No
TDS_Version = 7.0


For the TDS Version, if you are unsure about the version of your SQL Server, then set the TDS Version to 7.0.

You can set the TDS Version according to the following:

  • TDS Ver. 7.0 for SQL Server 7.0
  • TDS Ver. 7.1 for SQL Server 2000
  • TDS Ver. 7.2 for SQL Server 2005
  • TDS Ver. 7.3 for SQL Server 2008
  • TDS Ver. 7.4 for SQL Server 2012, 2014, 2016 or 2017
Lower TDS version has more compatibility but less feature in SQL command and less supported data types.

Another file you need to edit is odbcinst.ini.

sudo nano /etc/odbcinst.ini

[FreeTDS]
Description = FreeTDS unixODBC Driver
Driver = /usr/lib/arm-linux-gnueabihf/odbc/libtdsodbc.so
Setup =  /usr/lib/arm-linux-gnueabihf/odbc/libtdsS.so
fileusage=1
dontdlclose=1
UsageCount=1


Now you should be ready to access to your SQL Server from this Raspberry Pi.

Python test script:

import pyodbc

server = 'your_SQL_Server_IP_address'
database = 'your_database'
user = 'database_user'
password = 'database_user_password'
query = 'SELECT * FROM INFORMATION_SCHEMA.TABLES;'   # your SQL query

conn = pyodbc.connect('DRIVER={FreeTDS};SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+user+';PWD='+password+';')
cursor = conn.cursor()
cursor.execute(query)
for row in cursor.fetchall():
        print row


PHP test script:

$server = 'your_SQL_Server_IP_address';
$database = 'your_database';
$user = 'database_user';
$password = 'database_user_password';
$query = 'SELECT * FROM INFORMATION_SCHEMA.TABLES;';   // your SQL query


if ($db = odbc_connect('DRIVER={FreeTDS};SERVER='.$server.';PORT=1433;DATABASE='.$database.';',$user,$password)) {
        $res = odbc_exec($db, $query);

        while( $row = odbc_fetch_array($res) ) {
                print_r($row);
        }

        odbc_close($db);
}





Hint: Click on the "Older Posts" link to continue reading, or click here for a listing of all my past 3 months articles.