ported php to new table structure