Forum Moderators: coopster

Message Too Old, No Replies

Newbie exploding text from MySQL

it is the little things that we assume everyone knows

         

Storyman

8:16 pm on Oct 11, 2004 (gmt 0)

10+ Year Member



Developement Program: Dreamweaver MX 2004

Using a text field input lines of text are typed, using a carriage return to break the lines on input.

The text is stored in a MySQL text field.

When retrieved the goal is to make the lines of text format as a bulleted unordered list.

This is the bit of code that I'm working with now:
<?php
if (!empty($row_rsHomePage['mt_list'])) {
$thelist = ($row_rsHomePage['mt_list']);
$newlist = explode('/n', $thelist);
echo "$newlist[0]<br />";
echo "$newlist[1]";
}?>

Right now I'm unable to explode the list because I'm don't know what the delimiter is that separates the lines (/n is not it).

What delimiter should I use so explode will break the lines?

From here the plan is to count the number of arrays, then echo each array with a dot and space preceding it. Is there a better way to do this?

Birdman

8:58 pm on Oct 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello,

The line break could be either \r or \n(or both I believe). I would suggest an if/else to check which one exists. I wrote a one-liner below. It may look confusing but it's just a shorthand version(ternary, I believe) of an if/else statement.

$newlist = (strpos('\r', $thelist) === false)? explode('/n', $thelist) : explode('\r', $thelist);

And a quick way to put the in list format using a foreach loop:

$list = '<ul>';
foreach($newlist as $val){
$list .= '<li>'.$val.'</li>';
}
$list .= '</ul>';

print $list;

Hope it helps,
Birdman

Storyman

9:15 pm on Oct 11, 2004 (gmt 0)

10+ Year Member



Birdman,

Since posting I came up with a solution, but I like your's better. Thanks for the solution.

<?php
if (!empty($row_rsHomePage['mt_list'])) {
$thelist = nl2br($row_rsHomePage['mt_list']);
$newlist = explode('<br />', $thelist);
$i = 0; $n = count($newlist)-1;
while ($i < $n) {
echo "<ul><li>$newlist[$i]</li></ul>";
$i++;
}
}?>

Storyman

6:20 pm on Oct 14, 2004 (gmt 0)

10+ Year Member



Discovered that my code works fine in IE6, but makes monkey shines in FireFox. In FF the <ul> tag is handled differently and creates an extra bullet to the left of the item list.

Using Birdman's approach I've changed to the code so it works in both IE and FF just fine:

<?php
if (!empty($row_rsHomePage['para_1_list'])) {
$readylist = '';
$thelist = nl2br(trim($row_rsHomePage['para_1_list']));
$newlist = explode('<br />', $thelist);
foreach($newlist as $val) {
$readylist .= "<li>$val</li>";
}
echo "<ul>$readylist</ul>";
}
?>

Just in case anyone is interested in the CSS code:

#content ul {
list-style-position: outside;
list-style-type: disc;
padding-left: 5px;
margin-bottom: 10px;
margin-top: 10px;
margin-left: 180px;
margin-right: 10px;
}