homepage Welcome to WebmasterWorld Guest from 54.205.254.108
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Deleting last comma
rtrim and substr not giving result I need
meganp




msg:1247745
 2:45 pm on Mar 14, 2004 (gmt 0)

Hi,

The code below displays pet names like so:

Fido, Bowser, Kittycat, Spot,

and I want to delete only the last comma. I tried 'substr' and 'rtrim' and both of those delete the space and comma between each name, not just the last name as I'd like, so I end up with:
Fido,Bowser,Kittycat,Spot,

How would I delete only the last comma, so that it is this instead?
Fido, Bowser, Kittycat, Spot

Even with trim and substr, I end up with:
FidoBowserKittycatSpot


<?php
$memID=$_POST['memID'];
// Connect to the database
require_once ('../Connections/mysql_connect.php');
// Get data
$states = "SELECT state FROM users GROUP BY state";
$result = mysql_query($states);
while ($row = mysql_fetch_assoc($result)) {
echo "<p>" . $row["state"] . "<br>\n";
$statename = $row["state"];
$allpets = "SELECT memID, petName FROM users WHERE state='$statename' ORDER BY petName ASC";
$pet = mysql_query($allpets);
while ($pets = mysql_fetch_assoc($pet)) {
echo "<a href='memorials.php?tribute={$pets['memID']}'>{$pets['petName']}</a>, ";
}
}
?>

I tried this:

while ($pets = mysql_fetch_assoc($pet)) {
$url="<a href='memorials.php?tribute={$pets['memID']}'>{$pets['petName']}</a>, ";
$url=substr($url, 0, -1);
echo $url;

I thought that the result of $url was considered a string, but it appears that each petName within $url is a string, and that would account for rtrim and substr not working when I tried them. So I'm at a loss of what to do. Can someone point me in the right direction?

Thanks!

 

Paul in South Africa




msg:1247746
 2:55 pm on Mar 14, 2004 (gmt 0)

$url = "";
while ($pets = mysql_fetch_assoc($pet)) {
$url .="<a href='memorials.php?tribute={$pets['memID']}'>{$pets['petName']}</a>, ";
}
$url=substr($url, 0, -2);
echo $url;

Birdman




msg:1247747
 2:59 pm on Mar 14, 2004 (gmt 0)

You were close. You just need to change one parameter in substr() and add a period before the equals operator(.=), which means "append this value to the var". As you have it now, $url is rewritten each time the loop occurs, so you end up with only the last value.

Try this:

while ($pets = mysql_fetch_assoc($pet)) {
$url .= [php.net] "<a href='memorials.php?tribute={$pets['memID']}'>{$pets['petName']}</a>, ";
$url=substr [php.net]($url, 0, -2);
echo $url;

Too quick for me Paul;)

meganp




msg:1247748
 5:36 pm on Mar 14, 2004 (gmt 0)

Thanks...I tried that, however I ended up with this:

BowserBowserFidoBowserFidoKittycatBowserFidoKittycatSpot

jatar_k




msg:1247749
 6:03 pm on Mar 14, 2004 (gmt 0)

remember proper braces

while ($pets = mysql_fetch_assoc($pet)) {
$url .= "<a href='memorials.php?tribute={$pets['memID']}'>{$pets['petName']}</a>, ";
}
$url=substr($url, 0, -2);
echo $url;

other wise it will chop the string through every iteration. Or don't use any braces since it is one line inside the loop

while ($pets = mysql_fetch_assoc($pet)) $url .= "<a href='memorials.php?tribute={$pets['memID']}'>{$pets['petName']}</a>, ";
$url=substr($url, 0, -2);
echo $url;

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved