eagleweb Posted May 21, 2007 Share Posted May 21, 2007 I have an array which ends with a comma and a space. I want to remove the comma and space at the end, but not between the rest of the words in the array. Below is what I have done. I have a list of floor types and a list of people who can have multiple floor types. I have two tables; table1: floortypes: id (int) and type (varchar): id 1 = carpet, id 2 =linoleum, id 3 = tile, id 4 = wood. table 2: listings: id (int) and floortype (varchar): id 1 = 1, 3, 4 <?php mysql_select_db($database_mysql_connect, $mysql_connect); $query = "SELECT * FROM listings WHERE id = '".$_GET['ID']."'"; $results = mysql_query($query, $mysql_connect) or die(mysql_error()); $row = mysql_fetch_assoc($results); $floorchoices = explode(", ",$row['floortype']); query2 = "SELECT * FROM floortype"; $results2 = mysql_query($query2, $mysql_connect) or die(mysql_error()); $row2 = mysql_fetch_assoc($results2); ?> The Floor Types are: <?php do { ?> <?php if (in_array($row2['id'], $floorchoices)) {echo "".$row2['type'].", ";} ?> <?php } while ($row2 = mysql_fetch_assoc($floor)); ?> This is the output: The Floor Types are: Carpet, Tile, Wood, I need to remove that last comma and space. I tried using combinations of rtrim and strlen and substr but can't seem to get it right. Got any ideas on how to remove that comma and space at the end? Quote Link to comment https://forums.phpfreaks.com/topic/52397-solved-remove-last-two-characters-of-an-array/ Share on other sites More sharing options...
per1os Posted May 21, 2007 Share Posted May 21, 2007 www.php.net/substr <?php $string = "art, wood, tile, "; $string = substr($string, 0, -2); echo $string; ?> Quote Link to comment https://forums.phpfreaks.com/topic/52397-solved-remove-last-two-characters-of-an-array/#findComment-258569 Share on other sites More sharing options...
kenrbnsn Posted May 21, 2007 Share Posted May 21, 2007 Change this: <?php do { ?> <?php if (in_array($row2['id'], $floorchoices)) {echo "".$row2['type'].", ";} ?> <?php } while ($row2 = mysql_fetch_assoc($floor)); ?> to <?php $tmp = array(); while ($row2 = mysql_fetch_assoc($floor)) if (in_array($row2['id'], $floorchoices)) $tmp[] = $row2['type']; echo implode(', ',$tmp); ?> Note: you shouldn't enter/leave PHP on each line. Ken Quote Link to comment https://forums.phpfreaks.com/topic/52397-solved-remove-last-two-characters-of-an-array/#findComment-258582 Share on other sites More sharing options...
eagleweb Posted May 22, 2007 Author Share Posted May 22, 2007 This is much closer. It is leaving out the first item in the array; in this case it is 'carpet' so I am getting: Tile, Wood Thanks for the note. It is sloppy right now. Quote Link to comment https://forums.phpfreaks.com/topic/52397-solved-remove-last-two-characters-of-an-array/#findComment-258710 Share on other sites More sharing options...
kenrbnsn Posted May 22, 2007 Share Posted May 22, 2007 What is the code before the loop? Ken Quote Link to comment https://forums.phpfreaks.com/topic/52397-solved-remove-last-two-characters-of-an-array/#findComment-258731 Share on other sites More sharing options...
eagleweb Posted May 22, 2007 Author Share Posted May 22, 2007 I made a slight error when copying the original code to this forum, but it was affecting it. The only code is this: <?php mysql_select_db($database_mysql_connect, $mysql_connect); $query = "SELECT * FROM listings WHERE id = '".$_GET['ID']."'"; $results = mysql_query($query, $mysql_connect) or die(mysql_error()); $row = mysql_fetch_assoc($results); $floorchoices = explode(", ",$row['floortype']); query2 = "SELECT * FROM floortype"; $results2 = mysql_query($query2, $mysql_connect) or die(mysql_error()); $row2 = mysql_fetch_assoc($results2); ?> Then there is this: The Floor Types: <?php $tmp = array(); while ($row2 = mysql_fetch_assoc($results2)) if (in_array($row2['id'], $floorchoices)) $tmp[] = $row2['type']; echo implode(', ',$tmp); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52397-solved-remove-last-two-characters-of-an-array/#findComment-258732 Share on other sites More sharing options...
kenrbnsn Posted May 22, 2007 Share Posted May 22, 2007 Change <?php $results2 = mysql_query($query2, $mysql_connect) or die(mysql_error()); $row2 = mysql_fetch_assoc($results2); ?> to <?php $results2 = mysql_query($query2, $mysql_connect) or die(mysql_error()); ?> i.e. remove the line "$row2 = mysql_fetch_assoc($results2);" since that's now done in the while loop. Ken Quote Link to comment https://forums.phpfreaks.com/topic/52397-solved-remove-last-two-characters-of-an-array/#findComment-258927 Share on other sites More sharing options...
eagleweb Posted May 22, 2007 Author Share Posted May 22, 2007 Thank you much Super Guru Ken. It works perfectly. I do appreciate the help! Quote Link to comment https://forums.phpfreaks.com/topic/52397-solved-remove-last-two-characters-of-an-array/#findComment-259014 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.