4 Replies - 593 Views - Last Post: 01 April 2012 - 07:31 AM Rate Topic: -----

#1 squibby  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 65
  • Joined: 21-January 12

Wordwrap with PHP string problem

Posted 01 April 2012 - 04:36 AM

Hello,

I have the following code that works fine:

	<div class = "wrapper-blog-display">
	  <div class = "blog-display-title">A TitleA TitleA TitleA</div>
	  <div class = "blog-display-postedby">A name</div>
	  <div class = "blog-display-post"> A long post about something  blah blah blah etc</div>
	  <div class = "blog-display-post-readmore"><a href = "a-file.php">Read More</a></div>
        </div>	





It displays a blog post, however naturally i need to have this work dynamically so i made the following code:


<?php

  include ('connect.php');
							
  $qry = ('SELECT * FROM blog');
  $result = mysql_query ($qry) or die();
    while($row = mysql_fetch_array($result))

	{			
      echo "<div class = 'wrapper-blog-display'>";
      echo "<div class = 'blog-display-title'>".  $row['title'] . "</div>"  ;
      echo "<br>";
      echo "<div class = 'blog-display-postedby'>Posted by:&nbsp;<font color=#00CC00>".  $row['createdby'] . "</font>&nbsp;&nbsp;" .$row['created']. "</div>"  ;
      echo "<br>"; 
      echo "<div class = 'blog-display-post'>".  $row['content'] . "</div>"  ;
      echo "<br>";
      echo "<div class = 'blog-display-post-readmore'><a href = '".  $row['filename'] . "'>Read More</a></div>"  ;
      echo "<br>";	
      echo  "</div>"; 
        }
?>	






When i use the first way the text inside the blog-display-post div tag displays fine and wraps over on to the next line. When i pull thee info from a database, all the text is on one long line instead of naturally wrapping when it hits the end of the div box. WHY?

I would assume it should do the same as the php generated version is exactly the same.

Does anyone know why or how to fix?

thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Wordwrap with PHP string problem

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5672
  • View blog
  • Posts: 22,524
  • Joined: 23-August 08

Re: Wordwrap with PHP string problem

Posted 01 April 2012 - 04:44 AM

Just to make sure we're all on the same page...the exact same content, all on one line in the first case, wraps fine, but the same content from the DB, all on one line, does not wrap?
Was This Post Helpful? 0
  • +
  • -

#3 squibby  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 65
  • Joined: 21-January 12

Re: Wordwrap with PHP string problem

Posted 01 April 2012 - 05:26 AM

Ok i got it kind of working like this. But is this the way i should be doing it?? Im sure it should work without doing this.


							  
 $myString = $row['content'];
 $myString = wordwrap($myString, 24, "<br />", true);

 echo "<div class = 'blog-display-post'>". $myString . "</div>"  ;





View PostJackOfAllTrades, on 01 April 2012 - 04:44 AM, said:

Just to make sure we're all on the same page...the exact same content, all on one line in the first case, wraps fine, but the same content from the DB, all on one line, does not wrap?



Yes this is correct. i will go back and triple check all my code.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5672
  • View blog
  • Posts: 22,524
  • Joined: 23-August 08

Re: Wordwrap with PHP string problem

Posted 01 April 2012 - 05:34 AM

Yeah, see, I would think it would wrap per the div size as defined in the CSS. Maybe someone more in tune with markup will be along to clarify. What you have will do the trick, though, but it won't "flow" if your window/div is resized.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,529
  • Joined: 08-August 08

Re: Wordwrap with PHP string problem

Posted 01 April 2012 - 07:31 AM

If you make it a little simpler you can more easily deal with the details:
PHP:
<?php
$dsn = "mysql:host=localhost;dbname=somedb";
$login = "root";
$password = "root";

$pdo = new PDO($login, $password, $dsn);
$query = "SELECT * FROM blog";
$rows = $pdo->prepare($query);
$rows->execute();

$output = "";
$posts = file_get_contents("Blogposts.html");
$replace = array("{blogtitle}", "{username}", "{blogpost}", "{readmore}");
foreach($rows as $row) {
	$output .= str_replace($replace,array($row['title'],$row['createdby'],$row['content'], $row['filename']), $posts);
}
echo $output;


Blogposts.html:
<div class = "wrapper-blog-display">
  <div class = "blog-display-title">{blogtitle}</div>
  <div class = "blog-display-postedby">Posted by: {username}</div>
  <div class = "blog-display-post">{blogpost}</div>
  <div class = "blog-display-post-readmore"><a href = "{readmore}">Read More</a></div>
       </div>	


connection.php:
<?php
// Use appropriate values here:
$dsn = "mysql:host=localhost;dbname=somedb";
$login = "root";
$password = "root";
?>


Oh, and using PDO is much better than outdated and insecure mysql_* functions.

This post has been edited by CTphpnwb: 01 April 2012 - 07:33 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1