xXREDXIIIXx Posted August 4, 2009 Share Posted August 4, 2009 OK heres the problem: I am working on my video games section. you all probably know the some games ore only on certain platforms and some are on a few or all, I would like to show which platforms the game is on (I can achive this) BUT with comma seperaters. I have tried using implode but if one of the vars is empty it produces something like: Playstation3, , Xbox 360, , So I need a way of either removing the excess commas or a way around it Please help Quote Link to comment https://forums.phpfreaks.com/topic/168728-solved-comma-seperaters/ Share on other sites More sharing options...
phpSensei Posted August 4, 2009 Share Posted August 4, 2009 Post your script so we can help you. edit: You can always do str_replace(", ,",",",$string); But there are better alternatives Quote Link to comment https://forums.phpfreaks.com/topic/168728-solved-comma-seperaters/#findComment-890180 Share on other sites More sharing options...
ldougherty Posted August 4, 2009 Share Posted August 4, 2009 Since implode is just creating an array you could just unset the items you don't want in the array. foreach($array as $key => $value) { if($value == ", ") { unset($array[$key]); } } $new_array = array_values($array); Quote Link to comment https://forums.phpfreaks.com/topic/168728-solved-comma-seperaters/#findComment-890186 Share on other sites More sharing options...
xXREDXIIIXx Posted August 4, 2009 Author Share Posted August 4, 2009 Thanks for quick reply I love this place here was the code if($_SESSION['by'] == 'games'){ if($row['ps3'] == 1){$ps3 = "Playstation 3";} if($row['xbox'] == 1){$xbox = "Xbox";} if($row['360'] == 1){$x360 = "Xbox 360";} if($row['wii'] == 1){$wii = "Wii";} if($row['pc'] == 1){$pc = "Windows";} $array = array($ps3, $xbox, $x360, $wii, $pc); $comma_separated = implode(", ", $array); $comma = str_replace(", ,", ",", $comma_separated); echo str_replace(", ,", ",", $comma); } I had to do the str_replace twice once wasnt enough --- reply to new post could you show me how to implament that into my above? I better understand new code when I see with my own words so to speak. Quote Link to comment https://forums.phpfreaks.com/topic/168728-solved-comma-seperaters/#findComment-890189 Share on other sites More sharing options...
ToonMariner Posted August 4, 2009 Share Posted August 4, 2009 3 tables table 1 - platformID platformName table 2 - gameID gameName ... table 3 - gameID platformID table 3 will have foreign keys that relate to the primary keys of the other tables. query on game id selecting platfrom ids that match in table 3 and so get the corsponding platform name from platform id. Put results in an array and then implode(', ', $arr); Job done. Quote Link to comment https://forums.phpfreaks.com/topic/168728-solved-comma-seperaters/#findComment-890194 Share on other sites More sharing options...
kenrbnsn Posted August 4, 2009 Share Posted August 4, 2009 I would use the function array_filter to remove all the null values before doing the implode: <?php $a = array('one','','two','','three'); echo implode(', ',array_filter($a)); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/168728-solved-comma-seperaters/#findComment-890199 Share on other sites More sharing options...
xXREDXIIIXx Posted August 4, 2009 Author Share Posted August 4, 2009 3 tables table 1 - platformID platformName table 2 - gameID gameName ... table 3 - gameID platformID table 3 will have foreign keys that relate to the primary keys of the other tables. query on game id selecting platfrom ids that match in table 3 and so get the corsponding platform name from platform id. Put results in an array and then implode(', ', $arr); Job done. errm got it wroking with the previous post, thanks anyway if($_SESSION['by'] == 'games'){ if($row['ps3'] == 1){$ps3 = "Playstation 3";} if($row['xbox'] == 1){$xbox = "Xbox";} if($row['360'] == 1){$x360 = "Xbox 360";} if($row['wii'] == 1){$wii = "Wii";} if($row['pc'] == 1){$pc = "Windows";} $array = array($ps3, $xbox, $x360, $wii, $pc); foreach($array as $key => $value) { if($value == "") { unset($array[$key]); } } $consoles = implode(", ", $array); echo $consoles; } This can be marked as solved thanks Quote Link to comment https://forums.phpfreaks.com/topic/168728-solved-comma-seperaters/#findComment-890202 Share on other sites More sharing options...
phpSensei Posted August 4, 2009 Share Posted August 4, 2009 edit: nvm, was going to say use an array function... Quote Link to comment https://forums.phpfreaks.com/topic/168728-solved-comma-seperaters/#findComment-890203 Share on other sites More sharing options...
ldougherty Posted August 4, 2009 Share Posted August 4, 2009 OK, instead of creating the array with items you don't want how about you only put the item into the array if it tests true (==1) using array_push http://us.php.net/array_push This should work if($_SESSION['by'] == 'games') { # Create the Array $array = array(); if($row['ps3'] == 1) { array_push($array, "Playstation 3"); } if($row['xbox'] == 1) { array_push($array, "Xbox"); } if($row['360'] == 1) { array_push($array, "Xbox 360"); } if($row['wii'] == 1) { array_push($array, "Wii"); } if($row['pc'] == 1) { array_push($array, "Windows"); } $comma_separated = implode(", ", $array); echo $comma_separated; } Quote Link to comment https://forums.phpfreaks.com/topic/168728-solved-comma-seperaters/#findComment-890204 Share on other sites More sharing options...
ToonMariner Posted August 4, 2009 Share Posted August 4, 2009 @xXREDXIIIXx by all means use the previous code but be warned its nasty and horrible and harder to extend or maintain... Quote Link to comment https://forums.phpfreaks.com/topic/168728-solved-comma-seperaters/#findComment-890206 Share on other sites More sharing options...
phpSensei Posted August 4, 2009 Share Posted August 4, 2009 I would use the function array_filter to remove all the null values before doing the implode: <?php $a = array('one','','two','','three'); echo implode(', ',array_filter($a)); ?> Ken fastest and easiest way... Quote Link to comment https://forums.phpfreaks.com/topic/168728-solved-comma-seperaters/#findComment-890212 Share on other sites More sharing options...
xXREDXIIIXx Posted August 4, 2009 Author Share Posted August 4, 2009 Thank you all I have another thing which I will post in another topic Quote Link to comment https://forums.phpfreaks.com/topic/168728-solved-comma-seperaters/#findComment-890218 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.