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.