azonicds2 Posted April 30, 2010 Share Posted April 30, 2010 Hi guys Had some great help from here before so thought id try my luck again. Basically i have an array query that works fine, but the echo is going into a piece of Javascript, its basically telling the Java what image paths to use for a Slideshow, each of these needs to be seperated by a comma. Accept for the last one in the array needs to have the comma removed. I can remove the last comma for each row no problem but thats where the problem lies. I only want to remove it from the last row in the array. Any ideas? My code is as follows: <?php $query2 = mysql_query("SELECT * FROM homeimages ORDER by Filename ASC") or die(mysql_error()); while ($row2=mysql_fetch_array($query2)){ $string = "['http://www.benbrownphotography.co.uk/images/home/upload/".$row2['Filename']."'],"; $output = substr_replace($string,"",-1); echo "$output"; } ?> This is fine for if there is only one row in the database but when you add more it is obviously removing the comma from every row. How can i remove it only from the last row in the array? Thanks a lot in advance! Danny Quote Link to comment https://forums.phpfreaks.com/topic/200256-removing-last-comma-from-an-array-row/ Share on other sites More sharing options...
robert_gsfame Posted April 30, 2010 Share Posted April 30, 2010 substr($string,0,strlen($string)-1) Quote Link to comment https://forums.phpfreaks.com/topic/200256-removing-last-comma-from-an-array-row/#findComment-1050914 Share on other sites More sharing options...
azonicds2 Posted April 30, 2010 Author Share Posted April 30, 2010 substr($string,0,strlen($string)-1) Wont that do it for every row though, rather than just the last one? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/200256-removing-last-comma-from-an-array-row/#findComment-1050916 Share on other sites More sharing options...
azonicds2 Posted April 30, 2010 Author Share Posted April 30, 2010 Have tried that Robert and i can't get that to work either. Any other ideas? Looking at my code am i going about this the wrong way, should i be using some sort of count? Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/200256-removing-last-comma-from-an-array-row/#findComment-1050929 Share on other sites More sharing options...
ignace Posted April 30, 2010 Share Posted April 30, 2010 Another method would be to use implode. I rewrote your code and enhanced it (in a few parts): function get_file_name($resource) { list($filename) = mysql_fetch_row($resource); return $filename; } $result = mysql_query('SELECT Filename FROM homeimages ORDER by Filename ASC'); $files = array(); if (false !== $result && ($num_rows = mysql_num_rows($result)) > 0) { $files = array_map('get_file_name', array_fill(0, $num_rows, $result)); } echo '["http://www.benbrownphotography.co.uk/images/home/upload/', implode('"],["http://www.benbrownphotography.co.uk/images/home/upload/', $files), '"]'; If you have PHP5.3 running on your server you may use closures instead of get_file_name() Quote Link to comment https://forums.phpfreaks.com/topic/200256-removing-last-comma-from-an-array-row/#findComment-1050940 Share on other sites More sharing options...
azonicds2 Posted April 30, 2010 Author Share Posted April 30, 2010 Ignace you are a god! That worked brilliantly. Thank you very much for that. Very much appreciated! I had tried to use implode before but i wasn't using it correctly obviously. Thanks mate! Danny Quote Link to comment https://forums.phpfreaks.com/topic/200256-removing-last-comma-from-an-array-row/#findComment-1050948 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.