Jump to content

Recommended Posts

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;

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>';

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.

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>';

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.