jesushax Posted March 27, 2009 Share Posted March 27, 2009 hi all what im trying to do is say $DogField = "german shepard,shitzu,husky, doberman, Lindsey Lohan"; how would i get that variable into <ul> <li>German Shepard</li> <li>Shitzu</li> <li>Husky</li> </ul> <ul> <li>Doberman</li> <li>Lindesy Lohan</li> </ul> so that every three <li>s a new list is created my idea was something like str_replace(",","</li><li>",$Dogfield); Quote Link to comment https://forums.phpfreaks.com/topic/151388-solved-turn-field-results-sperated-by-comma-into-list/ Share on other sites More sharing options...
lonewolf217 Posted March 27, 2009 Share Posted March 27, 2009 <?php $dogArray = explode(",",$DogField); $i=1; foreach($dogArray as $dog) { if($i == 1) { echo "<ul>"; } echo "<li>" . $dog . "</li>"; if($i == 3) { echo "</ul>"; $i = 0; } $i++; } if($i ==2) { echo "</ul>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151388-solved-turn-field-results-sperated-by-comma-into-list/#findComment-795130 Share on other sites More sharing options...
kenrbnsn Posted March 27, 2009 Share Posted March 27, 2009 Here's another way: <?php $DogField = "german shepard,shitzu,husky, doberman, Lindsey Lohan"; $dogs = array_map('trim',explode(',',$DogField)); $st = 0; $len = 3; $part = array_slice($dogs,$st,$len); while (!empty($part)) { echo '<ul><li>' . implode('</li><li>',$part) . "</li></ul>\n"; $st += $len; $part = array_slice($dogs,$st,$len); } ?> Using the $len variable, you can easily change the number of elements in the generated lists. Ken Quote Link to comment https://forums.phpfreaks.com/topic/151388-solved-turn-field-results-sperated-by-comma-into-list/#findComment-795137 Share on other sites More sharing options...
jesushax Posted March 27, 2009 Author Share Posted March 27, 2009 GREAT thanks what if there is less than 3 <li>? cos the particular dog field in looking at only has 1 but im getting <li>Doggy1<li><li> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/151388-solved-turn-field-results-sperated-by-comma-into-list/#findComment-795141 Share on other sites More sharing options...
kenrbnsn Posted March 27, 2009 Share Posted March 27, 2009 The method I posted works fine for any number of entries, from 1 to as many as you like. Ken Quote Link to comment https://forums.phpfreaks.com/topic/151388-solved-turn-field-results-sperated-by-comma-into-list/#findComment-795143 Share on other sites More sharing options...
lonewolf217 Posted March 27, 2009 Share Posted March 27, 2009 who's method did you use ? I dont see how mine could have two <li> tags in a row then again, neither of our methods would have all <li> tags without closing any of them, so maybe you just didn't copy/paste exactly what you saw regardless, kenrbnsn's method is probably more dynamic Quote Link to comment https://forums.phpfreaks.com/topic/151388-solved-turn-field-results-sperated-by-comma-into-list/#findComment-795150 Share on other sites More sharing options...
jesushax Posted March 27, 2009 Author Share Posted March 27, 2009 i used both, both of them display <ul><li>Architects / Designers</li><li></li></ul> the field is trades and contains this data Architects / Designers, there is not trailing spaces either what you think it is? Quote Link to comment https://forums.phpfreaks.com/topic/151388-solved-turn-field-results-sperated-by-comma-into-list/#findComment-795153 Share on other sites More sharing options...
lonewolf217 Posted March 27, 2009 Share Posted March 27, 2009 nevermind, ken found the issue Quote Link to comment https://forums.phpfreaks.com/topic/151388-solved-turn-field-results-sperated-by-comma-into-list/#findComment-795159 Share on other sites More sharing options...
kenrbnsn Posted March 27, 2009 Share Posted March 27, 2009 Remove the trailing comma before exploding. The trailing comma is causing an empty entry in the array. <?php $str = "Architects / Designers,"; $ary = array_map('trim',explode(',',rtrim($str,','))); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/151388-solved-turn-field-results-sperated-by-comma-into-list/#findComment-795161 Share on other sites More sharing options...
Yesideez Posted March 27, 2009 Share Posted March 27, 2009 $DogField = "Lindsey Lohan"; PMSL!!! Loving your sense of humour!!! Quote Link to comment https://forums.phpfreaks.com/topic/151388-solved-turn-field-results-sperated-by-comma-into-list/#findComment-795167 Share on other sites More sharing options...
jesushax Posted March 27, 2009 Author Share Posted March 27, 2009 well heres the actual code, for what im using it for EDIT: no sorry its displaying 1 blank <li>now <?php $TradesField = $row["Sect1_6"]; $trades = array_map('trim',explode(',',rtrim(',',$TradesField))); $st = 0; $len = 3; $part = array_slice($trades,$st,$len); while (!empty($part)) { echo '<ul><li>' . implode('</li><li>',$part) . "</li></ul>\n"; $st += $len; $part = array_slice($trades,$st,$len); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151388-solved-turn-field-results-sperated-by-comma-into-list/#findComment-795174 Share on other sites More sharing options...
lonewolf217 Posted March 27, 2009 Share Posted March 27, 2009 can you post a sample of $row["Sect1_6"]; to test ? Quote Link to comment https://forums.phpfreaks.com/topic/151388-solved-turn-field-results-sperated-by-comma-into-list/#findComment-795178 Share on other sites More sharing options...
jesushax Posted March 27, 2009 Author Share Posted March 27, 2009 it comes out Architects / Designers, no trailing spaces Quote Link to comment https://forums.phpfreaks.com/topic/151388-solved-turn-field-results-sperated-by-comma-into-list/#findComment-795180 Share on other sites More sharing options...
kenrbnsn Posted March 27, 2009 Share Posted March 27, 2009 I modified my post after you copied it. I had the parameters in the wrong order for rtrim. change <?php rtrim(',',$TradesField) ?> to <?php rtrim($TradesField,',') ?> Sorry. Ken Quote Link to comment https://forums.phpfreaks.com/topic/151388-solved-turn-field-results-sperated-by-comma-into-list/#findComment-795183 Share on other sites More sharing options...
jesushax Posted March 27, 2009 Author Share Posted March 27, 2009 that did it, thank you both very much Quote Link to comment https://forums.phpfreaks.com/topic/151388-solved-turn-field-results-sperated-by-comma-into-list/#findComment-795187 Share on other sites More sharing options...
jesushax Posted March 27, 2009 Author Share Posted March 27, 2009 $DogField = "Lindsey Lohan"; PMSL!!! Loving your sense of humour!!! well she is isnt she lol Quote Link to comment https://forums.phpfreaks.com/topic/151388-solved-turn-field-results-sperated-by-comma-into-list/#findComment-795190 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.