Monday, October 17, 2011

Determining if a Checkbox is Checked Using jQuery

A checkbox can be checked initially by using

<input type="checkbox" id="yourCheckBox" name="yourCheckBox" checked="checked" />

or

$("#yourCheckBox").attr('checked', true);


When one of the code above is used, the following two lines of code can fail in retrieving the correct value of the checkbox:

$('#yourCheckBox').attr("checked")

and

$('#yourCheckBox').val()


A better way is to use the following jQuery code instead:

$('#yourCheckBox').is(":checked")

My office mate and I took almost an hour to figure out that bug in our project code.

Saturday, October 8, 2011

Using and Creating a MySQL Dump

To use a database dump:
mysql -u <username> -p  <database_schema> < schema_dump.sql

To create a dump for the whole database:
mysqldump -u <username> -p  <database_schema> > schema_dump.sql

To create a dump of only a specific table of the database:
mysqldump -u <username> -p <database_schema> <table_name> > table_dump.sql

To create a compressed dump all of the databases on your MySQL:
mysqldump --all-databases -p | bzip2 -c > databasebackup.sql.bz2

Just make sure to enter the correct values on the parts enclosed between < and >.

Redirect to Login Page in Wordpress

Using PHP, you can use the following code to redirect a user to the log-in page before being able to access a page. It will go to the page the user is trying to access once he is logged-in.

<?php auth_redirect() ?>

Some say it doesn't work right for logged-in users.
Better add a checking just to make sure it works all the time:

<?php
  if ( !is_user_logged_in() ) {
    auth_redirect();
  }
?>

Refresh Web Page in JavaScript

Here's a very simple code:

window.location.reload(true);

So simple that you might not even care & easily forget about its syntax... that's why it's here. :)

List Recent Posts on Drupal 7 Using PHP

There's already a version 8 of Drupal but still, it's not easy to look for documentations of Drupal 7. They have a lot of methods from version 6 that I have to translate from my old code lately.

It almost frustrated me until I came up with the following code that works:

<?php
$query = db_select('node', 'n');

$nids = $query
    ->fields('n', array('nid'))
    ->orderBy('created', 'DESC')
    ->range(0, 7)
    ->execute()
    ->fetchCol('title');

$nodes = node_load_multiple($nids);
echo '<ul class="menu">';

foreach ($nodes as $recentNode) {
echo '<li class="first"><a href="?q='.drupal_lookup_path('alias',"node/".$recentNode->nid).'">'.$recentNode->title.'</a></li>';
}

echo '</ul>';

?>