Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying dynamic data as bulleted list

Status
Not open for further replies.

tshey

Technical User
Dec 28, 2005
78
AU
I have a mysql database table field set to text. I want to be able to display each line in this field as a bulleted list. Is this possible?
 
Sure. Use split() to split your string on new lines (\n) and then simply loop through the array and for every item output <li> at the beginning and </li> at the end. Don't forget the <ul> and </ul> before and after the loop.
 
Could you please give me an example, or a link to a really good example?
 
Code:
$textfieldfromdatabase = "This is \r\n some text \r\n that i want to delimit";
$str = str_replace(array("\r\n", "\n"), "</li><li>", $textfieldfromdatabase);
echo "<ul><li>".$str."</li></ul>";

if your input string is messy you might want to explode the string into an array first and then test for empty strings in the array elements before making them into a bulleted list
Code:
$textfieldfromdatabase = "This is \r\n some text \r\n that i want to delimit";
$str = str_replace(array("\r\n", "\n"), "***{}***", $textfieldfromdatabase);
$array = explode("***{}***", $str);
echo "<ul>";
foreach ($array as $val):
	$val = trim($val);
	if (!empty($val)):
		echo "<li>$val</li>";
	endif;
endforeach;
echo "</ul>";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top