Adil's blog Blog about web-development.

18Jun/100

Ignoring browser commands

I have rarely encountered such situation, but for larger projects it is certainly common problem. Assume that the script performs some queries in the database (deleting / adding / editing) and during these user presses the Stop button in your browser. By this command php script completes its work, which in our case may be fraught with violations of the integrity of the database.

This problem is resolved by function ignore_user_abort ():

ignore_user_abort (true);

... Is executed queries to the database ...

ignore_user_abort (false);

Original source: http://i-novice.net/poleznye-php-tryuki/

Note: Best solution for such problems is using transaction on databases :)

18Sep/091

fixed position on IE6

Fixed position on xhtml elements make the elements fixed at its coordinates and it is not moved when you scroll the pages. One of popular example can be facebook panel.

#elementId {
    position:fixed;
    bottom:0;
}

An example above will make the element stayed at the bottom of page.

But there is an issue on IE6 and this can be fixed via javascript. Simply get the javascript code from here and include to your page:

<script type="text/javascript" src="fixed.js"></script>

Reference:

http://www.doxdesk.com/software/js/fixed.html

Filed under: Tips & tricks 1 Comment
14Sep/090

Regular expressions in SQL queries

Regular expressions are useful for parsing string, matching patterns, validating etc. If we talk about strings so, I think it's good idea using it in SQL queries.

Let we have table users:

mysql> SELECT * FROM users;
+----+-----------+----------+----------------------+
| id | first     | last     | email                |
+----+-----------+----------+----------------------+
|  1 | John      | SMITH    | jsmith@example.az    |
|  2 | Bill      | Gates    | bgates@example.ru    |
|  3 | Steve     | Jobs     | sjobs@example.de     |
|  4 | Sylvester | Stallone | stallone@example.com |
|  5 | Chuck     | Norris   | cnorris@example.net  |
|  6 | Bruce     | Lee      | blee@example1.net    |
+----+-----------+----------+----------------------+
6 rows in set (0.00 sec)

Now we want to get users whose email ends with three-letter TLD (.com,.net,.org etc). We can do this as follows:

mysql> SELECT * FROM users WHERE email LIKE '%.___';
+----+-----------+----------+----------------------+
| id | first     | last     | email                |
+----+-----------+----------+----------------------+
|  4 | Sylvester | Stallone | stallone@example.com |
|  5 | Chuck     | Norris   | cnorris@example.net  |
|  6 | Bruce     | Lee      | blee@example1.net    |
+----+-----------+----------+----------------------+
3 rows in set (0.00 sec)

Well.

What can we do, if we need get users whose email ends with digit dot three-letter TLD(blee@example1.net)? ooops.

I'll not lie, I don't know how to do it with ANSI SQL and I'll use RDBMS specific functions. This example works for MySQL:

mysql> SELECT * FROM users WHERE email REGEXP '^.+[0-9]\.[a-z]{3}$';
+----+-------+------+-------------------+
| id | first | last | email             |
+----+-------+------+-------------------+
|  6 | Bruce | Lee  | blee@example1.net |
+----+-------+------+-------------------+
1 row in set (0.00 sec)

For Oracle it will as follow:

SELECT * FROM users WHERE REGEXP_LIKE(email,'^.+[0-9]\.[a-z]{3}$')

Thanks for my co-worker Mahir for the idea ;)

Reference:
http://www.oracle.com/technology/oramag/webcolumns/2003/techarticles/rischert_regexp_pt1.html
http://dev.mysql.com/doc/refman/5.1/en/regexp.html

21Jul/091

font-size and print “toolbar” :)

Sometimes users need to make the font size larger for easy reading the text. It happens usually in news portals or some web sites like the web site of my university www.bsu.edu.az.

And also it's better to make the print version of the page. When saying print version it means that black text on white background without any styles and something else.

Let's begin. Simply insert this code to your HTML page:

<script type="text/javascript" src="http://tpl.bdu.az/toolbar.js"></script>
<div style="text-align:right" id="adilToolbar">
<a href="javascript:adilTextDec('content')"><img src="http://tpl.bdu.az/images/font_size_less.png"/></a>
<a href="javascript:adilTextInc('content')"><img src="http://tpl.bdu.az/images/font_size_up.png"/></a>
<a href="javascript:adilTextPrint('content')"><img src="http://tpl.bdu.az/images/printer.png"/></a>
</div>

toolbar.js:

 
var adilTextSize = 100; // general variable for saving the current textsize by percents.
 
function adilTextInc(objid) {
	// I think to make the font size 200% larger is enough 
	if (adilTextSize <=200)	adilTextSize += 20; // increment by 20%
	document.getElementById(objid).style.fontSize = adilTextSize + '%';
}
 
function adilTextDec(objid) {
	if (adilTextSize >=80) adilTextSize -= 20; // decrement by 20%
        document.getElementById(objid).style.fontSize = adilTextSize + '%';
}
 
function adilTextPrint(objid) {
	// open new window
	var doc=window.open('','name','height=600,width=800');
 
	doc.document.write('<html><head><title>Print a page</title>');
	doc.document.write('</head><body>');
 
	// copy the content of the page
	doc.document.write(document.getElementById(objid).innerHTML);
 
	// remove out toolbar if it's there
 
	doc.document.write('<script type="text/javascript">');
	doc.document.write('try{document.getElementById(\'adilToolbar\').style.display=\'none\';}catch(e){}');
	doc.document.write('</script>');
 
	doc.document.write('</body></html>');
 
	// flush the input
	doc.document.close();
 
	// print the page
	doc.print();	
}

P.S. The button for decrement of the font-size is for eagles. :-P