PHP
Problem adding column values in MySQL SELECT query / sorting by added column values
I wanted to sort an sql query according to added column values. Like this
<?php
$query = "SELECT * FROM myTable1 AS my1 ".
"LEFT JOIN myTable2 AS my2 ON my2.id = my1.id ".
"LEFT JOIN myTable3 AS my3 ON my3.id = my1.id ".
"ORDER BY (my2.price + my3.price)";
?>Setting up crontab in Drupal
Not knowing so much about Unix commands and such setting up crontabs for your Drupal site can be a hassle. I recently noticed that my supposed crontab run wasn't functioning as expected. Patching together intelligence from several sites I finally managed to make something useful at last.
First create directory and the file that the crontab will call, in my case it was put in a bin directory on the same level as my public_html directory.
$ mkdir bin
$ cd bin
$ pico weekly.crontab.sh
Letting primary links be active / highlighted when browsing sub paths by overriding theme_links() function in Drupal
I wanted to highlight the primary links when browsing the secondary links (or sub links of the primary links). After some googling I understood that I had to override the theme_link() function. The overriding basically looks if the first slash text is in the in the link url currently active in the loop. All this is dependent on URL aliases being activated. It then proceeds to add the active class to the anchor.
After I messed with the theme_link() function I placed it in the template.php page which now looks likes this
<?php
Search and replace for characters in the whole database for Drupal
<?php
# Define which characters you want changed (key) and what you want them changed to (value)
$charChangeArray = array(
'Ã¥'=>'å',
'Ã…'=>'Å',
'ö'=>'ö',
'Ö'=>'Ö',
'ä'=>'ä',
'Ä'=>'Ä',
'ç'=>'ç',
'é'=>'é',
'¿½'=>'è',
'â'=>'â',
A script for converting your MySQL database collation to utf8 and utf8_general_ci and table columns to uf8_bin in Drupal
I recently had to migrate a database from one server to another. The new server was MySQL 5.xxx and I got the chance to change the collation and character set of my database. Being of Swedish origin there's always a hassle with å,ä,ö letters.
Checking elements in an array, vector, in PHP against entries, column values, in a MySQL table
Basically what I wanted to do was to check if all the elements in the array was in the MySQL table and if so update it and if it not existed enter the values into the table.
The table consisted of two columns id and frequency. The procedure would be update - Add one to frequency, insert - Add new id and set frequency to one.
Now I could do this with a foreach statement and probing the table for each element but as the array/vector was quite large that would mean a lot of SELECTS and calls to the database which didn't sound very efficient to me.