lbaxterl Posted February 11, 2011 Share Posted February 11, 2011 I have the following script which outputs different results based on weather a result is odd or even, pretty simple. The prblem lies with the very last result ouputted, as you can see from the code it will have a comma trailing it, which causes IE to not display the gallery. Can Anyone advise me on how to remove this last comma/echo the last result differently. Thank you <?php include "config.inc.php"; $num = 1; function checkNum($num){ return ($num%2) ? TRUE : FALSE; } $result = mysql_query( "SELECT photo_id, photo_filename FROM gallery_photos WHERE photo_category= 6 AND promo= 1 "); $nr = mysql_num_rows( $result ); $NumOfRecords = mysql_num_rows($result); while($row = mysql_fetch_array($result)) { $id = $row['photo_id']; $img = $row['photo_filename']; if(checkNum($num) === TRUE){ echo " { src: 'admin/photos/".$img."', dir: 'left' },"; $num++; } elseif ($num == $NumOfRecords) { echo " { src: 'admin/photos/".$img."', dir: 'left' }"; } else{ echo " { src: 'admin/photos/".$img."', dir: 'right' },"; $num++; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/227359-remove-comma-from-last-result/ Share on other sites More sharing options...
lbaxterl Posted February 11, 2011 Author Share Posted February 11, 2011 Sorry i posted code i was messing around with there here is the working code (in firefox at least), it still outputs the last record having a comma after it. <?php include "config.inc.php"; $num = 1; function checkNum($num){ return ($num%2) ? TRUE : FALSE; } $result = mysql_query( "SELECT photo_id, photo_filename FROM gallery_photos WHERE photo_category= 6 AND promo= 1 "); $nr = mysql_num_rows( $result ); $NumOfRecords = mysql_num_rows($result); while($row = mysql_fetch_array($result)) { $id = $row['photo_id']; $img = $row['photo_filename']; if(checkNum($num) === TRUE){ echo " { src: 'admin/photos/".$img."', dir: 'left' },"; $num++; } else{ echo " { src: 'admin/photos/".$img."', dir: 'right' },"; $num++; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/227359-remove-comma-from-last-result/#findComment-1172714 Share on other sites More sharing options...
iPixel Posted February 11, 2011 Share Posted February 11, 2011 use rtrim()... $stringB = rtrim($stringA,", "); GOOD LUCK Quote Link to comment https://forums.phpfreaks.com/topic/227359-remove-comma-from-last-result/#findComment-1172719 Share on other sites More sharing options...
lbaxterl Posted February 11, 2011 Author Share Posted February 11, 2011 I'll look into this thank you! Quote Link to comment https://forums.phpfreaks.com/topic/227359-remove-comma-from-last-result/#findComment-1172747 Share on other sites More sharing options...
Jessica Posted February 11, 2011 Share Posted February 11, 2011 Or instead, put each value into an array, and when you're ready to echo it, use implode. Quote Link to comment https://forums.phpfreaks.com/topic/227359-remove-comma-from-last-result/#findComment-1172856 Share on other sites More sharing options...
Pikachu2000 Posted February 11, 2011 Share Posted February 11, 2011 ^^^ What she said. ^^^ Quote Link to comment https://forums.phpfreaks.com/topic/227359-remove-comma-from-last-result/#findComment-1172860 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.