How to apply CSS to dynamically generated PHP database results


Tags: php,mysql,css

Problem :

I'm trying to work out how I can break up a large block of text with CSS or PHP into paragraphs or line breaks. My PHP script queries a MYSQL database and returns a random number of entries (sentences) depending on the number selected by user. The results are echoed out in one large paragraph.

Is there anyway I can break up this one paragraph into smaller paragraphs? I don't want a line break after each result.

I can do this with fixed text in HTML with <p> tags or <br> tags but how can I do it with dynamically generated results when I have no idea how long the block of text will be? I'm quite new to PHP. Any suggestions appreciated.

Here is the code:

<?php
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="db"; // Database name 
$tbl_name="sentence"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sn=$_POST['numberofsentences'];


$query="SELECT line FROM `sentence` ORDER BY rand() LIMIT $sn";
$result = mysql_query($query);

while ( $row = mysql_fetch_array($result) ) {
// do something with $row. The following echos the results and adds a space after each sentence.
echo $row['line'], "&nbsp";
}

// Close the database connection
mysql_close();

 ?>


Solution :

One quick and easy way would be to set a static amount of sentences per paragraph like 4 or 5 and use that in your loop like so:

$count = 0; 
while ($row = mysql_fetch_array($result)) { 
    // do something with $row. The following echos the results and adds a space after each sentence. 
    echo $row['line'], "&nbsp"; 
    if ($count >= 4) { 
        echo '<br />&nbsp;&nbsp;&nbsp;&nbsp;'; 
        $count = 0; 
    } else { 
        $count++; 
    } 
}

    CSS Howto..

    How do I make the Android WebView not ignore the 'height' css property?

    How to prevent CSS inheritance where child selectors fail?

    How can I align text using CSS but perform like printf in C? [closed]

    CSS - How to match class (one dynamic and one static) between two elements

    How can I use jQuery (or CSS) to sort HTML tables by column?

    How to combine two images into one so I can vertically align across all browsers?

    How can I apply an external CSS class to a span created with dojo.create?

    How get dimension of HTML shadow element via JavaScript

    how to select margin for a background image in CSS/CSS3?

    How could i make CSS apply to all in a div?

    How to make children auto fit parent's width only with CSS?

    How to display a header inline with paragraph text using divs or an alternative method

    How to correctly add nested divs

    How to apply common JavaScript and CSS for all my jsp pages in the application?

    HTML+Javascript: How can I Load Data in Div via Class?

    How To Apply jQuery CSS Selector

    How to split page in half using CSS?

    How can I specify distance between list items?

    How to make submenus drop down below parent menu, instead of flying out?

    How to show transition of an element that is expanding over nearby content?

    How to prevent left “post” section from dropping off?

    How to shift a background image with css

    How to make the slider move right and left alternatively

    How to float a word next to animated text

    How to get css value of an element inside array?

    How to customize ionic style components (lists and tabs with different shapes)

    how do i include css based on screen size of the device

    How to better write complex styles to an element via javascript?

    CSS - how to position content data-title

    How can I position this drupal block?