SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Where do I add the brackets?

    Hi all

    I have a small issue trying to add php code within html code within a php variable. If that makes sense.
    Just can't get this to work. Does anybody know how to add the correct brackets or what I need?

    I've broken this down for easy viewing.

    Code PHP:
    <?php
    $breadcrumbs = '<li>
      <a href="http://www.example.com/venue/<?= $row['venue_name'] ?>">
        <span><?= $row['venue_id'] ?></span>
      </a>
    </li>';
    ?>

    Many thanks, Barry
    The more you learn.... the more you learn there is more to learn.

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,447
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Hi Barry,

    You don't need to include the php tags. If you use double-quotes PHP will parse any variables it finds within the string. When using associative arrays, you either need to leave off the quotes around the key, or enclose them with braces:
    PHP Code:
    $breadcrumbs "<li>
      <a href='http://www.example.com/venue/
    $row[venue_name]'>
        <span>
    {$row['venue_id']}</span>
      </a>
    </li>"


  3. #3
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool. Thanks seems so simple now

    The problem I'm having is I need to use single quotes as I have a lot of markup/attributes wrapped up within this and many other variables using double quotes.
    Wouldn't that mean I'd need to escape every double quote to avoid any errors? That was the reason to use double quotes.

    ?

    Cheers
    The more you learn.... the more you learn there is more to learn.

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,447
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    You could use single quotes around your html attributes, like I've done in the example above. The other option is to use string concatenation:
    PHP Code:
    $breadcrumbs '<li> 
      <a href="http://www.example.com/venue/'
    .$row['venue_name'].'"> 
        <span>'
    .$row['venue_id'].'</span> 
      </a> 
    </li>'


  5. #5
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So many ways. Always good to know thanks.
    Seems to be working now well no errors but the values within the link and span are empty.

    Have we missed something? Do we need to echo them?

    Barry
    The more you learn.... the more you learn there is more to learn.

  6. #6
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,447
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Hmm.. no you don't need to echo them. It seems more likely that those values aren't set for some reason. You could try:
    PHP Code:
    var_dump($row); 
    and see what you get.

  7. #7
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had the snippet above the SQL so the row had no value.

    Thanks.

    And just one question. You mention:
    When using associative arrays, you either need to leave off the quotes around the key...
    What do you mean when you say key? What are you referring to?
    I keep hearing this time and time again but not sure what it is even though I'm probably using it all the time.

    Cheers, Barry
    The more you learn.... the more you learn there is more to learn.

  8. #8
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,447
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Quote Originally Posted by computerbarry View Post
    What do you mean when you say key? What are you referring to?
    I keep hearing this time and time again but not sure what it is even though I'm probably using it all the time.
    Key is just the name given to the 'label' that is used to reference a value in an array. In the case of associative arrays like you're using in your example, the key is a string, but it can also be a number as in the case of indexed arrays. With your array $row, the string venue_name is an array key.

  9. #9
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right ok.
    That makes a bit more sense now.

    I appreciate your time and information fretburner, thank you

    Barry
    The more you learn.... the more you learn there is more to learn.

  10. #10
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I'll add another option: heredoc which is useful if you have a large block of text or a lot of mixed quotes.
    - Robert


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
  •