Search code examples
phphtmlcssgravityforms

Adding CSS class as a variable for PHP loop


I have a PHP function that takes a table that has been filtered using gravity flow. This function loops through each row of the table. I want to change the styling of this table. I have a specific CSS class I want the table styling to have. This class is "tr-shadow". I would apply this CSS class to each row of the table. I have a $style variable that is used for the CSS. How do I add the CSS class as a string for the $style variable so that the table can print out with the CSS from that class? Here is the code below.

'''

public function single_row_columns( $item ) {

list( $columns, $hidden ) = $this->get_column_info();

    foreach ( $columns as $column_name => $column_display_name ) {
        $class = "class='$column_name column-$column_name'";

        $style = "class='tr-shadow'";
        if ( in_array( $column_name, $hidden ) ) {
            $style = "class='tr-shadow'";
        }

        $data_label = ( ! empty( $column_display_name ) ) ? " data-label='$column_display_name'" : '';

        $attributes = "$class$style$data_label";

        if ( 'cb' == $column_name ) {
            echo '<th data-label="' . esc_html__( 'Select', 'gravityflow' ) . '" scope="row" class="check-column">';
            echo $this->column_cb( $item );
            echo '</th>';
        } elseif ( method_exists( $this, 'column_' . $column_name ) ) {
            echo "<td $attributes>";
            echo call_user_func( array( $this, 'column_' . $column_name ), $item );
            echo '</td>';
        } else {
            echo "<td $attributes>";
            echo $this->column_default( $item, $column_name );
            echo '</td>';
        }
    }
}

''' As you can see I have already tried adding "$style="class='tr-shadow". However, this doesn't add any style to the tables. I am assuming that I am not correctly formatting the $style variable in a way that the css class is recognizable. How can I use the $style variable to successfully output the css class to each row of the table.


Solution

  • Your $style variable is including the attribute style again, which will result in something like:

    class="column_name column-bar class=' ... 
    

    which is invalid.

    I'd recommend changing your code to something like this:

    public function single_row_columns( $item ) {
    
    list( $columns, $hidden ) = $this->get_column_info();
    
        foreach ( $columns as $column_name => $column_display_name ) {
            $class = "$column_name column-$column_name";
    
            $class .= " tr-shadow";
            if ( in_array( $column_name, $hidden ) ) {
                $class .= " tr-shadow";
            }
    
            $data_label = ( ! empty( $column_display_name ) ) ? " data-label='$column_display_name'" : '';
    
            $attributes = "$data_label";
    
            if ( 'cb' == $column_name ) {
                echo '<th data-label="' . esc_html__( 'Select', 'gravityflow' ) . '" scope="row" class="check-column">';
                echo $this->column_cb( $item );
                echo '</th>';
            } elseif ( method_exists( $this, 'column_' . $column_name ) ) {
                echo "<td class=\"$class\" $attributes>";
                echo call_user_func( array( $this, 'column_' . $column_name ), $item );
                echo '</td>';
            } else {
                echo "<td class=\"$class\" $attributes>";
                echo $this->column_default( $item, $column_name );
                echo '</td>';
            }
        }
    }
    

    Also, it looks like class is always tr-shadow regardless of what you do.