Archive for January, 2009

How to disable “inactivity” screen lock on Fedora (KDE)

Monday, January 26th, 2009

On my development machine which runs Fedora code 8 used to prompt me every once in a while for password. This is very annoying but secure :)

Here are the steps how to disable that screen saver password prompt

Click "K" > Control Center > Appearance & Themes > Screen Saver

Under Settings uncheck "Require password to stop"

Related

  • http://www.databook.bz/?page_id=219
  • http://forum.soft32.com/linux/disable-inactivity-screen-lock-Fedora-Core-ftopict357046.html

Add an URL in Perl CPAN module mirror list

Friday, January 23rd, 2009

[code]
perl -MCPAN -e shell

o conf urllist push ftp://ftp-mirror.internap.com/pub/CPAN/
o conf commit

[/code]

How to get the first element of an associative array in PHP

Saturday, January 10th, 2009

I've been always wondering how to get the first element of an associative array in PHP.
For numeric keys you could just do $arr[0] but it'll a little bit different for associative arrays.

Solutions

[code]
$arr = array('one' => 'hello', 'two' => 'world');
[/code]

Solution #1
[code]
reset($arr);
list ($key1, $val1) = each($arr);
[/code]

Solution #2
[code]
$i = 0;
foreach ($arr as $key => $value) {
if (++$i == 1)
echo $key;
break;
}
}
[/code]

Solution #3
[code]
reset($arr);
echo key($arr);
[/code]

Solution #4
[code]
reset($arr);
echo @array_shift(array_keys($arr));
[/code]

Suppressing Warnings such as:
Debug Strict (PHP 5): PHPDocument1 line 6 - Only variables should be passed by reference

Solution #5
[code]
reset($arr);
echo end(array_keys(array_reverse($arr)));
[/code]

Most of solutions can be found on http://www.webmasterworld.com/forum88/3811.htm

Related

Can you come up other solutions ?
Feel free to share your experience by commenting this posting below.

How to get the total number of rows of a complex query with joins, group …

Tuesday, January 6th, 2009

Many times we need just the total number of rows that are produced by a single SELECT statement.
If you have even just a little experience with MySQL or other database(s) you'll think of COUNT(*) or similar function.

The query below uses COUNT(*) function but does not return the total number of rows.
It actually returns how many orders each customer has made.
When a query contains GROUP BY clause the situation changes.

[code]
mysql> SELECT c.*, COUNT(c.id) as cnt FROM `test_orders` AS o INNER JOIN test_customers AS c ON o.customer_id = c.id GROUP BY c.id;
+----+-----------+------------------------+-----+
| id | cust_name | cust_email | cnt |
+----+-----------+------------------------+-----+
| 1 | Slavi | slavi at slavi dot biz | 2 |
| 2 | Test | test at test dot com | 1 |
+----+-----------+------------------------+-----+
2 rows in set (0.00 sec)
[/code]

What if we want to create a pagination ?
Then we'll need to calculate the total number of rows for this query.

Solutions:
#1 Putting the main query into a subquery

[code]
SELECT count(*) as total_rows FROM (SELECT c.*, COUNT(c.id) as cnt FROM `test_orders` AS o INNER JOIN test_customers AS c ON o.customer_id = c.id GROUP BY c.id) as tab
[/code]

#2 Using MySQL's SQL_CALC_FOUND_ROWS and performing a second query
[code]
SELECT SQL_CALC_FOUND_ROWS c.*, COUNT(c.id) as cnt FROM `test_orders` AS o INNER JOIN test_customers AS c ON o.customer_id = c.id GROUP BY c.id LIMIT 10;
[/code]

[code]
mysql> SELECT SQL_CALC_FOUND_ROWS c.*, COUNT(c.id) as cnt FROM `test_orders` AS o INNER JOIN test_customers AS c ON o.customer_id = c.id GROUP BY c.id;
+----+-----------+------------------------+-----+
| id | cust_name | cust_email | cnt |
+----+-----------+------------------------+-----+
| 1 | Slavi | slavi at slavi dot biz | 2 |
| 2 | Test | test at test dot com | 1 |
+----+-----------+------------------------+-----+

mysql> SELECT FOUND_ROWS();
+--------------+
| FOUND_ROWS() |
+--------------+
| 2 |
+--------------+
1 row in set (0.00 sec)
[/code]

Note:
For "mysql>" is means that the commands are executing in a MySQL console.

Database Scheme

