Jump to content

[SOLVED] need help with simple problem (don't know how to describe it in one line)


Recommended Posts

First of, sorry for the poor Subject title, I'll change it once I know what the problem is.

 

I just ran into a problem that I think shouldn't be hard to fix (it is for me though). I'm trying to use a function that puts data from an array into a new array, like this:

 

var content=new Array()
				//change the array below to the text associated with your links Expand or contract the array, depending on how many links you have

<? foreach ($postsG as $p) {?>
content[<? echo $p['guests']['id'];?>]=
'<?echo '<big><b>' . $p['guests']['name'] . '</b></big>' , $p['guests']['bio'] , '';
?>'
<?
}
?>

 

You can probably tell by looking at that code I'm pretty new to php. I'm using a javascript function that I modified to work with my mysql database. It originally looks like this (and isn't php by default):

 

content[]='<br><big><b>Title</b></big><br>You content here.'

 

Everything works fine (there's more to the code than I've put up here), but it gets into trouble when I define "content" as text containing multiple <p> paragraphs. It's stored in the database like this:

<p>paragraph one</p>

<p>paragraph two</p>

 

Which unfortunately makes the above code look like this:

 

content[]='<p>paragraph one</p>
<p>paragraph two</p>'

 

The problem is that those two paragraphs aren't one line but two, and that 'breaks' the code. In contrast, this works fine:

 

content[]='<p>paragraph one</p> <p>paragraph two</p>'

 

So I'm guessing I should find a way to put the database data into a single line somehow. Or not. Anybody a suggestion?

What language to you want to use?

In both (Javascript and PHP) is \n a linebreak and can be used with "

So for example:

$content = "<p>My content</p>\n<p>so</p>";

 

Well I don't think I can use that because the content comes from a database where it's stored with \n. Could I add the \n when putting the data in the array for example (the array which 'content' - javascript - uses)?

Ah right... then make this:

<?php
   $content = str_replace("\n", '\\n', $content_of_databas);
?>

 

remember to make that into " and " :)

so in Javascript it should looks like

var content = "<p>foo</p>\n<p>bar</p>

It doesn't work. The code now looks like this:

 

<? foreach ($postsG as $p) {
$bio = str_replace("\n", '\\n', $p['guests']['bio']);	?>
content[<? echo $p['guests']['id'];?>]=
"<?echo '<big><b>' . $p['guests']['name'] . '</b></big>' , $bio , '';?>"
<?
}
?>

 

Shouldn't I see that newline character when viewing the data in the database itself (phpmyadmin)? Because it's not there. The text is stored in several lines without <br> and such, like this:

line 1

line 2

 

Not:

line 1<br>

line 2

 

Also not:

line 1 line 2

 

I guess I'm looking for a way to make it:

 

line 1 <br> line 2

 

Thanks, but I already tried that one. No result unfortunately. But maybe I didn't use it correctly:

 

<? foreach ($postsG as $p) {

$bio = nl2br($p['guests']['bio']);

?>

content[<? echo $p['guests']['id'];?>]=

"<?echo '<big><b>' . $p['guests']['name'] . '</b></big>' , $bio , '';?>"

<?

}

?>

<?php foreach ($postsG as $p) {
$bio = nl2br($p['guests']['bio']);
?>
content[<?= $p['guests']['id']; ?>]="<?= '<big><b>' . str_replace("\n", "\\n", nl2br($p['guests']['name'])) . '</b></big>' . $bio;?>";
<?php
}
?>

Just to be clear: the data isn't stored with \n, but it is stored in multiple lines (mysql longtext field). I can't replace \n because it's not there. I tried the above suggestion, but it doesn't work either. So keep those suggestions coming please :-)

-.-

linebreak = 13;

echo ord("\n"); //13

 

Shows how much I know. I looked "13" up and it refers to an "enter", right? Which makes sense. Thanks for clearing that up. But still, the value of $bio still breaks the code :-\

 

Cinner, it looks like you're trying to define a JavaScript array by creating the code dynamically with PHP.  Is that right?  And you said the data comes from a database?  Can I see the query line that extracts the data from the database?  Is it MySQL?

Because nl2br ADDS a HTML line break and doesn't replace it, try the following...

 

Replace this:

$bio = nl2br($p['guests']['bio']);

 

With this:

$bio = str_replace(array("\r\n", "\r", "\n"), "<br />", $p['guests']['bio']);

 

-Kalivos

Cinner, it looks like you're trying to define a JavaScript array by creating the code dynamically with PHP.  Is that right?  And you said the data comes from a database?  Can I see the query line that extracts the data from the database?  Is it MySQL?

 

Yes you are right: I'm defining a JS array with content coming from an array that's filled with data from a MYSQL database. Here's the code:

 

		
				//select guests										
				$guests_query = "	SELECT *
									FROM guests
									ORDER BY name ASC";

				$resultG = mysql_query($guests_query); //results for guests					
				//search results & fill array
				while ($rowG = mysql_fetch_assoc($resultG)) {

					// fill array
					$postsG[] = array(
								'guests' => array(
									'name' => $rowG['name'],
									'id' => $rowG['guestID'],
									'photo' => $rowG['photourl'],
									'bio' => $rowG['biography']
									),
								);
				}

				mysql_free_result($resultG);

 

Because nl2br ADDS a HTML line break and doesn't replace it, try the following...

 

That does not work either ???

Instead of returning the "straight" biography data, try using the following SQL query:

 

$guests_query = 'SELECT name, guestID, photour1, REPLACE(biography,"\n","<br>") AS biography FROM guests ORDER BY name';

 

If that doesn't work, try "\r\n" instead of "\n".

Instead of returning the "straight" biography data, try using the following SQL query:

 

$guests_query = 'SELECT name, guestID, photour1, REPLACE(biography,"\n","<br>") AS biography FROM guests ORDER BY name';

 

If that doesn't work, try "\are\n" instead of "\n".

 

Well, we can rule that one out as well. Both versions don't work unfortunately.

Uh, the second one was supposed to be a slash followed by the letter that follows "q", not the word "are."  Did you try it with the letter or the word as it originally appeared in the earlier post?

 

(Apparently, an overzealous admin has decided to translate for the illiterate, SMS-speaking, high school students on these boards.  Now if they can only write a script that can explain their problems clearly, we might have something.)

Uh, the second one was supposed to be a slash followed by the letter that follows "q", not the word "are." 

 

OMG that fixed it! Yes I used "are" instead of the "letter that follows q", and now I fixed that it actually works. Thanks you and everybody else for helping! Problem solved :)

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.