SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pass variable value on css class

    Hello I am using the following working code and i would like to append the value of variable $something on the class of "<div class=\"lcp-customfield\">"

    Code:
      foreach ($custom_array as $something) :
            $my_custom_field = $custom_fields[$something];
            if (sizeof($my_custom_field) > 0 ):
              foreach ( $my_custom_field as $key => $value ) :
                $lcp_customs .= "<div class=\"lcp-customfield\">" . $value . "</div>";
              endforeach;
            endif;
          endforeach;

    What would be the correct syntax for this?

    many thanks
    Andy

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2011
    Location
    Kolding, Denmark
    Posts
    108
    Mentioned
    6 Post(s)
    Tagged
    0 Thread(s)
    Same as other strings, I guess.
    PHP Code:
    $lcp_customs .= '<div class="lcp-customfield"'.$something.'>' $value '</div>'

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i get confused with the backslashes.. why does my original code "<div class=\"lcp-customfield\">" have backslashes and your code doesn't ?

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    66 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by neodjandre View Post
    i get confused with the backslashes.. why does my original code "<div class=\"lcp-customfield\">" have backslashes and your code doesn't ?
    Because he starts off the string with single quote (') rather than a double quote (") which is used by css, which changes what needs escaped. If the html / css code required a single quote, then you would be doing some escaping with his code as well.

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,143
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by neodjandre View Post
    i get confused with the backslashes.. why does my original code "<div class=\"lcp-customfield\">" have backslashes and your code doesn't ?
    Don't fret about that too much, as it is purely a style of coding technique. He chose one that didn't require backslashes. If you want it to still use backslashes, you the following does exactly the same thing
    PHP Code:
    $lcp_customs .= "<div class=\"lcp-customfield" $something "\">" $value "</div>"
    I wrote the above in a way that was very familiar to what you started with.

    It could also be written like so:
    PHP Code:
    $lcp_customs .= "<div class=\"lcp-customfield{$something}\">{$value}</div>"

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks a lot everyone - this is all clearer now thanks to you !


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
  •