[code]
--
-- Table structure for table `test_customers`
--

CREATE TABLE `test_customers` (
`id` int(11) NOT NULL auto_increment,
`cust_name` varchar(255) NOT NULL,
`cust_email` varchar(255) NOT NULL,
KEY `id` (`id`,`cust_name`,`cust_email`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `test_customers`
--

INSERT INTO `test_customers` (`id`, `cust_name`, `cust_email`) VALUES
(1, 'Slavi', 'slavi at slavi dot biz'),
(2, 'Test', 'test at test dot com');

-- --------------------------------------------------------

--
-- Table structure for table `test_orders`
--

CREATE TABLE `test_orders` (
`order_id` int(11) NOT NULL auto_increment,
`customer_id` int(11) NOT NULL,
`order_date` datetime NOT NULL,
PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `test_orders`
--

INSERT INTO `test_orders` (`order_id`, `customer_id`, `order_date`) VALUES
(1, 1, '2009-01-06 20:00:00'),
(2, 1, '2009-01-06 20:30:00'),
(3, 2, '2009-01-06 19:00:00');
[/code]

Related

How to Remove Empty Elements From An Array in PHP

Tuesday, January 6th, 2009

Sometimes arrays get filled with empty values and we need to remove them.
Here is a function that removes all empty values in an array.
The text is cleaned by HTML if any.

Notes:

  1. There is not a check for duplicate elements.
  2. This function may break UTF-8 strings.

[code]
$a[5] = 777;
$a[4] = array(
1 => 'test3',
2 => '',
3 => '',
4 => 'test2',
9 => 'test',
17 => '',
);
$a[9] = 'test';
$a[1] = 111;
$a[2] = 222;
$a[7] = '';
$a['axa'] = 'sfaf';
$a['axgga'] = '';
$a['gsdgsdg'] = 'sfaf';
[/code]

// Output
[code]
array(7) {
[0]=>
string(3) "777"
[1]=>
array(3) {
[0]=>
string(5) "test3"
[1]=>
string(5) "test2"
[2]=>
string(4) "test"
}
[2]=>
string(4) "test"
[3]=>
string(3) "111"
[4]=>
string(3) "222"
["axa"]=>
string(4) "sfaf"
["gsdgsdg"]=>
string(4) "sfaf"
}
[/code]

[code]
/**
* Removes empty values from an array. Also the numeric keys get reordered.
* If a value is an array the process of cleaning is repeated recursively.
* Elements start from 1 not from 0 if $non_zero_start is set to 1. Defaults to 0.
* Values are cleaned by HTML and also by leading/trailing whitespaces.
*
* @param array $arr
* @param bool $non_zero_start if 1 is supplied the array will start from 1
* @return array
* @author Svetoslav Marinov
* @copyright January 2009
* @license LGPL
*/
function cleanupArray($arr, $non_zero_start = 0) {
$new_arr = array();
foreach ($arr as $key => $value) {
if (!empty($value)) {
if (is_array($value)) {
$value = cleanupArray($value, $non_zero_start);

// If after the cleaning there are not elements do not bother to add this item
if (count($value) == 0) {
continue;
}
} else {
$value = trim(strip_tags($value));
if (empty($value)) {
continue;
}
}

$new_arr[$key] = $value;
}
}
// Reordering elements
if (!empty($new_arr)) {
if (!empty($non_zero_start)) {
$new_arr = array_merge_recursive(array("") + $new_arr);

// We don't need an empty first element.
// This was used to shift other elements to start from 1 instead of 0
unset($new_arr[0]);
} else {
$new_arr = array_merge_recursive($new_arr);
}
}
return $new_arr;
}
[/code]

Related:

How to use Wordpress and FeedBurner an .htaccess solution

Friday, January 2nd, 2009

I recently added my RSS feeds to FeedBurner .
Here is what is worked for me.

My blog is located in /blog directory you may need to change RewriteBase if your blog is installed in another directory. Additionally you'll have to change YOUR_BLOG_ID to match your provided by FeedBurner.

In the .htaccess rules we allow FeedBurner itself to retrieve the feeds otherwise there will be an infinite loop.
[code]
RewriteEngine On
RewriteBase /blog/

# Enable FeedBurner services
RewriteCond %{HTTP_USER_AGENT} !^(FeedBurner|FeedValidator)
RewriteRule ^feed http://feeds.feedburner.com/YOUR_BLOG_ID [R=307,L]
[/code]

Related