How add css style to php code

Hi, I want to create easy number generator, where generated numbers will be each one in yellow circle. Can you help me with adding css style to each one number in css.
After pressing button the numbers will be generated. Thanks a lot for help.

Code:

<!DOCTYPE html>
<HEAD>
 
</HEAD>
<BODY>
 
<?php
$number = rand(10,100);
 
 
 
 
echo "<span style=\"background-image:url(\"images/yellowcircle.png\")><?php echo $number; ?> </span>";
echo "<span style=\"background-image:url(\"images/yellowcircle.png\")><?php echo $number; ?> </span>"; 
 
 
 
 
 
 
 
 
echo "<BUTTON TYPE=\'button\"  onClick=\"history.go(0)\" >Refresh</BUTTON>";
?>
 
 
</BODY>
</html>

You can add css styling to a page generated by php just the same as you would an ordinary html page. Typically you would link to a style sheet in the head section, or put the styling in <style> tags within the head. But you need to be more specific about what your needs are.
I notice you are using some in-line styling, which is generally not good practice.
You also need to pay attantion to valid code, you don’t have any <html> tags on the page.

As you have the code, both numbers will be the same, was that your intention?

Just a guess at what you may want:-

<?php
  // Make number array
  $howMany = 2 ;

  for ($i = 1; $i <= $howMany; $i++) {
    $numbers[] = rand(10,100);
  }
?>
<!DOCTYPE html>
<html>
  <head>
    <title>Number Maker</title>
    <style>
      .numbers span {
        display: inline-block;
        width: 3em;
        padding: 0.9em 0;
        margin: 0.5em;
        text-align: center;
        background: yellow;
        border-radius: 50%;
      }
    </style>
  </head>
  <body>
    <p class="numbers">
      <?php foreach($numbers as $num) : ?>
      <span><?php echo $num ?></span>
      <?php endforeach ?>
    </p>
    <button>Refresh</button>
  </body>
</html>

You are absolutely best, it is exactly what I wanted. Thank you 100 times man.

<?php Header ("Content-type: text/css; charset=utf-8");?>

@nimka707, could you please explain what you mean by this in the context of this thread?

1 Like

Just a little problem, some numbers are generated twice times, any idea how to solve it please?
How to create condition to treat it?
Thanks a lot

This might work for you.

Inside the for loop, add a conditional to check if the $numbers array already has that value.

If it doesn’t, assign it. If it does, continue.

Can you help me little or show me a little piece of code please. I am helpless:(

In pseudo code, something like

for (...) {
 if(! in_array(...){
// assign here 
 }
}

http://php.net/manual/en/function.in-array.php

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.