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 to create offer tag with css and pass value to it

    How to fix text with limited height and absolute position overflows

    How to use Javascript to close css drop down menu on outside click?

    Getting unordered list in front of image slide-show in IE8, IE7 and probably IE6

    Show Link on image at hovering in jquery

    How to get On/Off FlipSwitch to trigger function

    How to make Bootstrap navbar brand image larger

    How to get Susy going with a CSS only toggle navigation

    How to center checkbox vertically in a table-cell?

    100% width = viewport width. How to make it as wide as is page body instead?

    How do you set a div's location to be centered when we don't know the screen size?

    How to implement small text list search functionality using CSS attribute selectors

    How to center the buttons and make them same size

    How to write css for fixed header and scrollable table

    How to make a content property in css use php

    How can i make my paragraph go to the next line after a certain width

    how I can change the size of font inside PHP

    CSS and html: how to style input file in css (aline the text with the choose file button)?

    How to force Visual Studio to honor the BOM at the start of a UTF-8 encoded CSS file?

    How to center social buttons in bootstrap login page?

    Showing Text When Hovering Over an Image Link

    How to add custom CSS for displaying on mobile screens in WordPress?

    How do you disable the set width of parent element for child element?

    How to get two results with one button using CSS & Jquery

    How to get the rotation of degree with getBoundingClientRect?

    Using CSS to show/hide content. How do I make hide the default

    How to test a webpage for different aspect ratios

    multiple CSS classes, how they work and WHY do it this way? [duplicate]

    How do you get multiple views in one window like JSFiddle does [closed]

    CSS menu - main option makes wider when submenu appears - how to fix it?