asdwal
asdwal

Reputation: 41

Styling a PHP echo

I'm trying to style my PHP echo with the class users however it does not seem to be applying. I have done some research, but can't workout the difference?

Could someone please point me in the right direction?

PHP

$result = mysql_query("SELECT * FROM user"); 
$num_rows = mysql_num_rows($result);
echo '<div class=\"users\">', 'There are ' ,$num_rows, ' registered users', '</div>';

CSS

.users{
    font-family: "Myriad Pro";
    font-weight: lighter;
    font-size: 16px;
    text-decoration: none;
    color: #FFF;
    text-align: center;
}

Upvotes: 0

Views: 185

Answers (4)

Poonam
Poonam

Reputation: 4631

change your echo

echo '<div class="users">There are ' .$num_rows. ' registered users</div>';

OR you can use as suggested by @ Sean Doe

echo '<div class="users">There are $num_rows registered users</div>';

OR You can write simply html and add variable in php

<?php

$result = mysql_query("SELECT * FROM user"); 
$num_rows = mysql_num_rows($result);

?>
<div class="users">There are <?php echo $num_rows; ?> registered users</div>

Upvotes: 3

Adarsh Nahar
Adarsh Nahar

Reputation: 319

change your code to this,

echo "<div class='users'>"."There are ".$num_rows." registered users"."</div>";

php strings are always concat with

".$var." not '.$var.'

Upvotes: -1

moonwave99
moonwave99

Reputation: 22817

Don't get mad with echo, PHP was born as an HTML preprocessor!

<?php

$result = mysql_query("SELECT * FROM user"); 
$num_rows = mysql_num_rows($result);

?>

<div class="users">There are <?php echo $num_rows; ?> registered users</div>

Besides that, consider using a template engine [Twig, Mustache, Smarty], and drop mysql_* functions in favour of more secure and modern PDO or mysqli_* ones.

Upvotes: 3

Alex Siri
Alex Siri

Reputation: 2864

You should not escape the double quotes, that's breaking the HTML.

Change your PHP code to:

$result = mysql_query("SELECT * FROM user"); 
$num_rows = mysql_num_rows($result);
echo '<div class="users">', 'There are ' ,$num_rows, ' registered users', '</div>';

The double quotes don't need escaping inside of single quotes.

Upvotes: 6

Related Questions