proctk Posted September 21, 2007 Share Posted September 21, 2007 Hi I have a DB with a column that is set as Text format. The data stored in the field will be stored in many lines. When I get the data from the DB I want it to display where the line breaks are I'm not sure how to write the script to do this. I have worked withe explode before but there is nothing come at the end of each line to do this with. any help is excellent Link to comment https://forums.phpfreaks.com/topic/70079-mysql-text-field/ Share on other sites More sharing options...
cooldude832 Posted September 21, 2007 Share Posted September 21, 2007 Line breaks are generally known as a \n character so try mainiuplating the string by exploding at the \n or find the position of htem, whatever floats yoru boat. Link to comment https://forums.phpfreaks.com/topic/70079-mysql-text-field/#findComment-351957 Share on other sites More sharing options...
proctk Posted September 21, 2007 Author Share Posted September 21, 2007 here is what fixed my problem <?php echo nl2br($recipe['preperation']); ?> My next question s how would I add a number in front of each new line Link to comment https://forums.phpfreaks.com/topic/70079-mysql-text-field/#findComment-351974 Share on other sites More sharing options...
cooldude832 Posted September 21, 2007 Share Posted September 21, 2007 First are you looking for a constant or a counting number (like Line 1, Line 2, Line3) secondly, since nl2br is converting all the \n to <br /> lets explode the string at the "<br />"; then say foreach($exploded as $value) echo $i: $value; $i++; something like that (its psedeo code not real stuff) as long as you don't have a lot of lines (like more than 100) Link to comment https://forums.phpfreaks.com/topic/70079-mysql-text-field/#findComment-351976 Share on other sites More sharing options...
proctk Posted September 21, 2007 Author Share Posted September 21, 2007 thank you for the post, My newbie ness is having a hard time figuring out how to put it togther, could you post an example Link to comment https://forums.phpfreaks.com/topic/70079-mysql-text-field/#findComment-351982 Share on other sites More sharing options...
Jessica Posted September 21, 2007 Share Posted September 21, 2007 Instead of running the nl2br, just do this: <?php $lines = explode("\n", $recipe['preperation']); foreach($lines AS $num=>$line){ print $num.' '.$line.'<br/>'; } ?> Link to comment https://forums.phpfreaks.com/topic/70079-mysql-text-field/#findComment-351986 Share on other sites More sharing options...
cooldude832 Posted September 21, 2007 Share Posted September 21, 2007 <?php $string = nl2br($fromsql); $exploded = explode("<br />",$string); $i = 0; foreach($exploded as $value){ echo $i.": ".$value."<br />"; $i++; } ?> $fromsql is the output from mysql in this case. Link to comment https://forums.phpfreaks.com/topic/70079-mysql-text-field/#findComment-351987 Share on other sites More sharing options...
proctk Posted September 21, 2007 Author Share Posted September 21, 2007 thank you all for the replies thats excellent. Link to comment https://forums.phpfreaks.com/topic/70079-mysql-text-field/#findComment-351995 Share on other sites More sharing options...
Jessica Posted September 21, 2007 Share Posted September 21, 2007 by running nl2br on it before exploding, you have a line that isn't needed...why convert it to something else, to just explode it on that? Link to comment https://forums.phpfreaks.com/topic/70079-mysql-text-field/#findComment-351998 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.