Jump to content


Content Display Problem


10 replies to this topic

#1 Braunson

    Young Padawan

  • Members
  • Pip
  • 237 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 28 March 2007 - 03:02 PM

Alright, im trying to display my article out of my database right.

Now it displays correctly. I'm using substr($row['content'], 0, 200); 'substr' right to make it seem like a description, then they can click read more.. But for the description part, and it displays instead of spaces and breaklines it displays \r\n\r\n\r\n\content\r\n\r\nso


How would I make the \r\n\r\n\r\n turn into spaces & breaklines.
Here's my full code.

<?php

//
// view/display articles,downloads,news.php - this displays the content from the database.
//

$get_category = htmlspecialchars($_GET['category']);
if($get_category){

$result = mysql_query("SELECT * FROM `articles` WHERE category = '$get_category' ORDER by aid DESC");

while($row = mysql_fetch_array($result)){
	 $aid = $row['aid'];
	 $subject = $row['subject'];
	 $content = substr($row['content'], 0, 200);
	 $views = $row['views'];
	 $postdate = $row['postdate'];
	 $category = $row['category'];

	echo "
			<a name=\"$subject\"></a>
			<h1>$subject</h1>
				  
				  <p>$content... <strong><a href=\"$domain/view_article/$aid/\">read more...</a></strong></p>
	   
	  
			<p class=\"comments align-right clear\">
				<a href=\"$domain/view_article/$aid/\">Read more</a> : 
				<a href=\"#\">comments(0)</a> :
				<a href=\"$domain/print/$aid/\">print</a> : 
				$postdate :
				$views total views
			</p>

			";
					}
} 
?>


Any help is appreaciated.

#2 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 28 March 2007 - 03:21 PM

You can use the PHP function nl2br() to transform all new line characters into the <br /> tag.

#3 Braunson

    Young Padawan

  • Members
  • Pip
  • 237 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 28 March 2007 - 03:54 PM

I thought i posted that I tryed that already, guess not.
That didnt work either.

#4 shameless_w_o_a_d

    Young Padawan

  • Members
  • Pip
  • 42 posts
  • Gender:Male
  • Location:New Zealand
  • Interests:Web Design, Graphics Design, Computers, Games, Music, Basketball

Posted 29 March 2007 - 03:30 AM

Not sure if this will work (I'm going off the top of my head on this one), but if it doesn't work I'm sure someone will correct me. ^_^

Note: change br to the proper tag, since I was unsure on whether the br tag would be rendered or not, so I figured I'd take the safe guard route to make sure.

<?php
	 
	 //
	 // view/display articles,downloads,news.php - this displays the content from the database.
	 //
	 
	 $get_category = htmlspecialchars($_GET['category']);
	 if($get_category){
	 
	 $result = mysql_query("SELECT * FROM `articles` WHERE category = '$get_category' ORDER by aid DESC");
	 
	 while($row = mysql_fetch_array($result)){
		  $aid = $row['aid'];
		  $subject = $row['subject'];
		  $content = str_replace(array("\r\n"), "br", substr($row['content'], 0, 200));
		  $views = $row['views'];
		  $postdate = $row['postdate'];
		  $category = $row['category'];
	 
		 echo "
				 <a name=\"$subject\"></a>
				 <h1>$subject</h1>
					   
					   <p>$content...  <strong><a href=\"$domain/view_article/$aid/\">read  more...</a></strong></p>
			
		   
				 <p class=\"comments align-right clear\">
					 <a href=\"$domain/view_article/$aid/\">Read more</a> : 
					 <a href=\"#\">comments(0)</a> :
					 <a href=\"$domain/print/$aid/\">print</a> : 
					 $postdate :
					 $views total views
				 </p>
	 
				 ";
						 }
	 } 
	 ?>


#5 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 29 March 2007 - 08:58 PM

The problems with your code are these:
1. You're not handling line breaks for ever OS. Some use i believe just \n, but windows uses \r\n.
2. "br" needs to be "<br>"

I know i've posted the solution for this before, a couple of times even. Search for nl2br and you will find it. But since i'm to lazy to do that myself, here
$content = substr(preg_replace("\r\n", '<br>', $row['content']), 0, 200);


#6 Braunson

    Young Padawan

  • Members
  • Pip
  • 237 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 30 March 2007 - 07:13 AM

Quote

Warning: preg_replace() [function.preg-replace]: Empty regular expression


:S

#7 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 30 March 2007 - 01:11 PM

oops...
$content = substr(preg_replace("#\r\n#", '<br>', $row['content']), 0, 200);


#8 Braunson

    Young Padawan

  • Members
  • Pip
  • 237 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 30 March 2007 - 10:16 PM

Dosnt work still... I dont get any error, but Im back to where I was before. Hmm... :whistle:

I even tryed using a function like this..
function nltobr($string)
 {
  $string = str_replace("\n","<br />",$string);
  return $string;
 }

Still nothing works, i dont get any error but im back to where I started.

Edited by rc69, 31 March 2007 - 03:59 PM.


#9 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 31 March 2007 - 04:05 PM

Oops again.
$content = substr(preg_replace("#(\r\n|\r|\n)#", '<br>', $row['content']), 0, 200);
I really should stop posting things without re-reading what i'm posting...

If that doesn't work, i have a question. When you say

Quote

it displays \r\n\r\n\r\n\content\r\n\r\n
Do you mean that literally? As in, if i went to the page i could see the \r\n's with my own eyes.
Not like when using a textarea where, if you hit the enter key, the \r\n is there but all we see is the cursor move down a line.

I ask because, in the world of PHP, "\r\n" and '\r\n' are two different things.

#10 Braunson

    Young Padawan

  • Members
  • Pip
  • 237 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 31 March 2007 - 05:11 PM

I mean it literally, heres an screenshot of it.

Attached File  previewwww.JPG   18.55K   89 downloads

#11 Braunson

    Young Padawan

  • Members
  • Pip
  • 237 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 01 April 2007 - 07:51 PM

Anything yet. I'm trying everything to get this fixed :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users