...

View Full Version : Put - between values in an array



tomharto
05-26-2011, 11:35 AM
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


$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?

bullant
05-26-2011, 11:47 AM
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 - ?

tomharto
05-26-2011, 11:49 AM
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.

tomharto
05-26-2011, 11:52 AM
It works if i do it like this so unless this is a bad method ill stick with this :).


$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;

bullant
05-26-2011, 11:56 AM
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.

bullant
05-26-2011, 12:01 PM
Another option



<?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;
}
?>

TheMachineWare
05-26-2011, 05:43 PM
Why not change your code from this (original 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:

$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.

bullant
05-27-2011, 01:18 AM
@TMW - Why have 2 loops to do what you can do with one loop?

Inigoesdr
05-27-2011, 01:33 AM
It works if i do it like this so unless this is a bad method ill stick with this :).

That method should be fine.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum