Morris2 Posted October 4, 2011 Share Posted October 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248425-how-to-inline-css-style-within-a-php-while-loop/ Share on other sites More sharing options...
gizmola Posted October 4, 2011 Share Posted October 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/248425-how-to-inline-css-style-within-a-php-while-loop/#findComment-1275731 Share on other sites More sharing options...
Morris2 Posted October 4, 2011 Author Share Posted October 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248425-how-to-inline-css-style-within-a-php-while-loop/#findComment-1275753 Share on other sites More sharing options...
gizmola Posted October 4, 2011 Share Posted October 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248425-how-to-inline-css-style-within-a-php-while-loop/#findComment-1275780 Share on other sites More sharing options...
Morris2 Posted October 5, 2011 Author Share Posted October 5, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248425-how-to-inline-css-style-within-a-php-while-loop/#findComment-1275942 Share on other sites More sharing options...
gizmola Posted October 5, 2011 Share Posted October 5, 2011 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"; Quote Link to comment https://forums.phpfreaks.com/topic/248425-how-to-inline-css-style-within-a-php-while-loop/#findComment-1276205 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.