Formating/outputting content php/mysql

Hi all

I know my way around the basic php functions mysql etc.
The problem I’m having is outputting the content from the db.

Example:

db > users > bio

bio: some content about user, plus more content. some content about user, plus more content. some content about user, plus more content. some content about user, plus more content. etc

echo "<li><strong>Bio:</strong> " . $row['bio'] . "</li>";

Now, how do I split this bio into separate paragraphs without adding all kinds of html elements inside my clean db data? So instead of one big <p> or <li> how can I format this data using php?

Any help much appreciated thanks

Can you post the content of the ‘bio’ field and how you would like it to look?

Thanks Anthony

db > users > bio varchar(1300)

How it should be stored in my db.
Just dummy text. Just dummy text. Just dummy text. Just dummy text. Just dummy text. Just dummy text. Just dummy text. Just dummy text. Just dummy text. Just dummy text. Just dummy text. Just dummy text.

How I’ve got it stored in my db
<p>Just dummy text. Just dummy text. Just dummy text. Just dummy text. Just dummy text. Just dummy text. Just dummy text. Just dummy text.</p>
<p>Just dummy text. Just dummy text. Just dummy text. Just dummy text. </p>

As you can see, if my bio column or any db column for that matter needs any sort of formatting, I’ll need to add html to display and break the content up when outputting to the browser, I know its easy to add one <p> to the <php> but its when I want to break it all up into multiple <p>'s with a big chunk of text from the db.

Is there a php function or how do people break up the content without adding html to the db?

Hope this explains things thanks.

How do you know when a new paragraph should be started?

You could try something like…


function format_bio($bio, $tag = 'p'){
  $parts = preg_split('~[\\r|\
]~', $bio);
  $formatted = '';
  foreach($parts as $part){
    $formatted .= sprintf('<%1$s>%2$s</%1$s>' . "\
", $tag, $part);
  }
  return $formatted;
}

echo format_bio($row['bio']);

Breaking the string up by newlines, then wrapping each in the chosen tag.

update: posted same time

:slight_smile: That’s it I don’t, well not all the time. The project I’m currently working on I do, so I know exactly where I want the <p>'s that’s why I’ve added them manually to the db, but things are starting to get messy and very sure this is not the correct way to do things.

A suppose you can say I have two different scenarios:

  1. Somethimes I’ll just want to break up big strings
  2. I’ll need to add specific html at certain points in the string

What do you think?
I remember somebody mentioned adding some code to trigger the new <p> after every 3rd fullstop as an example, even then its a guessing game?

Then on the other hand, how would I add the <p> when I need it placing at the exact point?

Thank Anthony

Getting a bit advance, sounds good could you explain in more detail :cool:

Ok, so from:

echo "<li><strong>Bio:</strong> " . $row['bio'] . "</li>";

How do we fit the new code into the above?

Thanks Barry

Well, if you have newlines delimiting the text, just this:


echo "<li><strong>Bio:</strong> " . format_bio($row['bio']) . "</li>"; 

However, by the sounds of it, you don’t.

How is the bio entered, is it by the user? If you’re using a text area and adding newlines as you go, you may have got away with it. :slight_smile:

Don’t think I do.
The bio is mainly an example, I have a number of db’s and columns using this same method which I’ve added the content to the db myself, but a number of sites with user input via textarea and some using a mixture.

Barry

Just came across

nl2br: Adding Line breaks inside a string in place of carriage returns
Isn’t nl2br only used for user input?
How do I add <p> or any other html element instead of filling my page up with <br>?

As I mention above, sometimes the content is supplied by myself into the db and other times by user input via textarea, inputs etc.

Any more ideas Anthony?
Thanks Barry

Update

Another example is:

<h2><?=$user; ?></h2>
<p><?=$age; ?></p>
<p><?=$bio; ?></p>

How do I format $bio instead of wrapping the full string into one <p>?

Cheers :cool:

“The bio is mainly an example”

how are we supposed to know how you want a block of text coded?

Thanks r937

Yes I just mentioned bio instead of another name I was using that would just confuse people, but everything boils down to the same thing r937, weather the name is relevant or not.

I have a big string which needs formatting as discussed I don’t see how the name of the db > column_name would change things, sorry about the confusion :slight_smile:

I have about a dozen db > columns spread over different sites which all need updating, bio is just one of many. Once I understand how I can fix one, I can fix them all.

Any advice much appreciated thank you :cool:

but that’s my point :slight_smile:

how are we supposed to know how you want all these columns of data formatted?

how are we supposed to know how you want all these columns of data formatted?
I just thought the concept would be the same? Most of the time I’ll just need to add a couple of <p>'s here and there, but sometimes I’ll need to add more relevant html to fit the situation/correct markup etc.

Sometimes the html will need to be added to user input and sometimes manually by me (static content from client as an example). Or whatever is easiest.

making sense? :cool: