KitCarl Posted May 1, 2010 Share Posted May 1, 2010 I'm trying to generate a list of the names of associations from the choices made via check boxes which use the ID ($assoc) to pass a value. The code below returns the names and as expected adds a ' & ' too each, but I would like to code it to not add the last ' & '. Did a search on this board and scanned my 'PHP Visual Quickstart Guide' but found nothing to my understanding. I'm just getting starting with PHP and am not sure this is even close to the best type of code to do this or if an entirely different method is preferred. Any help appreciated and an explanation is always a plus. <?php $sql = "Select A.a As name FROM A WHERE A.aID IN($assoc)"; $result = mysql_query("$sql"); for( $i = 0; $i < $row = mysql_fetch_array($result); $i++) echo $row["name"]. ' & '; ?> Quote Link to comment https://forums.phpfreaks.com/topic/200388-removing-the-last-or-any-string-in-a-concatenation/ Share on other sites More sharing options...
papaface Posted May 1, 2010 Share Posted May 1, 2010 $str = trim($str, " & "); http://uk3.php.net/trim Thats how you'd remove it from a string. Quote Link to comment https://forums.phpfreaks.com/topic/200388-removing-the-last-or-any-string-in-a-concatenation/#findComment-1051609 Share on other sites More sharing options...
mikesta707 Posted May 1, 2010 Share Posted May 1, 2010 by the way, that for loop doesn't make any sense. for( $i = 0; $i < $row = mysql_fetch_array($result); //specifically //$i < $row = mysql_fetch_array($result) this will technically work because of the way that PHP treats comparisons between different types, but probably does not work the way you think it does. What actually happens is that $row is usually an array, and when you compare an array and an integer, an array is always greater. However, when mysql_fetch_array returns false (or some value that casts to false), which will than make the condition false, and exit the loop. perhaps you meant this for( ; $row = mysql_fetch_array($result) ; ) //or more commonly while($row = mysql_fetch_array($result)) Note: your loop probably "works", but just letting you know it may work in an unintended way. and the way your code is now, you don't actually have a string to use trim on because you just echo the results of the query, without storing them $str = "";//create empty string while( $row = mysql_fetch_array($result)) $str .= $row["name"]. ' & '; //concatenate values $str = trim($str, ' & '); echo $str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/200388-removing-the-last-or-any-string-in-a-concatenation/#findComment-1051618 Share on other sites More sharing options...
KitCarl Posted May 2, 2010 Author Share Posted May 2, 2010 Thanks for the help and the mini-lesson! Quote Link to comment https://forums.phpfreaks.com/topic/200388-removing-the-last-or-any-string-in-a-concatenation/#findComment-1051723 Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 There's rtrim too which is probably more specific. Quote Link to comment https://forums.phpfreaks.com/topic/200388-removing-the-last-or-any-string-in-a-concatenation/#findComment-1051727 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.