Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End PHP array help

  • This topic is empty.
Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #170510
    Jamie
    Participant

    I need some help with some php code. I am using Option tree with WordPress. They have a social option in the back end where you can press a button to add a new option. I have two options right now. When I do print_r on the variable, I get this

    Array
    (
        [0] => Array
            (
                [name] => Facebook
                [title] => 
                [href] => 
            )
    
    

    [1] => Array
    (
    [name] => Twitter
    [title] =>
    [href] =>
    )
    )
    `

    My problem is that I am not sure of the most efficient way to use this information. I thought about trying to put it in a switch statement. But I am not sure how big it could or would be. Each one would need to be echoed in an unordered list because each list item would have an icon associated with it.

    #170511
    chrisburton
    Participant

    I’m not really understanding your problem. What are you expecting to happen with the array?

    #170513
    __
    Participant

    Likewise. But from what I can take away from it, a switch is not what you need. Can you describe the HTML markup do you want to end up with?

    #170515
    Jamie
    Participant

    There needs to be some loop that will pick up when a new option is added in the back end. It will output this

    <li><a href="facebook.com"><img src="facebook.jpg">Facebook</a>

    So then if a Twitter option was added in the back end, the code would output a list item for twitter. So I need to read the array and cycle through the index to get what I need.

    #170535
    __
    Participant

    When you want to make sure you do something with every element of an array, use foreach.

    <?php
    
    foreach( $array as $item ){
        /*  do something to each item in array  */
    }
    

    e.g.,

    <?php
    
    $LIs = "";
    foreach( $yourArray as $item ){
        $LIs .= <<< HTML
    <li>
        <a href="{$item['href']}">
            <img src="{$item['img']}">
            {$item['name']}
        </a>
    </li>
    HTML;
    }
    print "<ul>$LIs</ul>";
    
    #170545
    Jamie
    Participant

    I have tried that and I get an error. Array to string conversion.

    #170548
    __
    Participant

    Can you show us the actual code you are working with (use a service like pastebin or make a gist on github if there is too much code to post here)?

    #170605
    shaneisme
    Participant
    $LIs .= &lt;&lt;&lt; HTML
    <li>
        <a href="{$item['href']}">
            <img src="{$item['img']}" />
            {$item['name']}
        </a>
    </li>
    HTML;
    

    Is this a method of doing HTML inside PHP that I don’t know about, or is this pseudocode? I tried doing some Googling on it and couldn’t find anything.

    #170618
    __
    Participant

    Sorry; it’s called a heredoc.

    I’ve developed a habit of using them, as it makes it very easy to write large blocks of HTML (since you can use variables, single, and double-quotes).

    #170619
    shaneisme
    Participant

    Awesome! New toy, thanks :)

    #170638
    chrisburton
    Participant

    I think it’s harder to read (as a beginner)

    #170642
    __
    Participant

    I think it’s harder to read (as a beginner)

    It certainly is harder to figure out, especially for beginners. (That’s why I said sorry for using it: it’s not the best choice for example code.) But once you understand how to use the delimiters (i.e., <<< SOME_WORD and SOME_WORD;), it gets very quick and convenient.

Viewing 12 posts - 1 through 12 (of 12 total)
  • The forum ‘Back End’ is closed to new topics and replies.