Jim R Posted January 28, 2011 Share Posted January 28, 2011 Is there a way to take data that is listed like a,b,c,d,e and turn that into an unordered list? Basically, I'm going to have a list of schools that will be listed with comma separators. I'll print that data out in two places, one will be printed as it's in the database. The other I would like to echo as an unordered list. Quote Link to comment https://forums.phpfreaks.com/topic/225926-comma-separated-list-converted-to-ordered-list/ Share on other sites More sharing options...
smerny Posted January 28, 2011 Share Posted January 28, 2011 $schools = explode(",", $commaList); echo "<ul>"; foreach($schools as $school) echo "<li>".$school."</li>"; echo "</ul>"; Quote Link to comment https://forums.phpfreaks.com/topic/225926-comma-separated-list-converted-to-ordered-list/#findComment-1166399 Share on other sites More sharing options...
Pikachu2000 Posted January 28, 2011 Share Posted January 28, 2011 A quick and dirty way would be to use str_replace on the commas. This would fail if any of the values intentionally contained commas, though. $list = 'a,b,c,d,e'; echo "<ol>\n<li>\n"; echo str_replace(',', "</li>\n<li>", $list); echo "\n</li>\n</ol>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/225926-comma-separated-list-converted-to-ordered-list/#findComment-1166400 Share on other sites More sharing options...
Jim R Posted January 28, 2011 Author Share Posted January 28, 2011 Could the same be done with :: commitment-2011 I'd like to pass the 2011 as a variable. Quote Link to comment https://forums.phpfreaks.com/topic/225926-comma-separated-list-converted-to-ordered-list/#findComment-1166485 Share on other sites More sharing options...
smerny Posted January 28, 2011 Share Posted January 28, 2011 don't really understand what you are meaning to do Quote Link to comment https://forums.phpfreaks.com/topic/225926-comma-separated-list-converted-to-ordered-list/#findComment-1166524 Share on other sites More sharing options...
Jim R Posted January 28, 2011 Author Share Posted January 28, 2011 commitment-2011 is a slug via WordPress, and I can Get that from the database. However, I'd just like to use the 2011. Quote Link to comment https://forums.phpfreaks.com/topic/225926-comma-separated-list-converted-to-ordered-list/#findComment-1166537 Share on other sites More sharing options...
smerny Posted January 28, 2011 Share Posted January 28, 2011 is the format always the same? Quote Link to comment https://forums.phpfreaks.com/topic/225926-comma-separated-list-converted-to-ordered-list/#findComment-1166551 Share on other sites More sharing options...
BlueSkyIS Posted January 28, 2011 Share Posted January 28, 2011 if you want just the numbers, you can use $input_str = preg_replace("/[\D]/","",$input_str); Quote Link to comment https://forums.phpfreaks.com/topic/225926-comma-separated-list-converted-to-ordered-list/#findComment-1166555 Share on other sites More sharing options...
Jim R Posted January 28, 2011 Author Share Posted January 28, 2011 is the format always the same? For where I would use it, the format would always be the same. It would be progressive, 2011, 2012, 2013, etc. I would Get the slug, such as commitment-2013, hopefully extract the 2013 part and use it to query another table. Quote Link to comment https://forums.phpfreaks.com/topic/225926-comma-separated-list-converted-to-ordered-list/#findComment-1166562 Share on other sites More sharing options...
Jim R Posted January 28, 2011 Author Share Posted January 28, 2011 Pikachu2000, I'm getting this error: Warning: Wrong parameter count for str_replace() in /home/jwrbloom/public_html/resources/wp-playerProfile.php on line 62 Using this code: echo '<ul>List: <li>'; echo str_replace(',','</li>\n<li>' . $line['committed']); echo '\n</li>\n</ul>\n'; Quote Link to comment https://forums.phpfreaks.com/topic/225926-comma-separated-list-converted-to-ordered-list/#findComment-1166638 Share on other sites More sharing options...
Pikachu2000 Posted January 28, 2011 Share Posted January 28, 2011 The parameters shuld be separated by commas. One of those looks like a period. Quote Link to comment https://forums.phpfreaks.com/topic/225926-comma-separated-list-converted-to-ordered-list/#findComment-1166647 Share on other sites More sharing options...
cyberRobot Posted January 28, 2011 Share Posted January 28, 2011 Should be: ... echo str_replace(',', '</li>\n<li>', $line['committed']); ... Quote Link to comment https://forums.phpfreaks.com/topic/225926-comma-separated-list-converted-to-ordered-list/#findComment-1166649 Share on other sites More sharing options...
cyberRobot Posted January 28, 2011 Share Posted January 28, 2011 Sorry, forgot to mention that you'll need to use double quotes for the newline characters: echo '<ul>List: <li>'; echo str_replace(',', "</li>\n<li>", $line['committed']); echo "\n</li>\n</ul>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/225926-comma-separated-list-converted-to-ordered-list/#findComment-1166650 Share on other sites More sharing options...
Jim R Posted January 28, 2011 Author Share Posted January 28, 2011 Still getting the error. Warning: Wrong parameter count for str_replace() in /home/jwrbloom/public_html/resources/wp-playerProfile.php on line 62 Here is the code with the double quotes: echo '<ul>List: <li>'; 62 --> echo str_replace(",","</li>\n<li>" . $line['committed']); echo "\n</li>\n</ul>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/225926-comma-separated-list-converted-to-ordered-list/#findComment-1166654 Share on other sites More sharing options...
cyberRobot Posted January 28, 2011 Share Posted January 28, 2011 You need to replace the period with a comma: ... echo str_replace(",","</li>\n<li>" . $line['committed']); ... The one before $line Quote Link to comment https://forums.phpfreaks.com/topic/225926-comma-separated-list-converted-to-ordered-list/#findComment-1166655 Share on other sites More sharing options...
Jim R Posted January 28, 2011 Author Share Posted January 28, 2011 I "JUST" did that before the notification of your post came through. I had that as an issue on something else a year ago, and it took about three days to get it figure out. It works. Thank you. I'm definitely keeping this topic handy, as I like the exploding idea too for another project. Quote Link to comment https://forums.phpfreaks.com/topic/225926-comma-separated-list-converted-to-ordered-list/#findComment-1166656 Share on other sites More sharing options...
cyberRobot Posted January 28, 2011 Share Posted January 28, 2011 ...and it took about three days to get it figure out. I know how you feel; it can be frustrating! Quote Link to comment https://forums.phpfreaks.com/topic/225926-comma-separated-list-converted-to-ordered-list/#findComment-1166658 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.