SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP syntax - referencing CSS amongst PHP loop code

    I have a page here, that uses a bit of PHP to loop a series of checkboxes.

    I've posted a screenshot here:

    http://www.handprintwebdesign.co.uk/gsg_checkboxes/

    I have the stripes working, but not the rules for the checkboxes so that they don't appear with a border in IE.

    So I have a couple of rules in my CSS:

    Code css:
    .tickbox_odd {
              border: 0;
              background-color:#006;
    }
    .tickbox_even {
              border: 0;
              background-color: #990000;
    }

    And at the moment, the code I have looks like:

    (the line in bold is the key line)
    PHP Code:
    while ($keyword=mysql_fetch_assoc($query)) {
     
     
        
    //If new category close previous row & display new category
        
    if ($keyword['Category']!=$current_category) {
            if (
    $current_category && $column !=) { echo "</tr>\n"; }
            
    $current_category $keyword['Category'];
            echo 
    "<tr class=\"categorycell\"><td colspan=\"10\">$current_category</td></tr>\n";
            
    $column 1$row_type="";
        }
     
     
    //Create new row if 1st keyword
        
    if ($column == 1) {
            
    $row_type = ($row_type=="odd")?"even":"odd";
            echo 
    "<tr class=\"".$row_type."\">";
        }
     
     
        
    //Display the checkbox
        
    echo "<td width=\"2%\">";
        echo 
    "<input type=\"checkbox\" class=\"tickbox_".$row_type."\"";
        if (
    in_array($keyword['ActivityID'],$photokeywords)) { echo " checked"; }
        echo 
    " name=\"ckbox[".$keyword['ActivityID']."]\" id=\"ckbox[".$keyword['ActivityID']."]\">";
        echo 
    "</td>\n";
     
     
        
    //Display the Keyword
        
    echo "<td width=\"18%\" align=\"left\">".$keyword['Activity']."</td>\n";
     
     
        
    //Close the row if 5th keyword OR increase column count
        
    if ($column == 4) { echo "</tr>"$column 1; }
        else { 
    $column++; }
    }
     
     
    if (
    $column != 1) { echo "</tr>"; } 
    But that isn't working - any help with this would be appreciated, just to get this detail tidied up.

    Thank you.
    Last edited by ScallioXTX; Mar 3, 2013 at 08:48. Reason: added bbcode for css and php

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,223
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Since you didn't bold any of the lines, I'm going to assume this is the key lines that are not working:
    PHP Code:
    if ($column == 1) {
    $row_type = ($row_type=="odd")?"even":"odd";
    echo 
    "<tr class=\"".$row_type."\">";

    Simply put, you forgot to prefix tickbox_ in your echo statement.

    You did it for the checkbox input type (but I wouldn't expect that to be very visible)

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    Since you didn't bold any of the lines, I'm going to assume this is the key lines that are not working:
    PHP Code:
    if ($column == 1) {
    $row_type = ($row_type=="odd")?"even":"odd";
    echo 
    "<tr class=\"".$row_type."\">";

    Simply put, you forgot to prefix tickbox_ in your echo statement.

    You did it for the checkbox input type (but I wouldn't expect that to be very visible)
    Sorry, the bold did go AWOL.

    Is that line not the alternating rows?

    The line I meant to bold was:

    PHP Code:
    echo "<input type=\"checkbox\" class=\"tickbox_".$row_type."\""

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to say I have now resolved this one.

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,223
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Cool, sorry, I didn't get as much of a chance as I'd like to look at it (work and life got in the way). What was the issue?

  6. #6
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,053
    Mentioned
    66 Post(s)
    Tagged
    0 Thread(s)
    Unless it is especially important that the alternating line colors show up in IE 7 or earlier, this sort of thing is better left to the nth-child psuedo-class.

    Code css:
    .tickbox {
              border: 0;
              background-color:#006;
    }
     
    tr:nth-child(even) .tickbox {
              background-color: #990000;
    }

    This way your PHP code doesn't even have to bother with this task, making it simpler to read and simpler to maintain.

    EDIT - corrected the CSS based on the HTML in your example.

    As an aside, you really need to learn how to do table-less layouts in CSS. It makes the server side code MUCH easier to read and write. Tables are for tabular data - which your example is not.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •