Pages

Handle PHP multiple choice select dropdown and checkbox

This took me hours to figure out that something was wrong in the PHP.

is_array() and count() can't really distinguish array() or string, if there is only one element.

For example.

$test = array('123');
is_array($test) will return true;
count($test) return 1

$test = '123';
is_array($test) will still return true.
count($test) will still return 1.


So there is no point to check a submitted field from PHP form as an array or a string.

This will cause problem when handling multiple choice select dropdown box or checkbox.

In PHP multiple choice field will need to be name with [] after the field.

For example:

state_id[]
In normal case, if the select box only accept one selection, the field name will be just state_id.

This confuse the PHP form validation. It returns just one string. So you will get error message when handling it with foreach loop.

for example
foreach($_POST['state_id'] as $state_id)
You will get an error like this.

Severity: Warning

Message: Invalid argument supplied for foreach()

Solutions:

No matter the select dropdown box allows to choice one or multiple options, always use multiple format. In other words, always use [] after the field name in the html form.

It will work with the validation process and treat the field as array as always.

CodeIgniter Cache Issues

Well! This is actually not an issues of CodeIgniter cache system. It's how I wrongly use the cache function.

Whenever you use db->cache_on() function to cache the current query. It will cache all the executed queries from this point for the rest of queries in the whole PHP page.

In other words, it will cache everything query that do not have db->cache_off(). WOW! What a big mistake.

So if you just want to cache the current query execution, remember to put db->cache_off() after executing the query. Always remember that!

for example:
$this->pf_db->db->cache_on();
$result = $this->pf_db->db->query($query);
$this->pf_db->db->cache_off();
Without the cache_off(), all the queries after this one will be cached!

Annoying bug in MYSQL 5.0.51b

Just realized that in MYSQL 5.0.51b, sorting data from INNODB on primary key with where clause will NOT work.

See other bug report at http://bugs.mysql.com/bug.php?id=31001

Basically, if you use where clause in the query like this. The primary won't sort.

SELECT * FROM (`store`) WHERE `publisher_id` = 1006 ORDER BY `store_id` desc
In the query, store_id is the primary key in the innodb.

If you remove the where clause, it will sort correctly.

WTF! This is a critical bug. How come MySQL don't catch that at first place. This bug seem to be there for a long time.

CodeIgniter Issues with .htaccess to display No input file specified. error

When running CodeIgniter on Apache 2.2 with PHP5, it display the following error when access url.

No input file specified.

System state:
Apache 2.2
PHP5.2 running as CGI mode with cgi.fix_pathinfo=1 according to PHPINFO().

Problems:
This problem is primary due to running PHP in CGI mode on Apache.

There are two solutions. No matter which one you choose, you will always have the same step 1.

Step 1: Obviously, PHP running under CGI mode has cgi.fix_pathinfo default to 1. Simpley open the /system/application/config/config.php file and make sure the following two options are set.

$config['index_page'] = "";

$config['uri_protocol'] = "AUTO";

Solutions 1:
Step 2: Create a customized PHP.INI file under the root directory of your site with the following line.

cgi.fix_pathinfo = 0;


Solutions 2:
Step 2:
Go to .htaccess file and add question mark after index.php and before slash.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]

Personally, I choose the Solution 1 with PHP.INI. It seems to run faster.

Problem solved!

PHP5 + Apache2.2 + MySQL5 Installation issue solved

I have tried many ways to get this working.

When install PHP5 + Apache2.2 + MySQL5, it shows that MySQL extension can not be loaded.

I followed every instruction to enable the extension in PHP.ini and point the Extension_dir to the right directory (ex. C:\PHP\ext). I also add the right model in Apache2.2 conf file to load the PHP5.

Problem:
PHP5 is unable to find the libmysql.dll and related MySQL extension. According to new PHP5 installation manual, you simply add your PHP5 installation path to the windows environment variables. Even you add your PHP5 installation path to the windows PATH environment variable, it still doesn't work.

Solution 1:
Copy libmysql.dll to your Windows System32 folder and restart your Apache2.2.

Warnning: This is not an ideal ways to solve the issues. You will run into situation that you need copy more dll to System32 folder. It is a night maire to upgrade the future versioin of PHP.

Solution 2:
After you set the PATH environment and restart your computer, it should just work without copying files to System32 folder. This is the reason, why it doesn't work.

Apache does not update the PATH environment until you reboot the server.
After you set the PATH environment variable, turn PHPINFO() and see if the Apache Environment variable is updated with the new path variable. If not, you will need to reboot the machine.

Problem solved! It's as simple as rebooting the machine.

MySQL Administrator :: server service or the configuration file could not be found

After install MySQL Administrator GUI tool, it is possible that you run into the error like the following when you login into the tool.




The solution is simple. Try login to the server without the login and password.


Remove the username and password and press control key. The "Cancel" button turns into a "Skip" button.

Press Skip and it will take you to the interface that you can manage and make correction to the config file. In most of the case, the default ini file is pointing to my.cnf. Change it to my.ini.




Problem solved!

Unable to Install WIndows Live Writer on XP SP3

I was using Windows Live Writer for sometime on XP SP2. After installing SP3, many programs stop working. One of them is the Windows Live Writer. I keep getting Windows Installer error and Windows Live Setup service errors in the event log.

It seems that when running Windows XP SP3 many people are having issues installing windows live applications using the windows live installer. After much googling I have come across a solution. This also solves issues that exist with the windows update service after installing SP3.

Essentially what you need to do is:

1. Download and run the Windows Live Installer Cleanup tool from:

2. Reboot

3. Click START>Run and type 'command' to load the console.

4. Type in:

regsvr32 wups2.dll

and press enter.

5. Type exit to leave the console

6. Download and run the windows live installer, it should now work perfectly.