WordPress.org

Ready to get started?Download WordPress

Forums

Foreach with multiple arrays custom field (14 posts)

  1. Fask
    Member
    Posted 2 years ago #

    $href = array('http://www.youtube.com/watch?v=yC3_gkdtlua', 'http://www.youtube.com/watch?v=yC3_gkdtlub', 'http://www.youtube.com/watch?v=yC3_gkdtluc', 'http://www.youtube.com/watch?v=yC3_gkdtlud');
    $name = array('1', '2', '3', '4');

    How do I make it so that I can combine both arrays to make a link?
    I want the output to be:

    <a href="http://www.youtube.com/watch?v=yC3_gkdtlua">1</a>
    <a href="http://www.youtube.com/watch?v=yC3_gkdtlub">2</a>
    <a href="http://www.youtube.com/watch?v=yC3_gkdtluc">3</a>
    <a href="http://www.youtube.com/watch?v=yC3_gkdtlud">4</a>

    Foreach only works with one array. This is the entire code:

    [Code moderated as per the Forum Rules. Please use the pastebin]

  2. bcwp
    Member
    Posted 2 years ago #

    Use a good ol' fashioned for loop:

    if ( count( $href ) === count( $name ) ) {
    for ( $i = 0; i < count( $href ) ; i++ ) {
    echo "<a href='{$href[$i]}'>{$name{$i]}</a>";
    }
    }

    But why not just combine the two into one array in the first place?

    $href = array( 'http://www.youtube.com/watch?v=yC3_gkdtlua' => '1', 'http://www.youtube.com/watch?v=yC3_gkdtlub' => '2', 'http://www.youtube.com/watch?v=yC3_gkdtluc' => '3', 'http://www.youtube.com/watch?v=yC3_gkdtlud' => '4' );

    Or combine the arrays into one (like the above structure) using PHP's array_combine():

    from PHP codex:

    array_combine — Creates an array by using one array for keys and another for its values

    Description
    array_combine ( array $keys , array $values )

  3. Fask
    Member
    Posted 2 years ago #

    The complete code has been moderated. You can see it on pastebin:

    http://pastebin.com/URpLfjwN

    As you can see from the code, after creating a custom field value of youtube and in the textarea insert the links to your video. Each link MUST be on a new line.

    The code above uses WordPress’ get_post_meta function to retrieve the custom field key. It then converts the linebreaks to html’s <br /> tag using PHP’s nl2br() function. We then create an array with the explode() function using the tag as the separator of the different lines of data.

    Can you please make a post with the arrays on this post (pastebin) and please explain how to do it?

    (sorry for my english)

  4. bcwp
    Member
    Posted 2 years ago #

    I'm not sure I follow the logic completely. I see what you're saying about this line:

    echo '<li><a href="#" onclick="openYouTube(\''.$v.'\')">1</a></li>';

    Currently, every line is going to say "1". But where are you planning to define the second array? Since your code is using get_post_meta(), I suggest you also store the second array as post meta data.

    Or, maybe I'm misunderstanding what you want. Are you simply trying to insert your arrays above into that code, like this?

    [for future posting of code, please follow http://codex.wordpress.org/Forum_Welcome#Posting_Code ]

    <?php
    $href = array('http://www.youtube.com/watch?v=yC3_gkdtlua', 'http://www.youtube.com/watch?v=yC3_gkdtlub', 'http://www.youtube.com/watch?v=yC3_gkdtluc', 'http://www.youtube.com/watch?v=yC3_gkdtlud');
    $name = array('1', '2', '3', '4');
    $videos = array_combine($href,$name);
      if (!empty($videos))
      {
        echo '<div id="playlist">';
        echo '<div class="s_playlist">';
        echo '<div id="playlist_t">PLAYLIST <strong>1</strong></div>';
        echo '<div class="cdiv"></div>';
        echo '</div>';
        echo '<div class="description">';
        echo '<ul>';
        foreach ($videos as $href => $name)
        {
        echo '<li><a href="#" onclick="openYouTube(\''.$href.'\')">'.$name.'</a></li>';
        }
        echo '<div id="bg" class="popup_bg"></div>';
        echo '</ul>';
        echo '<div class="cdiv">';
        echo '</div>';
        echo '<br>';
        echo '</div>';
        echo '</div>';
      }
    ?>
  5. Fask
    Member
    Posted 2 years ago #

    The first array is collected by get_post_meta, see image:

    http://img51.imageshack.us/img51/8339/firstarray.png

    sorry you're right: the line should be this:

    <li><a href="'$video'">'$name'</a></li>

    $video = http://www.youtube.com/watch?v=yC3_gkdtlua ecc..
    $name = 1,2,3,4 ecc...

    and is placed in the foreach loop:

    foreach ($videos as $video)

    the result is like this:

    http://img3.imageshack.us/img3/7697/risultatop.png

  6. bcwp
    Member
    Posted 2 years ago #

    I see. Well the code I posted above would do what you asked in your original question. To get the <a href="http://www.youtube.com/watch?v=yC3_gkdtlua">1</a> format, you would simply modify the echo '<li><a href="#" onclick="openYouTube(\''.$href.'\')">'.$name.'</a></li>'; to say
    echo '<a href="'.$href.'">'.$name.'</a>';

    However, I think I understand what you're trying to do now. You simply want a number - the index of the loop - to distinguish the different links. If so, you would take your original code and write the foreach loop like this:
    foreach ($videos as $i => $video) {
    and then echo the link inside the loop like this:
    echo '<a href="'.$video.'">'.$i+1.'</a>';

    There's no need for a second array of numbers because you can use the index from the loop, which would go 0, 1, 2, 3, 4...
    so I simply added a "1" to get 1, 2, 3, 4...

    Does that work for your situation?

  7. Fask
    Member
    Posted 2 years ago #

    following your advice, the code should be this:

    http://pastebin.com/uY1wUzmX

    but does not work. There is an error in the syntax. View image:

    foreach ($videos as $i => $video)
    '<li><a href="'.$video.'">'.$i+1.'</a></li>'

    http://img841.imageshack.us/img841/6323/sintaxerror.png

    I also tried this:

    echo '<a href="'.$video.'">'.$.'</a>';

    but does not work.

    What's wrong?

  8. bcwp
    Member
    Posted 2 years ago #

    Sorry, my example was rushed. Change the $i+1 to ($i + 1)

    For example:

    $videos = array( 'abc', 'querty', 'asdf', 'foo' );
    foreach( $videos as $i => $video ) {
    	echo '<b>' . ($i + 1) . '</b> = ' . $video . '<br />';
    }

    successfully outputs:

    1 = abc
    2 = querty
    3 = asdf
    4 = foo

    But if you're getting an error message, I would need to know exactly what the error message is.

    No offense intended, but I recommend you spend some time learning PHP. The problem you're having isn't a WordPress issue, but rather a lack of understanding PHP syntax rules and functions. The algorithm you're trying to write is fairly straightforward.

  9. Fask
    Member
    Posted 2 years ago #

    I know, are very scarce in PHP. In any case, thank you for your time. I could not solve the problem ... :(

    Can you please make a post with the arrays on first post and please explain how to do it?

    foreach ($videos as $i => $video)
            {
            echo '<li><a href="'.$video.'">'. ($i + 1) .'</a></li>';
            }
  10. bcwp
    Member
    Posted 2 years ago #

    "Can you please make a post with the arrays on first post and please explain how to do it?"

    Sorry, I'm not sure what you mean here.

    Are you receiving an error message, or is the HTML output just not what you expected? Can you make a screenshot of the output screen and also post the exact HTML that your code is generating or the error message that you're seeing.

  11. Fask
    Member
    Posted 2 years ago #

    This is the entire code:

    http://pastebin.com/uY1wUzmX

    I do not receive any error. The ouput is not shown, and the screen is cut in half.

  12. bcwp
    Member
    Posted 2 years ago #

    Sorry, but this topic is really going beyond the purpose of this forum. You really need to learn the basic concepts required to write and debug code, or hire a qualified programmer. Otherwise, you'll just encounter one roadblock after another.

    There has to be some HTML output produced by the code. In order for someone to help you, they would need to see where the error is occurring. That means you must post your code, any error messages, and any raw HTML output (meaning you view the page source in your browser and then copy it and post it for everyone to see).

    I've already given you all the code required to produce the output you want. All you need to do is figure out how to apply the concepts to what you're doing.

  13. Fask
    Member
    Posted 2 years ago #

    ok ... thank you for everything. You are available to perform some functions in PHP? I can pay

  14. bcwp
    Member
    Posted 2 years ago #

    It's really not about the money. I'd gladly write the function for you for free. The problem is the time involved - long distance communication makes simple tasks difficult.

    My advice would be to find a local programmer on craigslist. You could meet them at a coffee shop, and they could write the functions for you in the time it takes to drink one cup of coffee. The advantage for you is that you'll end up spending far less time and money because there won't be any problems with communication, and you can always work with them again in the future if you need more help.

    You're also likely to get a higher quality, more long-term solution to your problem because there won't be any communication issues, and the programmer will have a better understanding of the context (how the whole website is supposed to function).

Topic Closed

This topic has been closed to new replies.

About this Topic