Search code examples
phphtmltabbing

Do you bother with markup formatting?


I am a front end guy who is getting more and more into scripting and that being the case, I like my regurgitated markup to kind of look nice.

I ran a loop over some database values for a list and while most sites would just show a big old concatenated slew of <LI> tags back to back, I kind of like them \r\n distanced with proper \t tabbing. Weird thing is, the first list member renders like LI> rather than <LI> about 1 out of 5 page serves.

Anyone seen this? Should I not bother? Am I formatting the loops badly? Here's an example:

while ($whatever = mysql_fetch_array($blah_query)){
    echo "\t\t\t\t\t\t";
    echo "<li>\n";
    echo "\t\t\t\t\t\t";
    echo '<a href="#'.$whatever['name'].'" id="category_id_'.$whatever['id'].'">';
    echo ucfirst($whatever['name']);
    echo "</a>\n\t\t\t\t\t\t</li>\n";
}

Solution

  • this seems as if the goal is to output a page source that types out the proper indentions for you? at least for right now to debug and be easier read?

    while ($whatever = mysql_fetch_array($blah_query)){
        echo "\t\t\t\t\t\t";
        echo "<li>\n";
        echo "\t\t\t\t\t\t";
        echo '<a href="#'.$whatever['name'].'" id="category_id_'.$whatever['id'].'">';
        echo ucfirst($whatever['name']);
        echo "</a>\n\t\t\t\t\t\t</li>\n";
    }
    

    since you're using PHP to echo out those HTML codes, just type them as you would see them on the page source

    while($whatever = mysql_fetch_array($blah_query)){
        //When you want a new line, just hit enter. PHP will echo the carriage returns too
        echo'
                            <li>
                                <a href="#'.$whatever['name'].'" id="category_id_'.$whatever['id'].'">ucfirst($whatever['name'])</a>
                            </li>
    ';
    }
    

    this is how I would do it so that it would line break every time including the first time incase I have a left over "</div>" or some other closing tag without a line break after it.

    it will output a nicer clean list item that tabbed in with the breaks