Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts

    Put - between values in an array

    I thin kits implode i need to use however im not sure how to implement it into my code and need some help. Heres what i have

    PHP Code:
        $ThanksSql "SELECT * FROM `thanks` WHERE `post` = '$p'";
                
    $ThanksSql mysql_query($ThanksSql);
                while (
    $row2 mysql_fetch_array($ThanksSql)) {
                    
    $thankers[$row2['userID']] = $row2['username'];
                }
                foreach (
    $thankers as $id=> $name) {
                    echo 
    "<a href='profile.php?m=".$id."'>".$name."</a> ";
                } 
    That echoes just as "user1 User2 User3" etc and i want - between the names. Do i use implode before my foreach loop?

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    What do you mean by

    That echoes just as "user1 User2 User3" etc
    Your code will echo out the html for each link and where exactly do you want the - ?

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    I want it to echo the links out as

    "User 1 - User 2 - User 3"

    If i add the - to the echoes in the loop it would give me and unneeded one at either the start or the end.

  • #4
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    824
    Thanks
    93
    Thanked 18 Times in 18 Posts
    It works if i do it like this so unless this is a bad method ill stick with this .

    PHP Code:
    $ThanksSql "SELECT * FROM `thanks` WHERE `post` = '$p'";
                
    $ThanksSql mysql_query($ThanksSql);
                while (
    $row2 mysql_fetch_array($ThanksSql)) {
                    
    $thankers[] = "<a href='profile.php?m=".$row2['userID']."'>".$row2['username']."</a>";
                }
                
    $thankers implode(" - "$thankers);
                echo 
    $thankers

  • #5
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Since you have an associative array, you need another counter to keep track of what element in $thankers you are at.

    When the counter's value = the length of
    $thankers, don't add the - to link.

  • #6
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Another option

    PHP Code:
    <?php

    $count 
    1;
    while (
    $row2 mysql_fetch_array($ThanksSql)) {
        
    $str = ($count++ < mysql_num_rows($ThanksSql)) ? ' - ' '';
        echo 
    "<a href='profile.php?m=" $row2['userID'] . "'>" $row2['username'] . "</a>" $str;
    }
    ?>
    Last edited by bullant; 05-26-2011 at 11:10 AM.

  • #7
    New Coder
    Join Date
    Mar 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Why not change your code from this (original code):
    PHP Code:
        $ThanksSql "SELECT * FROM `thanks` WHERE `post` = '$p'";
                
    $ThanksSql mysql_query($ThanksSql);
                while (
    $row2 mysql_fetch_array($ThanksSql)) {
                    
    $thankers[$row2['userID']] = $row2['username'];
                }
                foreach (
    $thankers as $id=> $name) {
                    echo 
    "<a href='profile.php?m=".$id."'>".$name."</a> ";
                } 
    to this:
    PHP Code:
        $ThanksSql "SELECT * FROM `thanks` WHERE `post` = '$p'";
                
    $ThanksSql mysql_query($ThanksSql);
                while (
    $row2 mysql_fetch_array($ThanksSql)) {
                    
    $thankers[$row2['userID']] = $row2['username'];
                }
                foreach (
    $thankers as $id=> $name) {
                    echo 
    "<a href='profile.php?m=".$id."'>".$name."</a> - ";
                } 
    In your echo statement at the end, add " - " after the </a> element. You already had a space there, just add your hyphen and another space. Or remove the spaces completely and just have the hyphen there.

    I believe this will achieve what you are after.
    Last edited by Inigoesdr; 05-26-2011 at 06:10 PM. Reason: Use [php] tags please.

  • #8
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    @TMW - Why have 2 loops to do what you can do with one loop?
    Last edited by bullant; 05-27-2011 at 01:19 AM.

  • #9
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by tomharto View Post
    It works if i do it like this so unless this is a bad method ill stick with this .
    That method should be fine.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •