ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

PHP parsing headache

July 04, 2007 6:45pm

Subscribe [2]
  • #1 / Jul 04, 2007 6:45pm

    SteveJohnson

    8 posts

    I thought this would have been easy, but noooooo!

    Here’s the basic setup: a single weblog, custom fields of project_client, project_name, project_priority, project_duedate, project_comments, and project_notes.

    I have to display a page of tables, one for each distinct client. The table shows ongoing projects. Each project uses two rows: project name, priority, due date, and comments. The next rows displays the notes field, spanning 4 columns.

    One of the conditions of the setup is that it be as easy as possible for my client—in other words, he can’t set up new weblogs, etc. He can enter new clients as members, though, and change the custom project_client dropdown field to add each new client.

    So far, so good. Here’s how I did it (this is the repeating section of the page). PHP on, parse on output:

    <div id="projectdiv">
    
    {exp:query sql="SELECT username, screen_name as company FROM exp_members WHERE group_id = '6' ORDER BY screen_name" }
    
    <table class="projects" width="670" border="0" cellpadding="0" cellspacing="0">
        <caption align="top">{company}  Project Report</caption>
        <tr class="tablehead">
            <td width="160" class="name">Name</td>
            <td width="100"><div align="center">Priority</div></td>
            <td width="110"><div align="center">Due Date </div></td>
            <td width="300"><div align="center">Comments</div></td>
        </tr>
        <?php $rowcolor = " odd"; ?>
        {exp:weblog:entries weblog="projects"}
    
        {if project_client == username}
        <tr valign="top" class="entry<?php echo $rowcolor; ?>">
            <td class="name">{title}</td>
            <td class="priority">{project_priority}</td>
            <td class="duedate">{project_duedate format="%m/%d/%Y"}</td>
            <td class="comments">{project_comments}</td>
        </tr>
        <tr class="notes<?php echo $rowcolor; ?>">
            <td colspan="4">Notes: {project_notes}</td>
        </tr>
        <?php $rowcolor = ($rowcolor == "") ? " odd" : ""; ?>
        {/if}
    
        {/exp:weblog:entries}
    </table>
    
    {/exp:query}
    
    </div>

    All fine and dandy, except for the alternating row colors. According to MY logic, the rowcolor switch should ONLY happen when the {if project_client==username} block is done. But it doesn’t work that way, it seems. Even if nothing (the two table rows) is output, the $rowcolor switch still executes. Because this code loops through all the entries, there may be 2 entries skipped, maybe 3, maybe 5, no telling. Each skip changes $rowcolor, so that only by chance do my row colors alternate.

    I’m open to any suggestions as to how to improve this, or redo it, or whatever. I just need to get it working soonly.

  • #2 / Jul 05, 2007 5:39am

    silenz

    1651 posts

    Because I’m too lazy to explain why this happens I’ll introduce you to your new friend the {switch}-variable 😉

    <tr valign="top" class="entry{switch="odd|"}">
  • #3 / Jul 05, 2007 7:42am

    Daniel Walton

    553 posts

    Yep, as silenz says if you just want alternating row colours use switch.  The reason it’s happening is because the php is parsed before the ee conditional.  You could try setting the custom fields to php variables and evaluating those instead.

  • #4 / Jul 05, 2007 9:53am

    SteveJohnson

    8 posts

    I know, switch is my friend. I’ve used it before. I just couldn’t figure out how to make it work with TWO rows. As it happens, the client changed his mind and now wants the same background with some whitespace between. Problem solved.

    Thanks for the replies.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases