Cinner Posted May 19, 2007 Share Posted May 19, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/ Share on other sites More sharing options...
Cinner Posted May 20, 2007 Author Share Posted May 20, 2007 I can't modify my first post it seems, but if I could, the title would be: "Help: multiple lines break my code". Hopefully somebody can help with this problem. Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-257489 Share on other sites More sharing options...
Lumio Posted May 20, 2007 Share Posted May 20, 2007 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-257501 Share on other sites More sharing options...
Cinner Posted May 20, 2007 Author Share Posted May 20, 2007 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)? Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-257517 Share on other sites More sharing options...
Lumio Posted May 20, 2007 Share Posted May 20, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-257528 Share on other sites More sharing options...
Cinner Posted May 20, 2007 Author Share Posted May 20, 2007 Sorry I made a typo, the content in the database is NOT stored with \n. So there's nothing to replace with str_replace, right? because the content comes from a database where it's stored with \n. Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-257599 Share on other sites More sharing options...
kenrbnsn Posted May 20, 2007 Share Posted May 20, 2007 It's probably stored in the database with a newline character, so the str_replace should work. Ken Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-257600 Share on other sites More sharing options...
Lumio Posted May 20, 2007 Share Posted May 20, 2007 "\n" will get replaced to a linebreak. Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-257675 Share on other sites More sharing options...
Cinner Posted May 20, 2007 Author Share Posted May 20, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-257703 Share on other sites More sharing options...
Cinner Posted May 20, 2007 Author Share Posted May 20, 2007 Correction, I'm looking for: line 1 <br> line 2 Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-257705 Share on other sites More sharing options...
chigley Posted May 20, 2007 Share Posted May 20, 2007 nl2br() Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-257795 Share on other sites More sharing options...
Cinner Posted May 20, 2007 Author Share Posted May 20, 2007 nl2br() 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 , '';?>" <? } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-257839 Share on other sites More sharing options...
Lumio Posted May 20, 2007 Share Posted May 20, 2007 <?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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-257842 Share on other sites More sharing options...
Cinner Posted May 21, 2007 Author Share Posted May 21, 2007 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 :-) Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-258210 Share on other sites More sharing options...
Lumio Posted May 21, 2007 Share Posted May 21, 2007 -.- linebreak = 13; echo ord("\n"); //13 Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-258368 Share on other sites More sharing options...
Cinner Posted May 21, 2007 Author Share Posted May 21, 2007 -.- 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 :-\ Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-258412 Share on other sites More sharing options...
Wildbug Posted May 21, 2007 Share Posted May 21, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-258490 Share on other sites More sharing options...
kalivos Posted May 21, 2007 Share Posted May 21, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-258506 Share on other sites More sharing options...
Cinner Posted May 22, 2007 Author Share Posted May 22, 2007 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 ??? Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-258911 Share on other sites More sharing options...
Wildbug Posted May 22, 2007 Share Posted May 22, 2007 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". Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-259160 Share on other sites More sharing options...
Cinner Posted May 22, 2007 Author Share Posted May 22, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-259199 Share on other sites More sharing options...
Wildbug Posted May 22, 2007 Share Posted May 22, 2007 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.) Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-259212 Share on other sites More sharing options...
Cinner Posted May 22, 2007 Author Share Posted May 22, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/52132-solved-need-help-with-simple-problem-dont-know-how-to-describe-it-in-one-line/#findComment-259254 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.