rlarkinsmith@gmail.com Posted June 8, 2012 Share Posted June 8, 2012 I am trying to convert, using a function, data that appears in my form from comma delimited to bulleted list. For instance the data is showing like this: item 1, item 2, item 3, etc. and I want it to show like this: item 1 item 2 Below is what I have come up with so far and it works, but it places the information in the wrong place on my form. (I am using phprunner to create my forms). Any help will be very appreciated. $value = $data["mysqlDataField"]; $info = explode(",", $value); $list = "<ul>"; foreach( $info as $item ) { echo "<li>$item</li>\n"; } $list .= '</ul>'; echo $list; Quote Link to comment https://forums.phpfreaks.com/topic/263860-convert-data-from-mysql-field-from-comma-delimited-to-bulleted-list/ Share on other sites More sharing options...
jcbones Posted June 8, 2012 Share Posted June 8, 2012 If you are storing in a comma delimited string, then you are doing MySQL wrong. It will lead to many headaches later. If you posted your database structure, and a sample of your data, then we could help you to properly structure it. To answer your question. If the contents of your CSV string is correctly structured: ie item1,item2,item3 and not item3,item1,item2. Your current code should work. Although, I think you could use str_replace and it would be faster. $value = $data["mysqlDataField"]; echo '<ul><li>' . str_replace(',','</li><li>',$value) . '</li></ul>'; Quote Link to comment https://forums.phpfreaks.com/topic/263860-convert-data-from-mysql-field-from-comma-delimited-to-bulleted-list/#findComment-1352243 Share on other sites More sharing options...
Barand Posted June 8, 2012 Share Posted June 8, 2012 echo "<li>$item</li>\n"; replace "echo" with "$list .=" Quote Link to comment https://forums.phpfreaks.com/topic/263860-convert-data-from-mysql-field-from-comma-delimited-to-bulleted-list/#findComment-1352249 Share on other sites More sharing options...
rlarkinsmith@gmail.com Posted June 8, 2012 Author Share Posted June 8, 2012 Both of your suggestions worked equally well. Thank you for them. However, now I am getting a loop at the top of my form and it is still not bulleting the information on the form where I want it. I have emailed phprunner (the program that I use to develop in as I am an educator that does this on the side). Any other suggestions to stop the loop and get it to work in the place where I want it to? For what it's worth, I don't have control over how the data is saved in the database and yes it is saved as comma delimited in a text field. Quote Link to comment https://forums.phpfreaks.com/topic/263860-convert-data-from-mysql-field-from-comma-delimited-to-bulleted-list/#findComment-1352275 Share on other sites More sharing options...
rlarkinsmith@gmail.com Posted June 15, 2012 Author Share Posted June 15, 2012 Your string worked really well once I figured out some issues on my end. The only thing that is still happening that I hope you can help with is that the bulleted list is doing a bullet then a double space with a comma in it, then the next bullet and so on. Any ideas how to get rid of the comma and extra space between bullets? If you are storing in a comma delimited string, then you are doing MySQL wrong. It will lead to many headaches later. If you posted your database structure, and a sample of your data, then we could help you to properly structure it. To answer your question. If the contents of your CSV string is correctly structured: ie item1,item2,item3 and not item3,item1,item2. Your current code should work. Although, I think you could use str_replace and it would be faster. $value = $data["mysqlDataField"]; echo '<ul><li>' . str_replace(',','</li><li>',$value) . '</li></ul>'; Quote Link to comment https://forums.phpfreaks.com/topic/263860-convert-data-from-mysql-field-from-comma-delimited-to-bulleted-list/#findComment-1354148 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.