简体   繁体   中英

Need help styling PHP in HTML

This is my PHP coding from which I am grabbing data from my DB with a select where statement and outputting it. I am trying to change the text properties such as font, colour and size on the print statement can anybody assis with how to do this?

<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT fname, sname FROM tbl_stylist WHERE fname = 'liza'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        print " - Name: " . $row["fname"]. " " . $row["sname"]. "<br>";
    }
} else {
    echo "0 results";
}

mysqli_close($conn);
?>

You can style lists using HTML <ul> and <li> elements.

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>Stylists</title>
    <link rel="stylesheet" href="css/style.css" />
</head>
<body>
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT fname, sname FROM tbl_stylist WHERE fname = 'liza'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    echo "<ul>";

    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo "<li>Name: " . $row["fname"]. " " . $row["sname"]. "</li>";
    }
    echo "</ul>";
} else {
    echo "0 results";
}

mysqli_close($conn);
?></body>
</html>

And in css/style.css put something like this:

li {
   color: red;
   font-weight: bold;
   font-size: 12pt;
}

You can also put the style inline in the head section:

<style type="text/css">
li {
   color: red;
   font-weight: bold;
   font-size: 12pt;
}
</style>

You can output your markdown like this:

print "<div class = 'sampleclass'>  - Name: " . $row["fname"]. " " . 

Then style it by including the below in an attached css file

.sampleclass {
    font-size: 15px;
}

This is only about HTML and CSS, you could output directly your strings in HTML tags with echo. The script needs to be encapsulated into the HTML's body and a pre-defined CSS classes for styling:

<!doctype html>
<html>
  <head>
    ....
    <style type="text/css">
      .redBold {
         color: red;
         font-weight: bold;
      }
    </style>
  </head>
  <body>
    <?php
      ....
      echo '<span class="redBold">This text is red and bold</span>';
    ?>
  </body>
</html>

Of course you can also use inline styling:

 <span style="color: red; font-weight: bold;">Also this works</span> 

You can always use CSS file and define the values. For example:

body {
  font-family: arial;
  font-size: 15px;
}
<html>
  <head>
    <link rel="stylesheet" href="style.css">
  </head>
  <body>
    php txt
  </body>
</html>

Also, you can use <span> tags and style them. For example print this span using 'print' of php:

<span style="font-family: arial; font-size: 15px;">text</span>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM