donotturnoff

My worst code

This is probably the worst code snippet I've ever written.

<?php
if ($addHelpTooltips) {
    echo "<script type=\"text/javascript\">function loadTooltips() {";
    if (($handle = fopen("../tooltips.csv", "r")) !== false) {
        echo "var tooltips = {\"\":\"\"";
        while (($helpItem = fgetcsv($handle)) !== false) {
            echo ",\"" . $helpItem[0] . "\": \"" . $helpItem[1] . "\"";
        }
        fclose($handle);
        echo "};";
?>
    var helpElements = document.getElementsByClassName("help");
    for (var i = 0; i < helpElements.length; i++) {
        helpElements[i].title = (tooltips[helpElements[i].id] !== undefined) ? 
            tooltips[helpElements[i].id] : "No help available for this element";
    }
<?php
    }
    echo "} document.addEventListener(\"DOMContentLoaded\", loadTooltips, false);</script>";
}?>

It is a PHP script which dynamically produces JavaScript which dynamically adds in tooltips to page elements once the DOM has loaded. Some of that code is PHP, some is JavaScript, and some is HTML, and they're all mixed together with no indication of what's where. However, it worked. I ended up re-writing it for the sake of maintenance.