Jump to content

How to inline CSS style within a PHP while loop


Morris2

Recommended Posts

Hi.

 

I have this working PHP script.

I want to inline CSS style the variable "total" and the free tekst "USD".

 

I want both to appear in bold and golden.

 

How do I do this? It seems to be a bit different to style within a PHP while loop, than in a normal HTML code.

 

<?php
$chart = mysql_query("SELECT * FROM Hitlist ORDER BY total DESC limit 5"); 

$num_rows = mysql_num_rows($chart);
while($row = mysql_fetch_assoc($chart))
{echo $row['artist']." ... "
.$row['city']." ("
.$row['country'].") "
.$row['total']." USD <br>";}
?>

 

Regards Morris

Link to comment
Share on other sites

This isn't really a php question -- you just need to echo out the html markup:

 

echo '' . $row['total'] . ' USD
';

 

You can echo out the actual inline style but I would highly recommend that you don't do that and instead refer to a style you set in your style sheet.  Actually using an inline style defeats the purpose of style sheets.

 

 

Link to comment
Share on other sites

 

Thanks. :-\

 

It is because I wanna keep the template in Joomla, but still style some objects differently.

 

Where in my PHP script above should I place this line?

Does this look right: echo '<span class="style="font-size: 18px">' . $row['total'] . ' USD</span><br>';

 

Morris

Link to comment
Share on other sites

 

Thanks. :-\

 

It is because I wanna keep the template in Joomla, but still style some objects differently.

 

Where in my PHP script above should I place this line?

Does this look right: echo '' . $row['total'] . ' USD

';

 

Morris

 

Just give your style a different name.  Do you understand css?  You simply need to create a class in one of your style sheets... could be the global joomla style sheet.  Adding a style that joomla doesn't use willl not effect any existing styles because you are going to reference this style by its name.

 

.bigHitList {
  font-size: 18px;
  color: yellow;
}

 

echo '' . $row['total'] . ' USD
';

 

Doing it this way, you can change the appearance of the span by changing the style sheet, rather than having to update the php code. 

This might help you:  http://www.csstutorial.net/css-intro/introductioncss-part1.php

Link to comment
Share on other sites

OK thanks.

I've placed the following code in the CSS file in my Joomla template:

 

bigHitList {

  font-size: 18px;

  color: yellow;

}

 

Exactly where should I insert the span-code-line in my own document below?

I've tried to place it here and there, but with no results.

 

echo '<span class="bigHitList">' . $row['total'] . ' USD</span><br>';

 

My own document:

 

chart = mysql_query("SELECT * FROM Hitlist ORDER BY total DESC limit 5"); 
$num_rows = mysql_num_rows($chart);     
while($row = mysql_fetch_assoc($chart))
{echo $row['artist']." ... "
.$row['city']." ("
.$row['country'].") "
.$row['total']." USD <br>";}

 

Though it is not a PHP question, I have to figure out where in this PHP script I'll have to place this span-code-line.

 

Best regards Morris

 

 

 

 

Link to comment
Share on other sites

Morris,

  You just need to tweak your output where you are already echoing out the values from the row.  I also am using a technique called "interpolation" that lets you embed your variables in one string instead of having to concatenate them all using the '.' operator.  Hopefully it will make it simpler to see what you're doing in the future and is a great technique to know.  The secret is that you need to wrap arrays inside the {} to get it to work.

 

chart = mysql_query("SELECT * FROM Hitlist ORDER BY total DESC limit 5"); 
$num_rows = mysql_num_rows($chart);     
while($row = mysql_fetch_assoc($chart)) {
  echo "{$row['artist']} ... {$row['city']}  ({$row['country']}) " . '' . "{$row['total']} USD
";
}

 

The important thing in php is to learn the difference between using single quotes and double quotes and how they work.  Single quotes make a string literal/constant.  It is exactly what it is.  Double quotes makes an "interpolated" string, where the php parser will go through it at runtime looking for variables to replace, and inserting their values.

 

$name = 'George';
$fruit = array();
$fruit['name'] = 'orange';
$literal = 'The value of the $name variable will not be interpolated here';
$interp = "The value of the name variable is $name";
$interparray = "I have an {$fruit['name']} in my lunch today";

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.