Wednesday, March 25, 2009

PHP get number of lines in a text file

Counting number of lines in a text file is easy.

Here is the code:

$file = "file.txt";
$nr_lines = count(file($file));

Now you know how to count the number of lines in a text file with PHP.

Monday, March 23, 2009

Synchronize MySQL structure

Here is the scenario:

You have a localhost database, that is your development database.
And you have a production database, somewhere on a server.

After you've done some updates on your localhost development mysql database, you want to synchronize with the production database.

You can do that very easy using Navicat for MySQL. Tools -> Data Synchonization. You cand download it, it is fully functional for 30 days, after that you'll have to buy it.

So no more searching for altered tables, new tables, deleted fields, just synchronize your database easy.

Friday, March 20, 2009

First letter uppercase in MySQL

So you have all types of string in a mysql database.

Eg:

MySQL
MYSQL
mysql
MySqL

And you want to make the first letter of each record uppercase and the rest lowercase.
Here is the update syntax for this job:

UPDATE mytable SET myfield=CONCAT(UPPER(SUBSTRING(myfield, 1, 1)), LOWER(SUBSTRING(myfield FROM 2)))

Thursday, March 12, 2009

MySQL implode like function

If you need an implode like function in MySQL, there is a solution: GROUP_CONCAT

GROUP_CONCAT concatenates a string by comma (,).

So if you had a table tags (id, tag)

1 test
2 test2
3 test3

SELECT GROUP_CONCAT(tag) FROM tags

The result would be: test,test2,test3

Pretty cool, huh?

Friday, March 6, 2009

Javascript back button

Create a link or a button to go to the previous page, using this code:
<a href="javascript:history.go(-1)">Back</a>



This way, the visitor can go back to the previous page without pressing the "Back" button of his browser.